Scala – Debugging Scala code with simple-build-tool (sbt) and IntelliJ

intellij-ideajettysbtscala

What's the easiest way to debug Scala code managed by sbt using IntelliJ's built-in debugger? The documentation from "RunningSbt" from sbt's google code site lists commands for running the main class for a project or the tests, but there seem to be no commands for debugging.

Follow-up question: what's the easiest way to attach IntelliJ's debugger to Jetty when using sbt's jetty-run command?

Best Answer

There's a very convenient -jvm-debug flag in the official SBT packages for Mac, Linux & Windows. You can use the flag to specify the debug port:

sbt -jvm-debug 5005

Under the covers, this starts the JVM for SBT with the typical verbose debugging incantation:

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

You now can run your code as normal, for example with the sbt run command.

Configuring IntelliJ to connect to the running code...

Now you connect IntelliJ to your running process using a Remote Debug configuration. Note that the upper 3 fields in this form, while scary, are just for you to copy text out of, rather than into (they're giving the verbose debugging incantation specified above, which -jvm-debug already takes care of for you) - the only configuration you can change is in theSettings section halfway down:

Remote Debug configuration in IntelliJ

Related Topic