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

Support use of environment variables in `vmArgs`

Open siddjain opened this issue 2 years ago • 2 comments

I am not able to use environment variables in vmArgs properties in launch.json

this does not work:

"vmArgs": "-javaagent:$PWD/src/main/resources/instrumentation-1.0-SNAPSHOT.jar -enableassertions"

I get error:

Error opening zip file or JAR manifest missing : $PWD/src/main/resources/instrumentation-1.0-SNAPSHOT.jar

this works:

"vmArgs": "-javaagent:/Users/me/java/tdbqdiff/src/main/resources/instrumentation-1.0-SNAPSHOT.jar -enableassertions"
Environment
  • Operating System: Mac OS Big Sur
  • JDK version: 11
  • Visual Studio Code version:

Version: 1.74.2 (Universal) Commit: e8a3071ea4344d9d48ef8a4df2c097372b0c5161 Date: 2022-12-20T10:26:09.430Z (3 days ago) Electron: 19.1.8 Chromium: 102.0.5005.167 Node.js: 16.14.2 V8: 10.2.154.15-electron.0 OS: Darwin x64 20.6.0 Sandboxed: No

  • Java extension version: 0.25.7 microsoft extension pack for Java
  • Java Debugger extension version:
Steps To Reproduce
  1. [step 1]
  2. [step 2]

[attach a sample project reproducing the error] attach logs

Current Result
Expected Result
Additional Informations

siddjain avatar Dec 23 '22 17:12 siddjain

Maybe I did not understand your request correct but I am using environment variables in vmArgs. The syntax is

${env:PWD}

see the answer here: https://stackoverflow.com/questions/72118677/set-vs-code-launch-json-to-use-windows-environment-variables

treimers avatar Mar 27 '24 13:03 treimers

I am experiencing the same behavior. Detailed information follows: VS Code on macOS Sonoma 14.5 (23F79)

Version: 1.91.0 (Universal) Commit: ea1445cc7016315d0f5728f8e8b12a45dc0a7286 Date: 2024-07-01T18:54:18.541Z Electron: 29.4.0 ElectronBuildId: 9728852 Chromium: 122.0.6261.156 Node.js: 20.9.0 V8: 12.2.281.27-electron.0 OS: Darwin x64 23.5.0

Using graalvm-jdk-22.0.1+8.1

The launch configuration is:

{
  "type": "java",
  "name": "HelloFX",
  "request": "launch",
  "env": {
      "HOST": "127.0.0.1",
      "PORT": 8080
  },
  "mainClass": "org.gmnz.playground.HelloFX",
  "projectName": "playground",
  "vmArgs": "--module-path ${env:HOST} ${env:PORT} --add-modules javafx.controls,javafx.fxml"
}

While the actual values that WOULD be passed to vmArgs do not make strictly sense, it was a test created to check for proper variable substitution in the final command line.

Running this configuration produces the following command line:

cd /Users/XX_REMOVED_XX/Documents/src/java/java-playground ; /usr/bin/env HOST=127.0.0.1 PORT=8080 /Library/Java/JavaVirtualMachines/graalvm-jdk-22.0.1+8.1/Contents/Home/bin/java @/var/folders/vg/5g50shk53s5c7g1204brqh800000gp/T/cp_52beafh940af1mkkpcenb3b7l.argfile org.gmnz.playground.HelloFX
Error: --module-path requires module path specification

As you can see the variables HOST and PORT are correctly set up in the command line, but looking into the argfile ...

❯ cat cp_52beafh940af1mkkpcenb3b7l.argfile
--module-path   --add-modules javafx.controls,javafx.fxml -XX:+ShowCodeDetailsInExceptionMessages -cp "/Users/smonotti/Documents/src/java/java-playground/target/classes:/Users/smonotti/.m2/repository/org/openjfx/javafx-controls/22.0.1/javafx-controls-22.0.1.jar:/Users/smonotti/.m2/repository/org/openjfx/javafx-controls/22.0.1/javafx-controls-22.0.1-mac.jar:/Users/smonotti/.m2/repository/org/openjfx/javafx-graphics/22.0.1/javafx-graphics-22.0.1.jar:/Users/smonotti/.m2/repository/org/openjfx/javafx-graphics/22.0.1/javafx-graphics-22.0.1-mac.jar:/Users/smonotti/.m2/repository/org/openjfx/javafx-base/22.0.1/javafx-base-22.0.1.jar:/Users/smonotti/.m2/repository/org/openjfx/javafx-base/22.0.1/javafx-base-22.0.1-mac.jar"%

... as you can see there are no values following the --module-path parameter. In this test I expected the values 127.0.0.1 and 8080 to show up. (once again, I do know that these values are semantically wrong for the --module-path parameter, I purposefully added them there to check for their correct substitution).

The same behavior is seen using the envFile attribute on the launch configuration.

ADDENDUM: as a workaround I do not use environment variables in this context, but rather hardcode the values I need directly on the value of the vmArgs attribute, and it's working.

smonotti-mwb avatar Jul 11 '24 14:07 smonotti-mwb

VS Code itself already supports the predefined variables such as cwd and ${env:Name}, see https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables.

I believe the supported environment variable replacement should already exist in your machine's enviroment variable list, not the env of launch.json. Since you have defined the env in launch.json manually, you can also specify it again in the vmArgs. We don't have plan to support this additional behavior at the moment.

testforstephen avatar Jul 15 '24 04:07 testforstephen