Hi everyone, does anyone know how to debug Kotlin ...
# compiler
c
Hi everyone, does anyone know how to debug Kotlin compiler plugins after setting breakpoints? When I run
debug autoservice
, the plugin registered by this plugin is printed, but I can't debug it with breakpoints. The implementation of
KotlinCompilerPluginSupportPlugin
allows for breakpoint debugging, but I don't understand why.
p
In gradle.properties file add line
Copy code
kotlin.compiler.execution.strategy=in-process
In IDE run configuration specify 'Debug gradle scripts' (Modify options -> Gradle -> Debug gradle scripts) Be aware that incremental compilation may not call plugin code if decide that nothing have been changed since previous compilation.
d
Another option is opening the debug port in the kotlin compiler daemon and attaching debugger to it:
Copy code
./gradlew -Dkotlin.daemon.jvm.options="-agentlib:jdwp=transport=dt_socket\\,server=n\\,suspend=y\\,address=5005" your-task-to-run
Don't forget to kill existing daemons first.
c
@PHondogo @dmitriy.novozhilov
"I have attempted to implement the debugging solutions provided, but I am encountering a specific issue. I have configured a 'Remote JVM Debug' profile in my IDE to listen on port 8080. To trigger the debugger, I am using the following command:
./gradlew -Dorg.gradle.jvmargs="-Xmx3072M -Dfile.encoding=UTF-8 -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:8080" composeApp:compileKotlinJs --rerun-tasks
Using this command, I can successfully hit breakpoints within my Gradle plugin (
ZiplinePlugin.kt
). However, breakpoints within my IR transformation logic (
AdapterGenerator.kt
) are never hit. Furthermore, I observed that after executing the command, the build process hangs indefinitely, which seems to be related to the
suspend=y
flag. I compared this to an existing project where debugging a test function seems to directly attach to and debug the IR compilation phase. When I try to replicate that behavior, my build also hangs in the same way as my command-line attempt. Could you please explain why the Gradle plugin breakpoints are hit but the IR breakpoints are not
effect.mp4
d
With
-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:8080"
you've made the gradle worker to attach the debugger. But the compilation is actually happens inside the separate process, the Kotlin daemon. And to attach the debugger to it, you need to pass
-Dkotlin.daemon.jvm.options="-agentlib:jdwp=transport=dt_socket\\,server=n\\,suspend=y\\,address=8080"
argument instead. The
-Dkotlin.daemon.jvm.options
specifies the jvmargs which would be passed to the kotlin daemon process when gradle starts it.
c
@dmitriy.novozhilov
./gradlew -Dkotlin.daemon.jvm.options="-agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:8080" composeApp:compileKotlinJs --rerun-tasks
Okay, I tried the modified command, but the breakpoint still hasn't been executed. Even if a breakpoint was added to the implementation of KotlinCompilerPluginSupportPlugin, it wouldn't be executed. However, if we replace this daemon process with org.gradle.jvmargs, the breakpoint of the implementation of
KotlinCompilerPluginSupportPlugin
will be executed. But if a breakpoint is added to the code of the following class, it won't be executed. The log shows that it was indeed executed.
Copy code
@AutoService(CompilerPluginRegistrar::class)
class ServiceCompilerPluginRegistrar : CompilerPluginRegistrar()
I'll skip this question. I'll give up starting with debugging for IR Anyway, thank you for your reply
d
It's very strange. The only guess I have left is that you might have the previous kotlin daemon alive (without debug option). So I usually stop all daemons first with
./gradlew --stop
before running the command with debugger. As another option I could recommend you using the compiler test framework to setup regular tests for your plugin, which will be debuggable without any additional arguments, just right from IDE. You can check how to setup these tests in the official compiler-plugin-template.
c
Actually, I execute it before debugging every time /gradlew -stop, This question is indeed very strange, and regarding the template project you mentioned, I will try it out
d
My final idea is to try disabling the kotlin daemon completely and debug the gradle daemon:
Copy code
# will try to attach to port 5005
./gradlew --no-daemon -Dorg.gradle.debug=true -Dorg.gradle.debug.server=false
The
--no-daemon
is important there. The debug arguments probably could be replaced with
-agentlib=...
, as you did before.
FTR I'm copying all these options from my own bash aliases, which I use on a regular basis. So I'm running of ideas here.