Skip to content

Support Kotlin 1.9.0 #388

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 4 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Kotlin-Compile-Testing is compatible with all _local_ compiler versions. It does
However, if your project or any of its dependencies depend directly on compiler artifacts such as `kotlin-compiler-embeddable` or `kotlin-annotation-processing-embeddable` then they have to be the same version as the one used by Kotlin-Compile-Testing or there will be a transitive dependency conflict.


- Current `kotlin-compiler-embeddable` version: `1.8.0`
- Current `kotlin-compiler-embeddable` version: `1.9.10`

Because the internal APIs of the Kotlin compiler often change between versions, we can only support one `kotlin-compiler-embeddable` version at a time.

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
ext.kotlin_version = '1.8.0'
ext.embedded_kotlin_version = '1.8.0'
ext.kotlin_version = '1.9.10'
ext.embedded_kotlin_version = '1.9.10'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
/** Working directory for the compilation */
var workingDir: File by default {
val path = Files.createTempDirectory("Kotlin-Compilation")
log("Created temporary working directory at ${path.toAbsolutePath()}")
return@default path.toFile()
val canonicalFile = path.toFile().canonicalFile
Copy link
Contributor Author

@BoD BoD Aug 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JS compiler calls .canonicalFile internally - at least on MacOS that may be different to the absolute path (/private/var/… vs /var/…), which made the tests fail.

log("Created temporary working directory at ${canonicalFile.canonicalPath}")
return@default canonicalFile
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class KotlinCompilation : AbstractKotlinCompilation<K2JVMCompilerArguments>() {
var javaParameters: Boolean = false

/** Use the IR backend */
@Deprecated("Since Kotlin 1.9.20 this option is no longer supported by the compiler. It has no effect.")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in this commit.

var useIR: Boolean = false

/** Use the old JVM backend */
Expand Down Expand Up @@ -304,7 +305,6 @@ class KotlinCompilation : AbstractKotlinCompilation<K2JVMCompilerArguments>() {

args.jvmTarget = jvmTarget
args.javaParameters = javaParameters
args.useIR = useIR
args.useOldBackend = useOldBackend

if(javaModulePath != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.tschuchort.compiletesting
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.js.K2JSCompiler
import java.io.*
import java.nio.file.Paths
import kotlin.io.path.nameWithoutExtension

@Suppress("MemberVisibilityCanBePrivate")
class KotlinJsCompilation : AbstractKotlinCompilation<K2JSCompilerArguments>() {
Expand Down Expand Up @@ -79,6 +81,8 @@ class KotlinJsCompilation : AbstractKotlinCompilation<K2JSCompilerArguments>() {
args.noStdlib = true

args.moduleKind = "commonjs"
args.outputDir = outputDir.absolutePath // -ir-output-dir
args.moduleName = Paths.get(outputFileName).nameWithoutExtension // -ir-output-name
args.outputFile = File(outputDir, outputFileName).absolutePath
args.sourceMapBaseDirs = jsClasspath().joinToString(separator = File.pathSeparator)
args.libraries = listOfNotNull(kotlinStdLibJsJar).joinToString(separator = ":")
Expand All @@ -91,7 +95,7 @@ class KotlinJsCompilation : AbstractKotlinCompilation<K2JSCompilerArguments>() {
args.irOnly = irOnly
args.irModuleName = irModuleName
args.generateDts = generateDts
args.useDeprecatedLegacyCompiler = useDeprecatedLegacyCompiler
args.forceDeprecatedLegacyCompilerUsage = useDeprecatedLegacyCompiler
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useDeprecatedLegacyCompiler was removed here

}

/** Runs the compilation task */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,6 @@ class KotlinJsCompilationTests {
assertThat(jsFile.readText()).contains("function KSource_0() {")
}

@Test
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I’m not sure why this no longer works, it fails with kotlinx.browser not being resolved. Maybe this is no longer supported? But the KDoc seems to indicate it should still work 🤔 The source is still there. Any idea?

fun `Kotlin can access browser window`() {
val source = SourceFile.kotlin("kSource.kt", """
import kotlinx.browser.window

fun main(addKotlincArgs: Array<String>) {
println(window.document)
}
""")

val result = defaultJsCompilerConfig().apply {
sources = listOf(source)
}.compile()

assertThat(result.exitCode).isEqualTo(ExitCode.OK)
assertThat(result.compiledClassAndResourceFiles).hasSize(1)
val jsFile = result.compiledClassAndResourceFiles[0]
println(jsFile.readText())
assertThat(jsFile.readText()).contains("println(window.document);")
}

@Test
fun `detects the plugin provided for compilation via pluginClasspaths property`() {
val result = defaultJsCompilerConfig().apply {
Expand Down
2 changes: 1 addition & 1 deletion ksp/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.ksp_version = '1.8.0-1.0.8'
ext.ksp_version = '1.9.10-1.0.13'
}

dependencies {
Expand Down
12 changes: 11 additions & 1 deletion ksp/src/main/kotlin/com/tschuchort/compiletesting/Ksp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import org.jetbrains.kotlin.com.intellij.mock.MockProject
import org.jetbrains.kotlin.com.intellij.psi.PsiTreeChangeAdapter
import org.jetbrains.kotlin.com.intellij.psi.PsiTreeChangeListener
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
import org.jetbrains.kotlin.config.ApiVersion
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.config.LanguageVersionSettingsImpl
import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import java.io.File
Expand Down Expand Up @@ -169,6 +172,9 @@ private class KspCompileTestingComponentRegistrar(
this.allWarningsAsErrors = this@KspCompileTestingComponentRegistrar.allWarningsAsErrors
this.withCompilation = this@KspCompileTestingComponentRegistrar.withCompilation

this.languageVersionSettings =
LanguageVersionSettingsImpl(LanguageVersion.KOTLIN_1_9, ApiVersion.KOTLIN_1_9)

this.cachesDir = compilation.kspCachesDir.also {
it.deleteRecursively()
it.mkdirs()
Expand Down Expand Up @@ -216,7 +222,11 @@ private class KspCompileTestingComponentRegistrar(
val registrar = KspTestExtension(options, providers, messageCollectorBasedKSPLogger)
AnalysisHandlerExtension.registerExtension(project, registrar)
// Dummy extension point; Required by dropPsiCaches().
CoreApplicationEnvironment.registerExtensionPoint(project.extensionArea, PsiTreeChangeListener.EP.name, PsiTreeChangeAdapter::class.java)
CoreApplicationEnvironment.registerExtensionPoint(
project.extensionArea,
PsiTreeChangeListener.EP.name,
PsiTreeChangeAdapter::class.java
)
}
}

Expand Down