Skip to content

Commit 1f40004

Browse files
committed
Small fixes
1 parent 5fb1f66 commit 1f40004

File tree

6 files changed

+46
-39
lines changed

6 files changed

+46
-39
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ dependencies {
4747
implementation(libs.junit)
4848
implementation(libs.logback.logstash.encoder)
4949
implementation(libs.kotlin.reflect)
50-
implementation(libs.bundles.kotlin.stdlib)
50+
implementation(libs.kotlin.stdlib)
5151
implementation(libs.kotlin.script.runtime)
5252
implementation(libs.kotlin.build.tools.api)
5353
implementation(libs.kotlin.build.tools.impl)

dependencies/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ dependencies {
103103
kotlinDependency(libs.hamcrest)
104104
kotlinDependency(libs.bundles.jackson)
105105
// Kotlin libraries
106-
kotlinDependency(libs.bundles.kotlin.stdlib)
106+
kotlinDependency(libs.kotlin.stdlib)
107107
kotlinDependency(libs.kotlin.test)
108108
kotlinDependency(libs.kotlin.test.junit)
109109
kotlinDependency(libs.kotlinx.coroutines.core.jvm)

gradle/libs.versions.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ gradle-develocity = "3.17.5"
2121
[libraries]
2222
kotlin-reflect = { group = "org.jetbrains.kotlin", name = "kotlin-reflect", version.ref = "kotlin" }
2323
kotlin-stdlib = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib", version.ref = "kotlin" }
24-
kotlin-stdlib-jdk7 = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-jdk7", version.ref = "kotlin" }
25-
kotlin-stdlib-jdk8 = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-jdk8", version.ref = "kotlin" }
2624
kotlin-stdlib-js = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-js", version.ref = "kotlin" }
2725
kotlin-stdlib-wasm-js = { group = "org.jetbrains.kotlin", name = "kotlin-stdlib-wasm-js", version.ref = "kotlin" }
2826
kotlin-test = { group = "org.jetbrains.kotlin", name = "kotlin-test", version.ref = "kotlin" }
@@ -70,7 +68,6 @@ spring-context-indexer = { group = "org.springframework", name = "spring-context
7068
springdoc = { group = "org.springdoc", name = "springdoc-openapi-starter-webmvc-ui", version.ref = "springdoc" }
7169

7270
[bundles]
73-
kotlin-stdlib = ["kotlin-stdlib", "kotlin-stdlib-jdk7", "kotlin-stdlib-jdk8"]
7471
jackson = ["jackson-databind", "jackson-core", "jackson-annotations"]
7572
compose = [
7673
"compose-runtime",

src/main/kotlin/com/compiler/server/compiler/components/CompilationLogger.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import org.jetbrains.kotlin.buildtools.api.KotlinLogger
77

88

99
/**
10-
* CompilationLogger is used for collecting logs from compilation with kotlin-build-tools-api.
11-
* It is passed as an argument while executing a compilation operation. It collects logs returned
12-
* during compilation, interprets returned strings, and saves logs in the map based on their severity.
13-
* The map is later used to provide information about warnings and errors in the user's code.
14-
*
10+
* This custom implementation of Kotlin Logger is needed for sending compilation logs to the user
11+
* on the frontend instead of printing them on the stderr. CompilationLogger extracts data from logs
12+
* and saves it in [compilationLogs] map, so that compilation messages can be later displayed to
13+
* the user, and their position can be marked in their code.
14+
1515
* KotlinLogger interface will be changed in the future to contain more log details.
1616
* Implementation of the CompilationLogger should be therefore updated after KT-80963 is implemented.
1717
*
@@ -59,7 +59,7 @@ class CompilationLogger(
5959

6060

6161
/**
62-
* Adds a compilation log entry to the `compilationLogs` map based on the sting log.
62+
* Adds a compilation log entry to the `compilationLogs` map based on the string log.
6363
*
6464
* @param msg The raw log message containing information about the compilation event,
6565
* including the file path and error details.

src/main/kotlin/com/compiler/server/compiler/components/KotlinCompiler.kt

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ class KotlinCompiler(
106106
}
107107
}
108108

109-
@OptIn(ExperimentalPathApi::class)
110109
fun compile(files: List<ProjectFile>, userCompilerArguments: Map<String, Any>): CompilationResult<JvmClasses> =
111110
usingTempDirectory { inputDir ->
112111
val ioFiles = files.writeToIoFiles(inputDir)
@@ -139,40 +138,42 @@ class KotlinCompiler(
139138
val jvmToolchain = toolchains.getToolchain(JvmPlatformToolchain::class.java)
140139
val operation = jvmToolchain.createJvmCompilationOperation(sources, outputDir)
141140
operation.compilerArguments.applyArgumentStrings(arguments)
142-
val session = toolchains.createBuildSession()
143141

144-
val result = try {
145-
session.executeOperation(operation, toolchains.createInProcessExecutionPolicy(), logger)
146-
} catch (e: Exception) {
147-
throw Exception("Exception executing compilation operation", e)
142+
toolchains.createBuildSession().use { session ->
143+
val result = try {
144+
session.executeOperation(operation, toolchains.createInProcessExecutionPolicy(), logger)
145+
} catch (e: Exception) {
146+
throw Exception("Exception executing compilation operation", e)
147+
}
148+
return toCompilationResult(result, logger, outputDir)
148149
}
150+
}
149151

150-
try {
151-
return when (result) {
152-
org.jetbrains.kotlin.buildtools.api.CompilationResult.COMPILATION_SUCCESS -> {
153-
val compilerDiagnostics = CompilerDiagnostics(logger.compilationLogs)
154-
val outputFiles = buildMap {
155-
outputDir.visitFileTree {
156-
onVisitFile { file, _ ->
157-
put(file.relativeTo(outputDir).pathString, file.readBytes())
158-
FileVisitResult.CONTINUE
159-
}
160-
}
152+
private fun toCompilationResult(
153+
buildResult: org.jetbrains.kotlin.buildtools.api.CompilationResult,
154+
logger: CompilationLogger,
155+
outputDir: Path,
156+
): CompilationResult<JvmClasses> = when (buildResult) {
157+
org.jetbrains.kotlin.buildtools.api.CompilationResult.COMPILATION_SUCCESS -> {
158+
val compilerDiagnostics = CompilerDiagnostics(logger.compilationLogs)
159+
val outputFiles = buildMap {
160+
outputDir.visitFileTree {
161+
onVisitFile { file, _ ->
162+
put(file.relativeTo(outputDir).pathString, file.readBytes())
163+
FileVisitResult.CONTINUE
161164
}
162-
Compiled(
163-
compilerDiagnostics = compilerDiagnostics,
164-
result = JvmClasses(
165-
files = outputFiles,
166-
mainClasses = findMainClasses(outputFiles),
167-
)
168-
)
169165
}
170-
171-
else -> NotCompiled(CompilerDiagnostics(logger.compilationLogs))
172166
}
173-
} finally {
174-
session.close()
167+
Compiled(
168+
compilerDiagnostics = compilerDiagnostics,
169+
result = JvmClasses(
170+
files = outputFiles,
171+
mainClasses = findMainClasses(outputFiles),
172+
)
173+
)
175174
}
175+
176+
else -> NotCompiled(CompilerDiagnostics(logger.compilationLogs))
176177
}
177178

178179
private fun findMainClasses(outputFiles: Map<String, ByteArray>): Set<String> =

src/main/kotlin/com/compiler/server/configuration/BuildToolsConfig.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import org.springframework.context.annotation.Configuration
55
@Configuration
66
class BuildToolsConfig {
77
init {
8+
/**
9+
* This flag is used by KotlinMessageRenderer in kotlin-build-tools-api to properly format
10+
* returned log messages during compilation. When this flag is set, the whole position of
11+
* a warning/error is returned instead of only the beginning. We need this behavior to
12+
* process messages in KotlinLogger and then correctly mark errors on the frontend.
13+
*
14+
* Setting this property should be removed after KT-80963 is implemented, as KotlinLogger
15+
* will return the full position of an error by default then.
16+
*/
817
System.setProperty("org.jetbrains.kotlin.buildtools.logger.extendedLocation", "true")
918
}
1019
}

0 commit comments

Comments
 (0)