bazel-vscode-java icon indicating copy to clipboard operation
bazel-vscode-java copied to clipboard

How to Debug Java JUnit Tests?

Open vorburger opened this issue 2 years ago • 7 comments

Similar to #80 but filed separately to focus on debugging (not running) of Bazel java_test instead of java_binary, and (to me) a much more important issue than that one:

Using my https://github.com/enola-dev/enola I have never managed to debug code using this VSC extension.

I keep working around it by adding temporary lines in tests to throw exceptions 😭 to inspect values, which is really quite dumb (of me)... 🤣

I'm guessing the JDT in VSC doesn't have any working "launch configurations", but I'm hoping that it can launch Bazel and remote attach?

I'm not quite sure if I'm just stupid and don't dig where / how to click? Or is this a feature gap and known to be not really working as-is right now?

@guw does this actually work for you?

vorburger avatar Apr 14 '24 21:04 vorburger

BTW: I was never entirely sure what "for the Bazel Run Targets we rely on importing IJ run configurations" (here) means... surely not that one needs to install IJ to create a ~~Launch & Run~~ Debug in VSC? 🎣

@lonhutt

vorburger avatar Apr 14 '24 21:04 vorburger

@vorburger Do you have the Java test extension installed in VS Code?

guw avatar Apr 14 '24 21:04 guw

@guw I do have this (from MSFT) but not this (which looks old), and the debugger.

I do have that green double arrow in the side bar. Clicking it opens a Test Results briefly.

But in Debug Console there is a Class not found dev.enola.common.io.resource.ResourceProvidersTest. Where's the equivalent of this for tests to see what it actually launched?

This Guide doesn't look particularly helpful.

I take it from your comment that it works for you?

vorburger avatar Apr 14 '24 23:04 vorburger

OMG 😮 I just discovered https://github.com/bazelbuild/bazel/issues/312 and was e.g. able to:

$  b test --java_debug //common/common:src/test/java/dev/enola/common/io/resource/ResourceProvidersTest

and then modify "port": "5005" in that .vscode/launch.json (discovered here) and with that it looks like I can debug - whoa!

Is there any easier way?

vorburger avatar Apr 14 '24 23:04 vorburger

I also seem to need to manually set e.g. "projectName": "common.common:common" to be able to see Variables and Watch.

vorburger avatar Apr 14 '24 23:04 vorburger

BTW: I was never entirely sure what "for the Bazel Run Targets we rely on importing IJ run configurations" (https://github.com/salesforce/bazel-vscode-java/issues/80#issuecomment-1879725172) means... surely not that one needs to install IJ to create a Launch & Run Debug in VSC? 🎣

https://github.com/salesforce/bazel-eclipse/blob/main/docs/common/projectviews.md#test_sources ?

vorburger avatar Apr 15 '24 14:04 vorburger

We are using this as well and for our projects it mostly "just" works. It looks like with your setup VS Code is not getting the full classpath.

guw avatar Apr 15 '24 14:04 guw