google / ksp

Kotlin Symbol Processing API
https://github.com/google/ksp
Apache License 2.0
2.72k stars 254 forks source link

Generated files not accesible after upgrade to kotlin 2.0.0 #1940

Open ionspin opened 4 weeks ago

ionspin commented 4 weeks ago

Trying to migrate to Kotlin 2.0.0 and KSP 2.0.0-1.0.21, and was unsuccessful, as none of the generated files which are present in the build folder are visible to the source.

Reproducer:

Use 'examples/multiplatform' from this repository, change gradle.properties to use kotlin 2.0.0 and appropriate ksp:

diff --git a/examples/multiplatform/gradle.properties b/examples/multiplatform/gradle.properties
index 97b2b488..0049d2e1 100644
--- a/examples/multiplatform/gradle.properties
+++ b/examples/multiplatform/gradle.properties
@@ -1,3 +1,3 @@
 org.gradle.jvmargs=-Xmx2048M
-kotlinVersion=1.9.21
-kspVersion=1.9.21-1.0.15
+kotlinVersion=2.0.0
+kspVersion=2.0.0-1.0.21

Observed:

 ./gradlew workload:compileKotlinJvm

> Task :workload:kspKotlinJvm
w: [ksp] [Bar.kt, Baz.kt]
w: [ksp] [Bar.kt, Baz.kt, Foo.kt]

> Task :workload:compileKotlinJvm FAILED
e: file:///home/ionspin/tmp/ksp/ksp/examples/multiplatform/workload/src/commonMain/kotlin/com/example/Bar.kt:4:15 Unresolved reference 'Foo'.
e: file:///home/ionspin/tmp/ksp/ksp/examples/multiplatform/workload/src/commonMain/kotlin/com/example/Baz.kt:4:15 Unresolved reference 'Foo'.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':workload:compileKotlinJvm'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 695ms
5 actionable tasks: 2 executed, 3 up-to-date
ting-yuan commented 3 weeks ago

This is required by the KMP build model, which is enforced by K2: common sources have no visibility on platform / target sources. The KSP example needs to be updated by expect / actual.

ionspin commented 3 weeks ago

Thank you for your answer, but I am having trouble understanding the separation. Running ./gradlew workload:compileCommonMainKotlinMetadata

will generate a proper Foo.kt class:

cat ./workload/build/generated/ksp/metadata/commonMain/kotlin/Foo.kt
package com.example

class Foo {
    val bar = true
    val baz = true
}

and the task will succeed. Running ./gradlew workload:compileKotlinJvm will generate proper a proper Foo.kt as well:

cat ./workload/build/generated/ksp/jvm/jvmMain/kotlin/Foo.kt
package com.example

class Foo {
    val bar = true
    val baz = true
}

but the task will fail with the previously mentioned error.

I'm failing to see the separation, both common and jvm have their own generated classes present.

Also I would expect that generating a simple data class in common sources would be then available in all other source sets, even with K2. Please correct me if I am wrong.

Thanks again for all your hard work on the library, it is greatly appreciated!

toffeantyri commented 2 weeks ago

I have the same problem.

I use Kotlin 2.0.0 Ksp 2.0.0- 1.0.21 Room 2.7.0-alpha01

Ios target generate KClazz.instatiateImpl()

But it extension is not visible , i cant import it, for creation room factory builder

jamesrapadmi commented 1 week ago

I'm seeing the same issue, even though I have no code in a platform specific source set. I get the Unresolved reference error for codegen run from the commonMain files.

HySerge commented 1 week ago

这是来自QQ邮箱的假期自动回复邮件。   您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。