google / ksp

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

KSP not picking up generated code from sqldelight #1912

Open aftabahmadTW opened 4 months ago

aftabahmadTW commented 4 months ago

Description

When using hilt with KSP and sqldelight, the build fails due to NonExistentClass as hilt is not able to find the sqldelight generated code. I've attached a small reproducer to highlight this issue. If you use kapt it works fine, the issue is only present in KSP.

Reproducer

DaggerKSP.zip

Environment

Studio Build: Android Studio Koala | 2024.1.1 Beta 2 Version of Gradle Plugin: 8.5.0-beta02 Version of Gradle: 8.7 Version of Java: javac 17.0.10 OS: Mac OS 13.6

bvella commented 4 months ago

I have a similar issue with micronaut annotation processor. When a class is generated in a folder that is added to the kotlin source set, it seems that ksp ignores it and micronaut test fails because the type of the injected bean becomes Object instead of the actual type. However, if the same class is generated in to source folder it works fine. This used to work with kapt, it is failing now in my exercise to upgrade to kotlin 2 and ksp.

This is a link to a github repo that reproduces it: https://github.com/bvella/ksp-generated-code-issue

there are 2 modules:

If you go to ksp-generated-code-issue/plugin/src/main/groovy/plugin.groovy and replace line 18 with line 19 it works. these 2 lines determine whether the plugin generates the code in build/generated/sources/pp/kotlin or src/main/kotlin

Version of Java: 21.0.3 OS: Linux 6.1.90-1-MANJARO

bvella commented 4 months ago

For the benefit or anyone encountering a similar issue, the problem was that the source set was being appended in the afterEvaluate block whereas the ksp task seems to harvest the source set on configuration rather than when it is executed. In my case, the effect was that the generated folder was added in the source set after the ksp configuration read the source set. moving the source set addition outside of the afterEvaluate block solved my problem.

ting-yuan commented 4 months ago

There doesn't seem to be any standards in Gradle about Kotlin source code generation and registration. In general, Gradle plugins need to cooperate with each other explicitly in order to achieve the desired behavior in the above comments. From KSP or any Gradle plugins' (except Kotlin Gradle Plugin, probably) point of view, the best they can guarantee is probably making sure NOT interacting with other plugins.