Hi, how do i attach a debugger to the kotlin compi...
# compiler
j
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
replying to this thread as I am also curious bout this
d
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
do i need to start the gradle process in debug mode too?
d
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
ok, i’l try it out! thanks
👌 1
d
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
should i just use no daemon?
d
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
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
Did you try to remove
--no-daemon
?
j
yes
and tried attaching to vm, switching the cli call to server=y
d
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
k trying
d
If IDE shows that debugger attached successfully try to press "pause" button to check if it really works
j
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
Did you kill daemons first with
./gradlew --stop
?
j
i’ll do it again and make sure
w
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
@wasyl that was the flag I mentioned before, thank you
j
k i’ll give that a go
that did it with the remote server debug
thank you both!
🎉 2
d
@wasyl and how did you manage to stop in the plug in? I'd like to stop in the Compose plug in
j
You need this
kotlin.compiler.execution.strategy=in-process
in
gradle.properties
and then attach your source project as a remote jvm on the default port after starting the gradle build
gradle will start up, you attach with the remove jvm debugger, and then it starts going. as long as you have the above flag it should step into your source. as for stepping into the compose compiler from google android that might be a tad different. but for customer compiler plugins that works
d
thanks a lot, i will try. Maybe will work for Compose compiler too.
Only success is with being able to step into the aapt2 code called by AGP. Cannot step into compose compiler, or any other, like hilt
w
I think KSP should work with the in-process strategy too 🤔 and btw you can easily run build in debug mode attached to IJ by opening
execute gradle task
action, typing a task and running it with
shift
pressed — the title window will change to
debug
and IJ will run Gradle task and attach to Gradle daemon right away
d
but what about Compose? real goal is step into Compose compiler
j
You’ll need to get the source from the google repo and do the remote debugging from that project. They have a slightly complex set up though.
d
argh, slightly complex is a bit of an understatement! 🙂 thanks!
😂 1
226 Views