Scala / sbt: How to stop at breakpoints in Eclipse - eclipse

I am trying to debug my Scala application when running tests using sbt. I have added the following lines to build.sbt:
javaOptions in test ++= Seq(
"-Xdebug",
"-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8001"
)
I have also set breakpoints in Eclipse and configured a remote debugger for that port 8001.
When launching the tests from command line, the remote debugger is start correctly:
Listening for transport dt_socket at address: 8001
But then the execution of the tests does not suspend, as expected, and so I am not able to connect the remote debugger. Instead, the tests are simply run.
Any ideas what I might be doing wrong here? How can I debug the sbt tests using breakpoints in Eclipse?

The following worked for me (not tested in test, but probably work the same way). In this example, I used a project generated by udash-generator (0.3.1).
$ sbt
> set fork := true
> set javaOptions ++= Seq("-Xdebug", "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8001")
> run
I used eclipse (Neon with ScalaIDE 4.4.1, Scala 2.11) (sbt eclipse with-source=true) and added a breakpoint to the com.example.rpc.ExposedRpcInterfaces.hello(name: String) method.
Created a run configuration:
Started the launch configuration (the eclipse debugger) and opened browser with http://localhost:8080/#/rpc, started typing to the textfield and eclipse stopped at the breakpoint.
I am not sure what was different for you, as it seems you were also forking a new JVM for tests. I can try your setup if you provide more details which test framework you were using.

Related

how to debug scala sbt project in vs code

I am trying to debug sbt project in vs code. I have downloaded vs code extension name scala metals. How to explicitly add build.sbt folder path in scala metal.
How to explicitly add build.sbt folder path in scala metal
Open the root of your Scala project via VSCode, and Metals should detect your build.sbt and ask you if you want to load it. Otherwise you can do it explicitly from the Command Palette (Ctrl+Shift+P) and write "Metals Import Build"
how to debug scala sbt project in vs code
Not sure how to do it via metals. But the way I do it, is to open sbt myself from the terminal, via sbt -jvm-debug 5005. Then I attach VSCode remote debugger. You'll have to install "Debugger for Java" VSCode plugin too.
Here is the official documents: https://scalameta.org/metals/docs/editors/vscode.html,
See the "Running and debugging your code" sections.
But I found it's hard for me to debug the web project based on play framework.
So I use inntelij instead.
Start the project in sbt with: sbt --jvm-debug 5006
You can run it in the terminal:
After entered sbt, type "run" to start the proejct.
Add a new debug configuration, set port to: 5006
And then click "run->debug...", you can add the breakpoint.

How to specify "fork in Test := false" from command line rather than from build.sbt in a play framework based project

To keep the question as short as possible I just need to know if there's a way to set fork to false directly from the command line when launching activator instead of hard coding it as follows "fork in Test := false" from under build.sbt in a play-framework 2.4 based project.
__________________All below is for further context___________________
I'm maintaining a "legacy" project based on play framework version 2.4, and been trying to configure my IntelliJ environment to enable debugging with unit tests. To achieve this I created 2 run configurations, the first launches activator in debug mode and pipes all its output to IntelliJ console, the second attaches the debugger to the formerly launched jvm. I'm then free to run and debug any tests as I please from activator's prompt.
In order for the debugger to work though, I have to add the following to my build.sbt:
fork in Test := false
which allows for debugging with my IntelliJ project setup, since I have it properly configured to take my "conf/test-config.conf" into consideration when launching activator. But when I simply run "activator test" from a native command line console, with the fork option set to false, activator ignores my test-config.conf file (obviously with configurations tuned for unit tests) and instead loads the original app config file "application.conf" causing my tests to fail with all sorts of conflicts. This happens even with the below configuration in my build.sbt:
javaOptions in Test += "-Dconfig.file=conf/test-application.conf"
Commenting out "fork in Test := false" fixes the problem when I launch the tests from a native console (the unit tests run properly), my IntelliJ launch configuration also succeeds to run the tests but the problem is it loses its ability to debug the code, which is actually the whole point in the first place.
Here's the command I'm currently using from IntelliJ to launch activator in debug mode:
java -Dactivator.home=<activator-home-path> -Xms1024m -Xmx1024m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=256m -Dsbt.override.build.repos=true -Dconfig.file=conf/test-application.conf -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=9990 -jar <activator-jar-path>
Simply use the following command when using sbt:
sbt "set fork in Test := false" test
For activator, you can try the following:
Add this to your build.sbt:
fork in Test := Option(System.getProperty("fork")) match {
case Some("false") => false
case _ => true
}
And then sumply run the following command:
activator -Dfork=false test

unable to debug play framwork 2.x in Scala IDE

I have a project with java & scala classes. I want to debug this project in Scala IDE.
I run this command at my application end
./activator -jvm-debug 9999 run
And debug in Scala IDE as Remote Java Application with localhost & port 9999.
It did not stop at my breakpoints.
Any pointers on this.
In my build.sbt
fork in run := true
I changed it to false. Now its working.

SBT Broken Pipe

I've been avoiding using SBT since the support in intellij for maven has always been far superior, plus I don't see much advantage in SBT; but I figure why fight the masses.
So one of my open source projects I've converted over to SBT. Now when I run tests (approx 1000 test cases), I get OOMs. Ok so I've tried
fork in Test := true
javaOptions in Test ++= Seq("-Xmx2048m", "-XX:MaxPermSize=512m")
Ok so my OOMs go away but now I get
sbt.ForkMain$Run$RunAborted: java.net.SocketException: Broken pipe
at sbt.ForkMain$Run.write(ForkMain.java:114)
at sbt.ForkMain$Run$1.info(ForkMain.java:132)
Seems to be in different places each time.
These tests all pass if I'm building via maven (scala test maven plugin).
Help me Obi-wan or SBT lovers.
Edit: Adding env details
sbt 0.12.4
java 7.25
scala 2.10.2

How make Intellij Idea debug a Play 2.1 app's source code files, instead of project definition only

When I create a new Play 2.1 project, Intellij Idea insists on debugging the project configuration files only. That is, lines in ./project/Build.scala, and no other files.
How do I tell Idea to debug stuff in ./app/ instead?
I've generated Idea project files like so: [project-name] $ idea with-sources=yes (in Play's console).
(I cannot remember this ever happening before with Play 2.0 or earlier versions of 2.1)
There were 2 causes for this issue: one "interesting" and one stupid.
The interesting cause:
Play 2.1 and SBT seems to fork tests (and thus run them in a separate process). Therefore breakpoints in my unit test code were never hit.
One way to "fix" this, is to add this config settinig:
Keys.fork in Test := false
The stupid cause:
I had not yet compiled any "real" source code files in the new project. So therefore Idea never activated breakpoints on those lines. But the project configuration Build.scala file was compiled when I launched Play's SBT console I think, so Idea activated breakpoints in Build.scala directly. — So there was never a problem really: I just had to hit run in the Play console and then the breakpoints came alive (after Play had compiled my sources).
I'm running Intellij, with remote debug on port 9999
1. ~apatzer> play debug
2. Then start my Remote configuration in Intellij
3. [playproject] $ run
==> Successful at hitting breakpoints and debugging.
Because of forking, I was unable to hit breakpoints in any of my tests. For example:
[playproject] $ test-only com.mydomain.pkg.MyTest
==> FAIL different process. Breakpoints not hit in IDE.
Here's what worked:
import sbt._
import Keys._
import play.Project._
object MyBuild extends Build {
...
lazy val main = play.Project(appName, appVersion, appDependencies).settings(
Keys.fork in testOnly := false,
libraryDependencies ++= deps,
...