spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
74.37k stars 40.51k forks source link

Allow ConfigurationProcessor to work with Kotlin Symbol Processor #28046

Open kkocel opened 2 years ago

kkocel commented 2 years ago

The problem: Currently, ConfigurationProcessor is backed by Annotation Processing. In Kotlin annotation processors can be used thanks to KAPT. Sadly KAPT does not work with JDK 16+ ( https://youtrack.jetbrains.com/issue/KT-45545 ). For Kotlin there are other processors like KSP ( https://github.com/google/ksp ), yet they do not rely on Java Annotation processing.

Proposed solution: Provide an abstraction layer on ConfigurationMetadataAnnotationProcessor that will allow plugging in other processors. Additionally provide an adapter for KSP.

@sdeleuze

wilkinsona commented 2 years ago

Thanks for raising this.

Sadly KAPT does not work with JDK 16+ ( https://youtrack.jetbrains.com/issue/KT-45545).

My reading of that Kotlin issue is that KAPT does work with JDK 16+, but that it requires adding some JVM arguments to open up some of the JVM's internals to KAPT. Have I misunderstood the discussion?

kkocel commented 2 years ago

@wilkinsona You understood it better than I :) Yes, it's possible to break javac encapsulation using add-opens flags, but ihmo it's temporary (this flag can be taken away as with --illegal-access=permit in JDK 17).

I know that adding an abstraction layer over processors can add in additional complexity, but it can also be a safer solution (also please note that KSP authors claim that it's up to 2x faster than java annotation processors).

As with everything you should weigh your options as Spring maintainers.

wilkinsona commented 2 years ago

The underlying problem with KAPT has now been fixed and will be part of the Kotlin 1.6 release. Rather than supporting a third-party alternative which, to me at least, has an unclear relationship with Kotlin and JetBrains, I think we should focus our efforts on working via KAPT. Thanks anyway for the suggestion.

bespaltovyj commented 2 years ago

Hi, @wilkinsona. The new Kotlin documentation says that kapt is in maintenance mode and new features are not planned. And recommend using ksp(it should work faster). image Will you support ksp after this news? If you don't mind, then I can start adding ksp support.

kkocel commented 2 years ago

@wilkinsona I asked about the relation of KSP and Kotlin: https://twitter.com/relizarov/status/1455452359669264384

So it looks like KSP is the way to go for Kotlin.

stillya commented 1 year ago

Any updates? Kapt is horrible annotation processor, it will be great if spring will support ksp. I have all my backend on kotlin and I have to use Kapt and main problem is not even speed(but it's slow), kapt shadows errors constantly, invalidate build cache.

hnljp commented 1 year ago

Just for your information. Google has created this abstraction layer over JavaAP and KSP. https://github.com/androidx/androidx/tree/androidx-main/room/room-compiler-processing https://mvnrepository.com/artifact/androidx.room/room-compiler-processing It was originally created for room, but they are now using it for different projects.

snv commented 10 months ago

When trying to update to Java 21, i run into an IllegalAccessError again

java.lang.IllegalAccessError: superclass access check failed: class org.jetbrains.kotlin.kapt3.base.javac.KaptJavaCompiler (in unnamed module @0x59db8216) cannot access class com.sun.tools.javac.main.JavaCompiler (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.main to unnamed module @0x59db8216

Since kapt keeps breaking every now and then, it would be really great if spring moved away from using it.

alwyn commented 9 months ago

I have been trying to use the usual workarounds on 21 with no success. If anyone has it working, please let me know.

ajgassner commented 9 months ago

I have been trying to use the usual workarounds on 21 with no success. If anyone has it working, please let me know.

The fix will be backported to Kotlin 1.9.21, see https://youtrack.jetbrains.com/issue/KT-60507 In the meantime you have to use JDK 20 or 17 LTS, you decide.

AlexeyTsvetkov commented 3 months ago

Slightly offtopic: there is a chance you don't actually need the configuration annotation processor at all. If you use Intellij IDEA Ultimate (I don't think that Community ever supported Spring configuration code insight), starting from 2023.2 Intellij does not rely on the metadata generated by the Spring boot configuration annotation processor. Other IDEs still might require it. I want to share this, because I was completely unaware of this until today (despite working at JetBrains), and removing KAPT improved our clean build times by 36%, incremental build times by up to 50%.

deejay1 commented 3 months ago

@AlexeyTsvetkov this still leaves the topic of the autoconfigure processor

AlexeyTsvetkov commented 3 months ago

@deejay1 oh, I did not know about the autoconfigure processor. Still, the info above could be useful for those who only use the ConfigurationMetadataAnnotationProcessor (org.springframework.boot:spring-boot-configuration-processor artifact).