How do you attach the Scala Intellij debugger for tests? - scala

What steps are needed to configure the IntelliJ debugger to attach to scala Play tests managed by sbt?
In IntelliJ 2019, you can attach the debugger to the sbt run command using the debug button in the sbt shell with no configuration changes.
However, the debugger does not attach to the test command.

Consider attaching the debugger via IntelliJ's sbt shell:
In the sbt projects tool window select checkbox Use sbt shell for builds
Open sbt shell tool window (usually found at the bottom)
Click Attach debugger to sbt shell button (the icon looks like a bug)
In the status bar it should say Connected to the target VM (see the image bellow)
Set breakpoints in your test
Execute test command in the sbt shell tool window
Debug sbt shell window should open and execution should stop at the first breakpoint.
Note if you are using Play like OP, then by default Play runs tests in forked JVM, so for the above technique to work try setting Test / fork := false in build.sbt.

First, you need to run your SBT with -jvm-debug [port] like:
sbt -jvm-debug 5005
Then open dialog Run/Debug Configuration and click on Remote.
On the newly created configuration set up the correct host and port (usually localhost and 5005) and click on apply.
Then when you'll just need to run this configuration and set up breakpoints. After that just run test inside SBT console.

Related

How to add new SBT shell tab in IntelliJ IDEA?

I want to add one extra sbt shell in the IJ. How to do it?
I want to run some sbt commands inside a two different SBT Shells.
An easy solution is to run one SBT in the sbt shell and the second in a terminal. You can run a terminal inside of the IDE by using menu View | Tool Windows | Terminal - see Terminal in the IDEA Help. When you open a terminal, it is opened with a project directory as a current directory. You type sbt and you have another sbt running for your project.
This second SBT has some limitations compared to a SBT shell:
you cannot navigate to errors and warnings by clicking on them
the SBT will not reload when you press Reload SBT in the IDE
Still, depending on what you want, it might provide a working solution.

sbt console - tab completion

I'm using sbt 1.0.4 (latest as of this writing), and running sbt console brings me into the Scala REPL (project uses Scala 2.12.4). What is the keyboard command to invoke code completion? Tab does not work for me, it just adds a tab character and backspace is broken successively. I'm starting sbt from Bash in Linux.
Such that Indexe<key> -> IndexedSeq, for example.
There is a related question. So there is a bug that affects sbt console, but one can just run sbt and then from the sbt prompt enter console, and then Tab works as expected.

How do I breakpoint/debug a Scala project in IntelliJ?

I have a simple HelloWorld.scala file.
object Hello World {
def main(arg: Array[String]) = {
println("Hello World!")
}
}
Normally, I run sbt, compile, run in my terminal. After setting breakpoints in IntelliJ, how do I get the breakpoints to actually trigger?
I do it normally like this:
First, set breakpoints by clicking in the editor.
select main method in the code, wait until the light-bulp shows up. In Its menu, you can choose "run".
This automatically creates a "run-configuration" for this class. After that, you should see the name of your Object in the upper-right select manue ("run configuration"), you can then just click the "bug"icon.
Intellij will allow you to setup a run configuration from within the IDE. Assuming you've imported the SBT project, right clicking on the main method/object should have a "debug" option in the context menu which would run the application from inside the IDE and that execution would stop at the breakpoints.
Alternatively, if you are tied to the SBT run, you can also try creating a run configuration for the SBT targets and debug that.
As an aside, this is the same process as for a Java application (or any other supported by the IDE)

How to repeatedly run Wicket/Maven/Jetty project in Eclipse?

Thanks to those info http://www.ralfebert.de/blog/wicket/wicket_eclipse_setup/ and How can I begin using Wicket? now I am able to edit Java code and html files with Eclipse, and run it to get Jetty started.
However there are still two problems which do not allow me to repeatedly run the project:
I have to stop previous run in order to run it again
unlike regular Java project, where I could simply choose "run", now I have to go into "Run Configuration", select Maven Build type, and below select my project to run, and then finally click "run"
ad.1. How to make Eclipse stop the previous run on next run -- i.e. I would like to just run project, and Eclipse should either run it, or stop previous one and run it again
ad.2. how to "bind" Run/Run to running my configuration (Maven Build / my project)?
Thank you in advance for help.
Another simply option is to use a Start class with a main that runs and embedded jetty. Then you will be able to run and debug your application as a normal desktop java application with your IDE.
Check the example start file that comes with the wicket quickstart here:
https://github.com/jordeu/wicket/blob/master/archetypes/quickstart/src/main/resources/archetype-resources/src/test/java/Start.java

Debug Playframework in Eclipse

I just moved from Netbeans to Eclipse. In Netbeans, I can debug Playframework applications out of the box. However, in Eclipse, it seems hard to configure this in order to debug a web app (a Playframework app in specific).
As in Play! documentation,
The main launcher in only usable with the Run As eclipse command. You
can then use the “Connect JPDA launcher” using “Debug As” to start a
debugging session at any time. Stopping the debugging session will not
stop the server.
However, I can not see “Connect JPDA launcher” in "Debug As" (I'm using Eclipse Classic 3.7.0).
I've searched about JPDA (I'm new to Java and don't know about "main launcher" and JPDA, then config Remote Debug as instruct in
http://javarevisited.blogspot.com/2011/02/how-to-setup-remote-debugging-in.html
But it's returning an error:
Failed to connect to remote VM. Connection refused. Connection
refused: connect
I've searched and found some suggestions like:
1.)editing catalina.bat and adding the lines : set
JPDA_TRANSPORT=dt_socket ... set JPDA_ADDRESS=8000 AND
2.)Editing startup.bat call "%EXECUTABLE%" jpda start %CMD_LINE_ARGS%
Actually I don't clearly understand this and I guess it's for configuring Tomcat. However, I'm using Play! default server, checked the application.conf and sure that it's listening to port 9000 and 8000 for JPDA.
Of course, I have started the Play app before debugging, I can access the app via port 9000 normally. I'm using Win 7 with no admin rights.
Do you have a detailed guide on debugging Play! in Eclipse?
As of Play 2.0, the eclipse folder and launchers are no longer generated when you run play eclipsify. After running play eclipsify, you can debug your project in Eclipse as follows:
In a console outside of eclipse, run play debug run.
In Eclipse, right-click your project, then choose Debug As -> Debug Configurations...
Right-click Remote Java Application, then click New.
The host should already be set to localhost. Set the port to 9999 (the default port used by the play debug run command).
Click Apply to save, then Debug to connect to your running Play instance.
First of all, you have created the configuration files for Eclipse by using the play eclipsify YourProject command, right? (If not, see Play framework documentation for more information.)
After that, you should have a folder named eclipse in your project. In it, you should see commands Connect JDPA to YourProject.launch, YourProject.launch and Test YourProject.launch.
Right-click on the YourProject.launch and choose Run As --> YourProject from the pop-up menu. That will start your application. When the application is running, right-click on the Connect JDPA to YourProject.launch, and choose Debug As --> Connect JDPA to YourProject.launch from the pop-up menu. That will start a debug session on your application.
Play 2.1-RC2 : Running Play! app that is synced with Eclipse
To make the project Eclipse compatible, got he project folder and then give command
">play eclipse". Now you can add it as an existing project form eclipse.
Then to run it you have to first run start play in the project folder i.e. command "play"
This will activate play server and identify the app in the project i.e. [appname]$
now you have to just give the command "run" to see your app running on the localhost:9000 or whichever port config you have specified.
Debug mode: To run this similarly you need first give command "play" then [appname]$ "play debug run" this will by default use port 9999 so when you go to localhost:9000 in your browser and in your eclipse file -> Debug As -> Debug configuration.. -> New Remote Java Application -> connect specify port 9999 and debug
With activator, you'll need activator -jvm-debug <port number> ~run
I guess most of you are running the play framework with Typesafe Activator. If you are doing so, This may help you,
activator -jvm-debug ~run
Like : activator -jvm-debug 9999 ~run
(then It will start the debugger in 9999 port along with the app running in 9000 port.)
Now go to eclipse and right click on the project name you want to debug and select Debug As -> Debug configuration.. -> (select)Remote Java Application -> new (top left an icon with tiny plus sign) -> connect specify port 9999 and debug.
As of Play 2.1.2, I was getting the 'address in use' error when running play debug run.
The fix was to edit play.bat in play-2.1.2 directory and change these two lines:
:setDebug
set JPDA_PORT=9999
to
:setDebug
set JPDA_PORT=8551
Then in Eclipse, select Debug Configurations, find "Remote Java Application", do New. Then set port to 8551. Now you can start play using
play debug run
And then in Eclipse start debugging using the debug configuration you created about.
If you run "play eclipsify" on the project folder, it will create two run configurations.
Looking at the run configuration details, you should check if the following line (or something similar) is in the "VM arguments":
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
-Dplay.debug=yes
-Dplay.id=test -Dapplication.path="${project_loc:NMKServer}"
-Djava.endorsed.dirs="c:\Java\play-1.2.2/framework/endorsed"
-javaagent:"c:\Java\play-1.2.2/framework/play-1.2.2.jar"
Here you can configure the debugging port. With this config, I can attach to process using the instructuctions you have.