https://kotlinlang.org logo
#compiler
Title
# compiler
j

Justin Tullgren

09/21/2023, 1:48 PM
Hi, how do i attach a debugger to the kotlin compilation process to look into a compiler plugin? Attaching to the gradle process does not seem to do the trick, it hits my gradle plugin but the kotlin plugin doesn’t hit but it is being invoked.
n

Nick Chong

09/21/2023, 1:49 PM
replying to this thread as I am also curious bout this
d

dmitriy.novozhilov

09/21/2023, 1:51 PM
You can run the kotlin daemon with opened debug port using this spell:
Copy code
./gradlew -Dkotlin.daemon.jvm.options="-agentlib:jdwp=transport=dt_socket\\,server=n\\,suspend=n\\,address=5005" taskToRun
And to attach debugger you need to create and start the following run configuration int the IDE
j

Justin Tullgren

09/21/2023, 1:53 PM
do i need to start the gradle process in debug mode too?
d

dmitriy.novozhilov

09/21/2023, 1:53 PM
Note that this setup implies that debugger is the server, and kotlin daemon process is a client, so you need to start debugger before running the compilation If you want switch them you can change
server=n
to
server=y
in the spell and change debugger mode in the configuration to "Attach to remote JVM"
do i need to start the gradle process in debug mode too?
No, you don't
j

Justin Tullgren

09/21/2023, 1:54 PM
ok, i’l try it out! thanks
👌 1
d

dmitriy.novozhilov

09/21/2023, 1:55 PM
Also I prefer to restart the daemon after restarting debugging session, so usually I use this bash/zsh alias in everyday work
Copy code
alias gdwDebugS='./gradlew --stop && ./gradlew -Dkotlin.daemon.jvm.options="-agentlib:jdwp=transport=dt_socket\\,server=n\\,suspend=n\\,address=5005" '
j

Justin Tullgren

09/21/2023, 1:55 PM
should i just use no daemon?
d

dmitriy.novozhilov

09/21/2023, 1:57 PM
Yes, this way can be used to In this case you need to disable kotlin daemon with some gradle property (I don't remember which one exactly, sorry) and then just run gradle itself in debugger mode AFAIK it opens the 5005 port, so you need to use same configuration in "Attach to remote JVM" mode
j

Justin Tullgren

09/21/2023, 1:58 PM
ok thanks, i’m setting it up to give it a go. i’ll reach back out if i can’t figure it out. thanks so much
👍 1
Unfortunately it doesn’t seem to work
Screenshot 2023-09-21 at 07.09.25.png,Screenshot 2023-09-21 at 07.09.44.png
d

dmitriy.novozhilov

09/21/2023, 2:13 PM
Did you try to remove
--no-daemon
?
j

Justin Tullgren

09/21/2023, 2:13 PM
yes
and tried attaching to vm, switching the cli call to server=y
d

dmitriy.novozhilov

09/21/2023, 2:14 PM
There are two daemons (gradle daemon and kotlin daemon), and I'm not sure how it works if you disable the first one but not the second
Try
server=y
and
suspend=y
In this case kotlin daemon should suspend until you attach the debugger (or fail one minute later and fallback to no-daemon mode)
j

Justin Tullgren

09/21/2023, 2:15 PM
k trying
d

dmitriy.novozhilov

09/21/2023, 2:16 PM
If IDE shows that debugger attached successfully try to press "pause" button to check if it really works
j

Justin Tullgren

09/21/2023, 2:16 PM
Copy code
╰─➤  ./gradlew -Dkotlin.daemon.jvm.options="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" :thunderhead-apps:app-compose-ui:clean :thunderhead-apps:app-compose-ui:build
didn’t pause
just ran through
d

dmitriy.novozhilov

09/21/2023, 2:17 PM
Did you kill daemons first with
./gradlew --stop
?
j

Justin Tullgren

09/21/2023, 2:17 PM
i’ll do it again and make sure
w

wasyl

09/21/2023, 2:17 PM
I recently had success with debugging a Gradle task directly from IJ with setting
kotlin.compiler.execution.strategy=in-process
in
gradle.properties
and running the Gradle task from IJ with shift pressed (it switches to debugging;
execute gradle task
action, type task, shift + enter). I was able to successfully stop in an Anvil plugin this way
2
d

dmitriy.novozhilov

09/21/2023, 2:18 PM
@wasyl that was the flag I mentioned before, thank you
j

Justin Tullgren

09/21/2023, 2:19 PM
k i’ll give that a go
that did it with the remote server debug
thank you both!
🎉 2
2 Views