I am trying to use a preview JDK in my Kotlin compiler config, but I am getting a strange error. Has...
g
I am trying to use a preview JDK in my Kotlin compiler config, but I am getting a strange error. Has anyone seen this before:
Copy code
val JDK_21_PATH = "/home/user/.sdkman/candidates/java/21.ea.6-open"

tasks.withType<org.jetbrains.kotlin.gradle.tasks.UsesKotlinJavaToolchain>().configureEach {
    kotlinJavaToolchain.jdk.use(JDK_21_PATH, JavaVersion.VERSION_21)
}
Copy code
Execution failed for task ':app:compileKotlin'.
> Error while evaluating property 'compilerOptions.jvmTarget' of task ':app:compileKotlin'.
   > Failed to calculate the value of property 'jvmTarget'.
      > Unknown Kotlin JVM target: 21
Specifically, I am trying to build around the changes present in the
Foreign Function & Memory API
since it's significantly changed since JDK19 LTS
(Otherwise I would just use JDK19)
t
JavaVersion.VERSION_21
was added in Gradle 7.6
g
I'm using Gradle 8.0
Copy code
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-rc-1-bin.zip
t
sorry, it is about Kotlin JVM Target... Kotlin does not yet have jvm target 21 in it. Probably, better for now to use
JavaTarget.VERSION_19
as task input but still point to JDK 21 location
g
Ohh, I didn't know you could do that 🤦
Ty, that makes sense, since the bytecode version does not need to be 21, just the JDK API right?
t
it is only in this specific case - KGP does not check that JDK version is the same as provided one
g
This is a great trick to know
Oh wait hmm, it still seems to fail with that 🤔
Copy code
val JDK_21_PATH = "/home/user/.sdkman/candidates/java/21.ea.6-open"

tasks.withType<org.jetbrains.kotlin.gradle.tasks.UsesKotlinJavaToolchain>().configureEach {
    kotlinJavaToolchain.jdk.use(JDK_21_PATH, JavaVersion.VERSION_19)
}
I think maybe you just cannot use preview JDK's with kotlin + gradle?
(I think Maven too, I remember trying once)
t
what is the failure?
g
Same as before =/
Copy code
[user@MSI bustub-kotlin]$ ./gradlew compileKotlin                                                                                                            
> Task :app:compileKotlin FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileKotlin'.
> Error while evaluating property 'compilerOptions.jvmTarget' of task ':app:compileKotlin'.
   > Failed to calculate the value of property 'jvmTarget'.
      > Unknown Kotlin JVM target: 21
Copy code
Caused by: java.lang.IllegalArgumentException: Unknown Kotlin JVM target: 21
        at org.jetbrains.kotlin.gradle.dsl.JvmTarget$Companion.fromTarget(JvmTarget.kt:26)
        at org.jetbrains.kotlin.gradle.tasks.DefaultKotlinJavaToolchain$wireJvmTargetToToolchain$1$1.invoke(DefaultKotlinJavaToolchain.kt:78)
        at org.jetbrains.kotlin.gradle.tasks.DefaultKotlinJavaToolchain$wireJvmTargetToToolchain$1$1.invoke(DefaultKotlinJavaToolchain.kt:70)
        at org.jetbrains.kotlin.gradle.tasks.DefaultKotlinJavaToolchain$sam$org_gradle_api_Transformer$0.transform(DefaultKotlinJavaToolchain.kt)
t
strange, it should pickup
JavaVersion.VERSION_19
for Jvm Target
do you have a repro?
g
Sure, I can make a repro, one moment
bustub-kotlin.zip
(You will have to install Java 21ea build 6 with
sdk install java 21.ea.6-open
and replace the path constant)
t
hm, I only see this error:
Copy code
Execution failed for task ':app:compileKotlin'.
> 'compileJava' task (current target is 11) and 'compileKotlin' task (current target is 19) jvm target compatibility should be set to the same Java version.
  Consider using JVM toolchain: <https://kotl.in/gradle/jvm/toolchain>
but it is possible to disable via
kotlin.jvm.target.validation.mode=ignore
after it I see just a normal compilation errors
g
That's so strange, something must be up on my system
Thanks for looking into it
Out of curiosity, does setting the JDK toolchain to Java 21 also work for you 🤔
Copy code
java {
    toolchain{
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}
t
yes, I've used the same JDK from SDKMAN as in example
g
Does it compile with just that as well?
t
nope:
Copy code
App.kt:12:26 Unresolved reference: MemorySession
and so on with the same error
g
ahh okay, so that means it does work (those were removed)
since it's not gradle error
man something is VERY broke with my gradle
maybe something strange with Gradle Windows/WSL2
Thank you so much for looking into it, I very much appreciate your time 🙏
6797 Views