https://kotlinlang.org logo
Title
g

Gavin Ray

01/23/2023, 2:49 PM
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:
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)
}
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

tapchicoma

01/23/2023, 2:54 PM
JavaVersion.VERSION_21
was added in Gradle 7.6
g

Gavin Ray

01/23/2023, 3:39 PM
I'm using Gradle 8.0
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-rc-1-bin.zip
t

tapchicoma

01/23/2023, 3:41 PM
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

Gavin Ray

01/23/2023, 3:42 PM
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

tapchicoma

01/23/2023, 3:42 PM
it is only in this specific case - KGP does not check that JDK version is the same as provided one
g

Gavin Ray

01/23/2023, 3:42 PM
This is a great trick to know
Oh wait hmm, it still seems to fail with that 🤔
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

tapchicoma

01/23/2023, 3:45 PM
what is the failure?
g

Gavin Ray

01/23/2023, 3:45 PM
Same as before =/
[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
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

tapchicoma

01/23/2023, 3:48 PM
strange, it should pickup
JavaVersion.VERSION_19
for Jvm Target
do you have a repro?
g

Gavin Ray

01/23/2023, 3:51 PM
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

tapchicoma

01/23/2023, 4:12 PM
hm, I only see this error:
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

Gavin Ray

01/23/2023, 4:24 PM
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 🤔
java {
    toolchain{
        languageVersion.set(JavaLanguageVersion.of(21))
    }
}
t

tapchicoma

01/23/2023, 4:29 PM
yes, I've used the same JDK from SDKMAN as in example
g

Gavin Ray

01/23/2023, 4:30 PM
Does it compile with just that as well?
t

tapchicoma

01/23/2023, 4:30 PM
nope:
App.kt:12:26 Unresolved reference: MemorySession
and so on with the same error
g

Gavin Ray

01/23/2023, 4:30 PM
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 🙏