-
Notifications
You must be signed in to change notification settings - Fork 84
Compilation with build tools API #876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/kotlin/com/compiler/server/compiler/components/CompilationLogger.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.compiler.server.compiler.components | ||
|
||
import com.compiler.server.model.ErrorDescriptor | ||
import com.compiler.server.model.ProjectSeveriry | ||
import com.compiler.server.model.TextInterval | ||
import org.jetbrains.kotlin.buildtools.api.KotlinLogger | ||
|
||
|
||
/** | ||
* This custom implementation of Kotlin Logger is needed for sending compilation logs to the user | ||
* on the frontend instead of printing them on the stderr. CompilationLogger extracts data from logs | ||
* and saves it in [compilationLogs] map, so that compilation messages can be later displayed to | ||
* the user, and their position can be marked in their code. | ||
|
||
* KotlinLogger interface will be changed in the future to contain more log details. | ||
* Implementation of the CompilationLogger should be therefore updated after KT-80963 is implemented. | ||
* | ||
* @property isDebugEnabled A flag to indicate whether debug-level logging is enabled for the logger. | ||
* If true, all messages are printed to the standard output. | ||
*/ | ||
class CompilationLogger( | ||
dkrasnoff marked this conversation as resolved.
Show resolved
Hide resolved
|
||
override val isDebugEnabled: Boolean = false, | ||
) : KotlinLogger { | ||
|
||
/** | ||
* Stores a collection of compilation logs organized by file paths. | ||
* | ||
* The map keys represent file paths as strings, and the associated values are mutable lists of | ||
* `ErrorDescriptor` objects containing details about compilation issues, such as error messages, | ||
* intervals, severity, and optional class names. | ||
*/ | ||
var compilationLogs: Map<String, MutableList<ErrorDescriptor>> = emptyMap() | ||
|
||
override fun debug(msg: String) { | ||
if (isDebugEnabled) println("[DEBUG] $msg") | ||
} | ||
|
||
override fun error(msg: String, throwable: Throwable?) { | ||
if (isDebugEnabled) println("[ERROR] $msg" + (throwable?.let { ": ${it.message}" } ?: "")) | ||
try { | ||
addCompilationLog(msg, ProjectSeveriry.ERROR, classNameOverride = null) | ||
} catch (_: Exception) {} | ||
} | ||
|
||
override fun info(msg: String) { | ||
if (isDebugEnabled) println("[INFO] $msg") | ||
} | ||
|
||
override fun lifecycle(msg: String) { | ||
if (isDebugEnabled) println("[LIFECYCLE] $msg") | ||
} | ||
|
||
override fun warn(msg: String, throwable: Throwable?) { | ||
if (isDebugEnabled) println("[WARN] $msg" + (throwable?.let { ": ${it.message}" } ?: "")) | ||
try { | ||
addCompilationLog(msg, ProjectSeveriry.WARNING, classNameOverride = "WARNING") | ||
} catch (_: Exception) {} | ||
} | ||
|
||
|
||
/** | ||
* Adds a compilation log entry to the `compilationLogs` map based on the string log. | ||
* | ||
* @param msg The raw log message containing information about the compilation event, | ||
* including the file path and error details. | ||
* @param severity The severity level of the compilation event, represented by the `ProjectSeveriry` enum. | ||
* @param classNameOverride An optional override for the class name that will be recorded in the log. | ||
* If null, it will be derived from the file path in the message. | ||
*/ | ||
private fun addCompilationLog(msg: String, severity: ProjectSeveriry, classNameOverride: String?) { | ||
val path = msg.split(" ")[0] | ||
val className = path.split("/").last().split(".").first() | ||
val message = msg.split(path)[1].drop(1) | ||
val splitPath = path.split(":") | ||
val line = splitPath[splitPath.size - 4].toInt() - 1 | ||
val ch = splitPath[splitPath.size - 3].toInt() - 1 | ||
val endLine = splitPath[splitPath.size - 2].toInt() - 1 | ||
val endCh = splitPath[splitPath.size - 1].toInt() - 1 | ||
val ed = ErrorDescriptor( | ||
TextInterval(TextInterval.TextPosition(line, ch), TextInterval.TextPosition(endLine, endCh)), | ||
message, | ||
severity, | ||
classNameOverride ?: className | ||
) | ||
compilationLogs["$className.kt"]?.add(ed) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/compiler/server/configuration/BuildToolsConfig.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.compiler.server.configuration | ||
|
||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class BuildToolsConfig { | ||
init { | ||
/** | ||
* This flag is used by KotlinMessageRenderer in kotlin-build-tools-api to properly format | ||
* returned log messages during compilation. When this flag is set, the whole position of | ||
* a warning/error is returned instead of only the beginning. We need this behavior to | ||
* process messages in KotlinLogger and then correctly mark errors on the frontend. | ||
* | ||
* Setting this property should be removed after KT-80963 is implemented, as KotlinLogger | ||
* will return the full position of an error by default then. | ||
*/ | ||
System.setProperty("org.jetbrains.kotlin.buildtools.logger.extendedLocation", "true") | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.