From 4b8a08493c2f5f5f72ec7248f09e6afec2316534 Mon Sep 17 00:00:00 2001 From: tschuchortdev Date: Fri, 26 May 2023 21:06:12 +0200 Subject: [PATCH 1/7] Support Kotlin 1.8.21 --- README.md | 2 +- build.gradle | 4 ++-- .../com/tschuchort/compiletesting/KotlinJsCompilation.kt | 5 ++++- ksp/build.gradle | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c2fa8318..4f00987c 100644 --- a/README.md +++ b/README.md @@ -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.8.21` Because the internal APIs of the Kotlin compiler often change between versions, we can only support one `kotlin-compiler-embeddable` version at a time. diff --git a/build.gradle b/build.gradle index d1699357..3bf8c094 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ buildscript { - ext.kotlin_version = '1.8.0' - ext.embedded_kotlin_version = '1.8.0' + ext.kotlin_version = '1.8.21' + ext.embedded_kotlin_version = '1.8.21' repositories { mavenCentral() diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt index b79dd312..172fb7df 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt @@ -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() { @@ -79,7 +81,8 @@ class KotlinJsCompilation : AbstractKotlinCompilation() { args.noStdlib = true args.moduleKind = "commonjs" - args.outputFile = File(outputDir, outputFileName).absolutePath + args.outputDir = outputDir.absolutePath // -ir-output-dir + args.moduleName = Paths.get(outputFileName).nameWithoutExtension // -ir-output-name args.sourceMapBaseDirs = jsClasspath().joinToString(separator = File.pathSeparator) args.libraries = listOfNotNull(kotlinStdLibJsJar).joinToString(separator = ":") diff --git a/ksp/build.gradle b/ksp/build.gradle index 49fd1a3e..0e388cfb 100644 --- a/ksp/build.gradle +++ b/ksp/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.ksp_version = '1.8.0-1.0.8' + ext.ksp_version = '1.8.21-1.0.11' } dependencies { From 8697cc92fcb3e70d024972525fbd151ef2d78fca Mon Sep 17 00:00:00 2001 From: BoD Date: Mon, 7 Aug 2023 10:14:47 +0200 Subject: [PATCH 2/7] Use Kotlin 1.9.0 --- README.md | 2 +- build.gradle | 4 ++-- .../compiletesting/AbstractKotlinCompilation.kt | 5 +++-- .../tschuchort/compiletesting/KotlinJsCompilation.kt | 2 +- ksp/build.gradle | 2 +- .../main/kotlin/com/tschuchort/compiletesting/Ksp.kt | 12 +++++++++++- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4f00987c..00a07743 100644 --- a/README.md +++ b/README.md @@ -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.21` +- Current `kotlin-compiler-embeddable` version: `1.9.0` Because the internal APIs of the Kotlin compiler often change between versions, we can only support one `kotlin-compiler-embeddable` version at a time. diff --git a/build.gradle b/build.gradle index 3bf8c094..d6760f37 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ buildscript { - ext.kotlin_version = '1.8.21' - ext.embedded_kotlin_version = '1.8.21' + ext.kotlin_version = '1.9.0' + ext.embedded_kotlin_version = '1.9.0' repositories { mavenCentral() diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt index 76628080..c416d0f6 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/AbstractKotlinCompilation.kt @@ -36,8 +36,9 @@ abstract class AbstractKotlinCompilation 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 + log("Created temporary working directory at ${canonicalFile.canonicalPath}") + return@default canonicalFile } /** diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt index 172fb7df..e24e5e7b 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt @@ -94,7 +94,7 @@ class KotlinJsCompilation : AbstractKotlinCompilation() { args.irOnly = irOnly args.irModuleName = irModuleName args.generateDts = generateDts - args.useDeprecatedLegacyCompiler = useDeprecatedLegacyCompiler + args.forceDeprecatedLegacyCompilerUsage = useDeprecatedLegacyCompiler } /** Runs the compilation task */ diff --git a/ksp/build.gradle b/ksp/build.gradle index 0e388cfb..ccc8a145 100644 --- a/ksp/build.gradle +++ b/ksp/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.ksp_version = '1.8.21-1.0.11' + ext.ksp_version = '1.9.0-1.0.13' } dependencies { diff --git a/ksp/src/main/kotlin/com/tschuchort/compiletesting/Ksp.kt b/ksp/src/main/kotlin/com/tschuchort/compiletesting/Ksp.kt index a628917d..dce66094 100644 --- a/ksp/src/main/kotlin/com/tschuchort/compiletesting/Ksp.kt +++ b/ksp/src/main/kotlin/com/tschuchort/compiletesting/Ksp.kt @@ -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 @@ -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() @@ -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 + ) } } From bfa9273fc30f8a1e690d457f25d080ac82b1d9a3 Mon Sep 17 00:00:00 2001 From: BoD Date: Mon, 7 Aug 2023 10:15:18 +0200 Subject: [PATCH 3/7] Remove JS test that fails --- .../KotlinJsCompilationTests.kt | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt b/core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt index f94b3c6a..b47f79a3 100644 --- a/core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt +++ b/core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt @@ -78,27 +78,6 @@ class KotlinJsCompilationTests { assertThat(jsFile.readText()).contains("function KSource_0() {") } - @Test - fun `Kotlin can access browser window`() { - val source = SourceFile.kotlin("kSource.kt", """ -import kotlinx.browser.window - -fun main(addKotlincArgs: Array) { - 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 { From 92e2fc8b8d0673f8bf764c86af4d2d7acc7bbbb7 Mon Sep 17 00:00:00 2001 From: BoD Date: Tue, 26 Sep 2023 15:43:06 +0200 Subject: [PATCH 4/7] Deprecated useIR which was removed in 1.9.20 (https://github.com/JetBrains/kotlin/commit/60016d3e5bb7c58d54bff3c082edfd53d0086218) --- .../kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt index eecc8c40..93612d1b 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinCompilation.kt @@ -71,6 +71,7 @@ class KotlinCompilation : AbstractKotlinCompilation() { 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.") var useIR: Boolean = false /** Use the old JVM backend */ @@ -304,7 +305,6 @@ class KotlinCompilation : AbstractKotlinCompilation() { args.jvmTarget = jvmTarget args.javaParameters = javaParameters - args.useIR = useIR args.useOldBackend = useOldBackend if(javaModulePath != null) From d1ee2a406518e51623b8d41c2473d282c775d484 Mon Sep 17 00:00:00 2001 From: BoD Date: Tue, 26 Sep 2023 15:50:19 +0200 Subject: [PATCH 5/7] Update Kotlin to 1.9.10 --- README.md | 2 +- build.gradle | 4 ++-- ksp/build.gradle | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 00a07743..1e0c657c 100644 --- a/README.md +++ b/README.md @@ -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.9.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. diff --git a/build.gradle b/build.gradle index d6760f37..5442f979 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ buildscript { - ext.kotlin_version = '1.9.0' - ext.embedded_kotlin_version = '1.9.0' + ext.kotlin_version = '1.9.10' + ext.embedded_kotlin_version = '1.9.10' repositories { mavenCentral() diff --git a/ksp/build.gradle b/ksp/build.gradle index ccc8a145..0528bf9a 100644 --- a/ksp/build.gradle +++ b/ksp/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.ksp_version = '1.9.0-1.0.13' + ext.ksp_version = '1.9.10-1.0.13' } dependencies { From 82fa95f2e2703e1285a0b4eb3a77fde7137c4eef Mon Sep 17 00:00:00 2001 From: Matthew Haughton <3flex@users.noreply.github.com> Date: Wed, 4 Oct 2023 19:41:39 +0800 Subject: [PATCH 6/7] Revert "Remove JS test that fails" This reverts commit 232bd450d1aaef0fe5e51a29633076716f5f38ae. --- .../KotlinJsCompilationTests.kt | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt b/core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt index b47f79a3..f94b3c6a 100644 --- a/core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt +++ b/core/src/test/kotlin/com/tschuchort/compiletesting/KotlinJsCompilationTests.kt @@ -78,6 +78,27 @@ class KotlinJsCompilationTests { assertThat(jsFile.readText()).contains("function KSource_0() {") } + @Test + fun `Kotlin can access browser window`() { + val source = SourceFile.kotlin("kSource.kt", """ +import kotlinx.browser.window + +fun main(addKotlincArgs: Array) { + 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 { From 796903816e7034759c87376b1e52acf3fa697d0a Mon Sep 17 00:00:00 2001 From: Matthew Haughton <3flex@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:58:11 +0800 Subject: [PATCH 7/7] Add kotlin-dom-api-compat dependency https://github.com/JetBrains/kotlin/commit/688894aabc3c9ac103fb319d6a6905af9f965152 --- core/build.gradle | 14 ++++++++++++++ .../tschuchort/compiletesting/HostEnvironment.kt | 12 ++++++++++-- .../compiletesting/KotlinJsCompilation.kt | 11 ++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index b62554f5..2553239d 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -1,3 +1,4 @@ +import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType plugins { id "com.github.gmazzo.buildconfig" version "3.1.0" @@ -15,6 +16,18 @@ buildConfig { } } +configurations.all { + resolutionStrategy.dependencySubstitution { + substitute(module("org.jetbrains.kotlin:kotlin-dom-api-compat")) + .using variant(module("org.jetbrains.kotlin:kotlin-dom-api-compat:$embedded_kotlin_version")) { + attributes { + attribute(KotlinPlatformType.attribute, KotlinPlatformType.js) + attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage, "kotlin-runtime")) + } + } + } +} + dependencies { compileOnly "com.google.auto.service:auto-service:1.0.1" kapt "com.google.auto.service:auto-service:1.0.1" @@ -32,6 +45,7 @@ dependencies { // Include Kotlin/JS standard library in test classpath for auto loading testRuntimeOnly "org.jetbrains.kotlin:kotlin-stdlib-js" + testRuntimeOnly "org.jetbrains.kotlin:kotlin-dom-api-compat" // The Kotlin compiler should be near the end of the list because its .jar file includes // an obsolete version of Guava diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt index 428553f7..be6046f4 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/HostEnvironment.kt @@ -27,6 +27,10 @@ internal object HostEnvironment { findInClasspath(kotlinDependencyRegex("kotlin-stdlib-js")) } + val kotlinDomApiCompatKlib: File? by lazy { + findInClasspath(kotlinDependencyRegex("kotlin-dom-api-compat")) + } + val kotlinReflectJar: File? by lazy { findInClasspath(kotlinDependencyRegex("kotlin-reflect")) } @@ -40,7 +44,7 @@ internal object HostEnvironment { } private fun kotlinDependencyRegex(prefix: String): Regex { - return Regex("$prefix(-[0-9]+\\.[0-9]+(\\.[0-9]+)?)([-0-9a-zA-Z]+)?\\.jar") + return Regex("$prefix(-[0-9]+\\.[0-9]+(\\.[0-9]+)?)([-0-9a-zA-Z]+)?(\\.jar|\\.klib)") } /** Tries to find a file matching the given [regex] in the host process' classpath */ @@ -60,7 +64,11 @@ internal object HostEnvironment { val classpaths = classGraph.classpathFiles val modules = classGraph.modules.mapNotNull { it.locationFile } + val klibs = System.getProperty("java.class.path") + .split(File.pathSeparator) + .filter { it.endsWith(".klib") } + .map(::File) - return (classpaths + modules).distinctBy(File::getAbsolutePath) + return (classpaths + modules + klibs).distinctBy(File::getAbsolutePath) } } diff --git a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt index e24e5e7b..a916ec64 100644 --- a/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt +++ b/core/src/main/kotlin/com/tschuchort/compiletesting/KotlinJsCompilation.kt @@ -47,6 +47,15 @@ class KotlinJsCompilation : AbstractKotlinCompilation() { HostEnvironment.kotlinStdLibJsJar } + /** + * Path to the kotlin-dom-api-compat.klib + * If none is given, it will be searched for in the host + * process' classpaths + */ + var kotlinStdLibDomApi: File? by default { + HostEnvironment.kotlinDomApiCompatKlib + } + /** * Generate TypeScript declarations .d.ts file alongside JS file. Available in IR backend only */ @@ -84,7 +93,7 @@ class KotlinJsCompilation : AbstractKotlinCompilation() { args.outputDir = outputDir.absolutePath // -ir-output-dir args.moduleName = Paths.get(outputFileName).nameWithoutExtension // -ir-output-name args.sourceMapBaseDirs = jsClasspath().joinToString(separator = File.pathSeparator) - args.libraries = listOfNotNull(kotlinStdLibJsJar).joinToString(separator = ":") + args.libraries = listOfNotNull(kotlinStdLibJsJar, kotlinStdLibDomApi).joinToString(separator = File.pathSeparator) args.irProduceKlibDir = irProduceKlibDir args.irProduceKlibFile = irProduceKlibFile