Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Kotlin Symbol Processing (KSP)

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)

like image 755
Max Farsikov Avatar asked Aug 02 '26 09:08

Max Farsikov


2 Answers

./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

like image 155
Kikiwa Avatar answered Aug 07 '26 00:08

Kikiwa


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:

  1. Make sure 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"
  1. Find the 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
  1. Then, attach the debugger to the process with the PID we just found.

  2. 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.

  3. 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

like image 28
Victor Choy Avatar answered Aug 07 '26 01:08

Victor Choy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!