Outbreak Labs

I can do anything I want to. And so can you.

Debugging Scala code inside SBT inside Vagrant, using IntelliJ

For Scala, a common development environment is the combination of IntelliJ, Vagrant, and SBT. IntelliJ to edit Scala code, Vagrant to keep a consistent, reproducible, VM-based work area, and SBT to build and run Scala code. It isn't obvious how to use the debugger in IntelliJ against the code executing under SBT within the VM. Here's how you do it.

1. In your project's SBT build settings, locate the area in which options are passed to the JVM. This likely includes such options as "-Djava.library.path=lib".
2. To this section, add the following options:
"-Xdebug",
"-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005"
3. Rebuild and run your SBT project.
4. Open your project in IntelliJ, and go to Run> Edit Configurations


5. Click the plus icon in the upper left to add a new configuration, and select Remote

6. Name this configuration whatever you'd like (I called mine Vagrant), but enter your vagrant box's hostname or IP address where indicated

7. Press "OK". You should now see this new configuration selectable and debuggable in the upper right corner of the IDE.



If you've done everything correctly, IntelliJ should attach to your running Scala program.

Things that could've gone wrong:
  • If the Java options were not correctly applied, the JVM won't listen on port 5005 for the remote debugger connection. You should see a message in your console indicating that it's listening on that socket.
  • Your Vagrant instance might not be set up for networking, or to forward ports.
  • Your guest or host OS might have a firewall preventing the connection from going through

Good luck!