How to debug KSP using Idea and Gradle?
Running build task from IDE in debug mode does not attach IDE to the process.
Is there any command-line way to make the processor wait until IDE attach to thte process?
(I'm using KSP 1.5.31-1.0.0)
./gradlew :sample:build --no-daemon -Dorg.gradle.debug=true -Dkotlin.compiler.execution.strategy=in-process
If debugging without daemon is too slow (it has to start jvm everytime), ./gradlew -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=8765\,server=y\,suspend=n" ... and attach to the KotlinCompileDaemon process in the debugger.
More info here: https://github.com/google/ksp/issues/31
The real debug feature is supported.
Now I refer that here.
Debug A Processor and/or KSP
Because processors, KSP and Kotlin Compiler run in KotlinCompileDaemon, not Gradle daemon, the debug button in IDE, which usually debugs the Gradle process, doesn't work. The process to debug is KotlinCompileDaemon:
KotlinCompileDaemon is attachable / debuggable by specifying deubg options in kotlin.daemon.jvm.options. For example, when invoking Gradle build from command line:$ ./gradlew :app:kspDebugKotlin --rerun-tasks -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=8765\,server=y\,suspend=n"
KotlinCompileDaemon. There can be multiple instances in the system, especially when IDE is being used or there are other Gradle Kotlin projects that are being built. Look for the PID using this command:$ ps ax | grep 8765 | grep KotlinCompileDaemon
Then, attach the debugger to the process with the PID we just found.
Because of the suspend=n in the above example, the compilation will just keep running without waiting for the debugger. On the other hand, because the daemon will be alive for a while (3 hours by default), the process is now attached and we can set break points.
Run the above gradle build command again. The daemon should now stop at some break point, if they are hit.
If you can't find or attach to the KotlinCompileDaemon, try killing it first. Stopping Gradle daemon sometimes helps, too. For example
$ ./gradlew --stop; pkill -f KotlinCompileDaemon
https://github.com/google/ksp/blob/main/DEVELOPMENT.md#debug-a-processor-andor-ksp
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With