google / ksp

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

KSP plugin works fine when compiling with gradle but fails when compiling with Intellij IDEA #969

Open aduchate opened 2 years ago

aduchate commented 2 years ago

The very simple project here demonstrates the issue:

https://github.com/aduchate/kmap-test

When compiled using gradle, everything goes fine but when compiled using IntelliJ, the following error occurs

Kotlin: [Internal Error] java.lang.IllegalStateException: The provided plugin com.google.devtools.ksp.KotlinSymbolProcessingComponentRegistrar is not compatible with this version of compiler
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.registerExtensionsFromPlugins$cli(KotlinCoreEnvironment.kt:667)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$ProjectEnvironment.registerExtensionsFromPlugins(KotlinCoreEnvironment.kt:169)
...
  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.AbstractMethodError: Receiver class com.google.devtools.ksp.KotlinSymbolProcessingComponentRegistrar does not define or inherit an implementation of the resolved method abstract registerProjectComponents(Lcom/intellij/mock/MockProject;Lorg/jetbrains/kotlin/config/CompilerConfiguration;)V of interface org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar.
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.registerExtensionsFromPlugins$cli(KotlinCoreEnvironment.kt:659)
    ... 28 more

I'm using IntelliJ iDEA 2022.01, Kotlin 1.6.20 and KSP 1.6.21-1.0.5

I checked in the source code of KSP and registerProjectComponents has the right signature. Any idea why this issue might arise or any way to disable the plugin when the code is compiled by IntelliJ ?

Thanks in advance

neetopia commented 2 years ago

If Gradle is working but IDE is not working, it is possibly your IDE's gradle settings' issue, can you check settings in Settings/Preferences -> Build,Execution,Deployment -> Build Tools -> Gradle?

aduchate commented 2 years ago

That's the point, I'd like to be able to compile with IntelliJ and not using gradle. If I use gradle as the build system, it works fine. When I was using kapt, the behaviour was that kapt wasn't called at all when the build was made using IntelliJ native build system. I would love to be able to have the same behaviour using kpt.

tklinchik commented 2 years ago

See exactly the same issue, also tried 1.7.0-Beta (in Gradle and IntelliJ plugin) and still get the same

neetopia commented 2 years ago

This might be caused by incompatible compilers used in Gradle and cmdline mode. Can you try to replace the artifact to be using symbol-processing-cmdline?

tklinchik commented 2 years ago

Tried with symbol-processing-cmdline artifact but IntelliJ is still not happy.

tklinchik commented 2 years ago

Any more color on this issue? Hard to believe KSP is getting a ton of traction in a community without InteliJ support for it

Zaky7 commented 1 year ago

any update on this issue i am also getting this exception com.google.devtools.ksp.KotlinSymbolProcessingComponentRegistrar is not compatible with this version of compiler

tklinchik commented 1 year ago

Doesn't seem like there is a solution for this that I'm aware of. I'm forced to use Gradle executor in IntelliJ rather than IDEA as a workaround.

neetopia commented 1 year ago

please make sure the compiler version used by KSP is matching the compiler version used by your build tool (IntelliJ Gradle executor in your case).

tklinchik commented 1 year ago

That's what I have currently unless IntelliJ for some reason uses different internal compiler version which seems unlikely. I've gone through many versions of IntelliJ, gradle and kotlin and still see the same issue. Any way I can confirm your suspicion of compiler version difference? The error almost seems to imply classpath differences when running in IntelliJ but I can't figure out what's actually expected to be there.

neetopia commented 1 year ago

well let's take one step back, what do you mean by use Gradle executor in IntelliJ rather than IDEA?

tklinchik commented 1 year ago

In IntelliJ Preferences under "Build, Execution, Deployment" -> "Build Tools" -> "Gradle" there are few options under "Run tests using". If you chose "IntelliJ IDEA" then the issue will manifest, if you choose "Gradle" then the issue goes away when running tests.

neetopia commented 1 year ago

Well that's what I originally meant then. You should be choosing Gradle for running tasks if you are using Gradle as the build tools for the project. It is also mentioned in the same setting window that if you are running a Gradle project with IDEA runner, Gradle plugins might not work and you might get incorrect results.

ianbrandt commented 1 year ago

Perhaps this is more of an issue to be filed against IntelliJ, similar to https://youtrack.jetbrains.com/issue/KT-15040/Support-Kapt-in-IDEA-build-system-JPS.

be-hase commented 1 year ago

Also reproduced in ksp playground... https://github.com/google/ksp/tree/main/examples/playground