Snippet: turn on JVM debugging in sbt

| 1 minute | Comments

Remote debugging can be used to debug externally executed programs, useful to activate in sbt in order to keep using it while debugging with your favorite IDE. See for example IntelliJ IDEA’s documentation:

UPDATE: there is now a --jvm-debug <port> parameter to the sbt executable …

sbt --jvm-debug 5005

OLD WAY: — if the above is not suitable, you can do a manual config like this:

fork := true

javaOptions ++= {
  val Digits = "^(\\d+)$".r
  sys.env.get("JVM_DEBUG") match {
    case Some("true") =>
      Seq("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005")
    case Some(Digits(port)) =>
      Seq(s"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=$port")
    case _ =>
      Seq.empty
  }
}

And then set a JVM_DEBUG environment variable, before executing sbt:

JVM_DEBUG=5005 sbt
| Written by
Tags: sbt | Scala | Snippet