Vscode-java-debug: How to match `Use classpath of module` setting from Intellij IDEA?

Created on 16 Nov 2019  ·  15Comments  ·  Source: microsoft/vscode-java-debug

I'm attempting to migrate from Intellij IDEA to VSCode, and everything's gone mostly well to be honest except for one minor thing. The Use classpath of module setting in Intellij doesn't appear to directly map to projectName as implied here. Intellij allows me to specify a dot path to a particular folder in my app structure, while projectName restricts to only the base package name.

For example, here's my folder structure for a Spring Boot app:

app
└── src
    ├── main
    |   ├── java
    |   |   └── com.hello.world
    |   └── resources
    |       └── application.yml
    └── test
        ├── java
        |   └── com.hello.world
        └── resources
            └── application.yml

Note: app is only one of the many projects found in the root folder. Though it should be the only one relevant to this issue.

And here's my launch.json:

{
  "configurations": [
    {
      "type": "java",
      "request": "attach",
      "name": "Debug (Attach)",
      "hostName": "localhost",
      "port": 8083
    },
    {
      "type": "java",
      "request": "launch",
      "name": "Debug (Launch)",
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}",
      "mainClass": "com.hello.world.Application",
      "projectName": "app"
    }
  ]
}

The issue I'm having, if you haven't guessed already, is the Java Debugger appears to be searching for classes and settings from application.yml via the path app which includes classes and settings found in the test folder. I'd like it to only search the app/main path instead.

I accomplish this in Intellij by setting Use classpath of module to RootFolderName.app.main where RootFolderName maps to the top-level gradle project. I'm forced to set projectName only to app which breaks my application in some respects because it applies unwanted settings automatically from the test folder.

Also note that I am generally a beginner with respect to some terminology within the Java world, so if some explanations seem off that's why. I'm very keen to solve this issue! Would anyone be able to point me in the right direction from here?

Environment
  • Operating System: Mac OS 10.14.6
  • JDK version: 11.0.4
  • Visual Studio Code version: 1.40.1
  • Java extension version: 0.8.0
  • Java Debugger extension version: 0.23.0
bug

All 15 comments

The debugger will exclude the test folder automatically when calculating the classpath. Could you share your argfile?
image

@testforstephen Sorry, I can't include the full argfile. Here's a potentially relevant portion however:

-classpath "/Users/brandon/Projects/RootFolderName/app/bin/main:/Users/brandon/Projects/RootFolderName/app/bin/test

It looks like both the app/main and app/test folders are being included in the classpath. Is this possibly a bug? Or is there a way for me to specify some classpath exclusion for a particular directory?

@testforstephen I just figured it out. The autogenerated .classpath file in the app folder included the test directory like so:

<classpathentry kind="src" output="bin/test" path="src/test/java">
    <attributes>
        <attribute name="gradle_scope" value="test"/>
        <attribute name="gradle_used_by_scope" value="test"/>
        <attribute name="test" value="true"/>
    </attributes>
</classpathentry>
<classpathentry kind="src" output="bin/test" path="src/test/resources">
    <attributes>
        <attribute name="gradle_scope" value="test"/>
        <attribute name="gradle_used_by_scope" value="test"/>
        <attribute name="test" value="true"/>
    </attributes>
</classpathentry>

After removing these entries my app worked as expected. Is it expected behavior for vscode to autogenerate a classpath for test directories?

Note: not sure if this helps but we're using apply plugin: 'eclipse' to let gradle autogenerate classpaths during build (as I understand it). So perhaps I would have been better off not allowing vscode to autogenerate classpath files itself?

The debugger is supposed to exclude the class path marked as test scope automatically when running the normal program. Looks like a bug.

@testforstephen ah found it. Version 0.20.0 of the Debugger for Java extension introduced the issue it seems. I have no issues with version 0.19.0 of the extension. Thank you for your help!

yes, we changed the class path computing algorithm in 0.20.0, looks like it has some bug for gradle project.

same issue, I am wondering when it will be fixed? Thanks

Experiencing the same issue with version 0.25.1. I can not launch my Spring Boot application because the component scan always picks up the test classes.

classpath.txt

so how would you enable including the test classpath and its dependencies via launch.json?

so how would you enable including the test classpath and its dependencies via launch.json?

If you're running test class, the test scope will be included automatically. You don't need to manually enable it.

@testforstephen thanks for the answer. I am not running a test class, where it works out of the box. I am running the main application. However, I have a Spring Boot application which should run in a dev profile, which uses Testdata and some Mockservices rather than Production data and some Production Services, which are both located in the test classpath. Is there a way to specify that in a specific launch configuration the classpath should also contain test classpath?

At the moment, we didn't expose the capability to specify the classpath scope in launch.json. A workaround is to explicitly add all your class paths to "classPaths"/"modulePaths" of launch.json, but this requires yourself to find all class paths data.

@testforstephen if there isn't a gradle/maven classpath variable that can be extended somehow, the option to list all classpaths is not an option. I guess my option right now is to put another main class in a test package which in turn calls the original main method.

if there isn't a gradle/maven classpath variable that can be extended somehow, the option to list all classpaths is not an option.

@stefanrybacki This is an interesting idea. I'm considering to support appending user-specified "classPaths" in launch.json to the auto resolved classpath. Allowing predefined variables in "classPaths" property of launch.json could be a new way to customize the classpath. Would you mind opening a new feature request for this?

I guess my option right now is to put another main class in a test package which in turn calls the original main method.

Yes, this can be a workaround. If the main class is in a test folder, then the test scope would be added to the resolved classpath.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

srfrnk picture srfrnk  ·  5Comments

mentaloaf picture mentaloaf  ·  6Comments

erihanse picture erihanse  ·  3Comments

mojo2012 picture mojo2012  ·  5Comments

llgcode picture llgcode  ·  7Comments