Hi everyone! I'm working on adding <OpenRewrite> s...
# gradle
t
Hi everyone! I'm working on adding OpenRewrite support for Kotlin 2.+. I am trying to build on top of the following PR. When trying to build the
rewrite-kotlin
module I get the following error:
Copy code
Caused by: java.lang.NoSuchMethodError: 'org.jetbrains.kotlin.buildtools.api.jvm.ClasspathEntrySnapshot org.jetbrains.kotlin.buildtools.api.CompilationService.calculateClasspathSnapshot(java.io.File, org.jetbrains.kotlin.buildtools.api.jvm.ClassSnapshotGranularity, boolean)'
along with the following warning:
Copy code
w: ⚠️ 'org.jetbrains.kotlin:kotlin-compiler-embeddable' Artifact Present in Build Classpath
The artifact `org.jetbrains.kotlin:kotlin-compiler-embeddable` is present in the build classpath along Kotlin Gradle plugin.
This may lead to unpredictable and inconsistent behavior.
I think it's important mentioning that I get when same error even when just trying to upgrade from Kotlin 1.9.25 to 2.2.0 in the main repo without the changes from the PR. From what I understand the cause of the error is a transitive dependency from the Kotlin Gradle plugin that introduces an older version on the build class path. So far I have tried to fix this by doing the following: 1. Exploring the config files for any hardcoded old Kotlin version, but nothing was found. 2. Commenting out the dependencies in the
build.gradle.kts
file of rewrite-kotlin
module
to prevent transitive leaks. Nothing changed. 3. Using the following command:
./gradlew :rewrite-kotlin:dependencies | grep -A 5 "kotlin-compiler-embeddable"
to check which version of
kotlin-compiler-embeddable
was Gradle resolving. The versions all appeared to match the set Kotlin language version. 4. Disabled classpath snapshots into
gradle.properties
by adding:
kotlin.incremental.useClasspathSnapshot=false
. Nothing changed. 5. Testing multiple Gradle/JDK combinations: Gradle versions between 8.14.3 & 9.0.0 with JDK 17, 21, 23, 24. Nothing changed. Does anyone have any pointers on what could be going wrong and how to fix it?
t
it looks like something forcing incosistent version of dependencies that KGP manages. Could you share a Gradle build scan?
m
Copy code
./gradlew :buildEnvironment

------------------------------------------------------------
Root project 'rewrite' - Eliminate tech-debt. Automatically.
------------------------------------------------------------

classpath
+--- org.openrewrite.build.root:org.openrewrite.build.root.gradle.plugin:latest.release -> 2.3.0
|    \--- org.openrewrite:rewrite-build-gradle-plugin:2.3.0
|         +--- org.openrewrite:rewrite-gradle:8.56.1
|         |    +--- org.openrewrite:rewrite-kotlin:8.56.1
|         |    |    +--- org.jetbrains:annotations:26.0.2 -> 13.0
|         |    |    +--- org.openrewrite:rewrite-java:8.56.1 (*)
|         |    |    +--- org.jetbrains.kotlin:kotlin-compiler-embeddable:1.9.25 -> 2.2.10
The rewrite-gradle-plugin pulls its own
kotlin-compiler-embeddable
, which might or might not be compatible with the one from KGP
👍 1
I'd recommend isolating the work of
rewrite-gradle-plugin
in isolated classloaders
t
@tapchicoma thank you! I tried to create a build scan, I am currently unable to, because I don't have a Develocity account. I will work on getting a build scan. @mbonnin thank you for the suggestion, I will try implementing it.
v
because I don't have a Develocity account
Isn't this about FOSS? Just create one on scans.gradle.org?
t
You're right, this is the build scan: https://scans.gradle.com/s/66fl7l6tzztd6
v
You apply the
org.openrewrite.build.root
plugin 2.3.0 which depends on
rewrite-kotlin
8.56.1 which depends on
kotlin-compiler-embeddable
1.9.25 I'd say and with that the newer KGP is not happy.
2
Regarding that warning about the embeddable compiler that is
m
There's actually code to make newer KGP happy here. But the net result of this code is that it makes
org.openrewrite.build.root
sad. Can't have them both happy at the same time 😄
Unless one (or both) of them isolates its work
t
@mbonnin could you try with
kotlin.compiler.runViaBuildToolsApi=true
in
gradle.properties
?
👀 1
m
I haven't actually been able to reproduce 1:1 OP's problem but I can try
Copy code
kotlin {
  compilerVersion.set(TODO())
}
Cools stuff, looks like it uncouples KGP and kotlinc versions?
t
nice spin 1
m
Yea, can't reproduce @Teodora Mihaila problem exactly. The PR currently assembles correctly:
Copy code
$ ./gradlew rewrite-kotlin:assemble
BUILD SUCCESSFUL in 15s
Tests fails for another toolchain related reason
@Teodora Mihaila any chance you can try with
kotlin.compiler.runViaBuildToolsApi=true
?
Side note but this is super cool 🤩
Copy code
Or you might want to update the version of the KGP but keep an older Kotlin compiler version.
Means I can get the configuration cache, project isolation, Gradle 9 updates from KGP while still targeting 2.1 for the non-JVM consumers 👀
t
note: for now it is only JVM compatible 🙂
👍 1
we are working on adding support for other platforms
👍 1
m
Alright! I was mainly interested in non-JVM for this feature so I'll wait a bit more before changing all my projects 🙂
t
@mbonnin Yes, it’s passing now. Right after I sent the message, a new commit was pushed that fixes the issue by forcing all Gradle configurations to use
kotlin-compiler-embeddable:2.2.0.
Thanks!
m
@Teodora Mihaila good if it's working 🥳It's still dangerous because
org.openrewrite.build.root
might rely on removed APIs. But you can worry about that when it's an actual problem