Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing environment variables to debugger: 'env' or 'environment'? #22

Closed
nebjamin99 opened this issue Apr 20, 2016 · 26 comments
Closed

Comments

@nebjamin99
Copy link

The documentation and examples for launch.json show the use of an object list (key/value pairs in {}) called 'env' for passing environment variables to a debugging session.

But on my version which I installed yesterday, it's an array ([]) called environment. Hovering over it to get Intellisense gives a description that corresponds with above.

How can I pass environment variables to the debugger?

@delmyers
Copy link
Contributor

What OS are you using?

We could be clearer in the examples. The key/value pairs should go inside the array. So, something like this:

[{"name":"value", "name2":"value2"}]

Also, it looks like setting environment variables isn't working on mac.

@nebjamin99
Copy link
Author

nebjamin99 commented Apr 20, 2016

Ubuntu 16.04 beta 2. Thanks for clarifying. That sorted it! Works great!

@nebjamin99
Copy link
Author

I reopened this issue because I was mistaken when I said that sorted it.

When specifying anything in the 'environment' tag vscode reports the following error:

error while processing request 'launch' (exception: Required property 'Value' not found in JSON. Path 'environment[0]', line 15, position 6.)

My debug launch script looks like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",
            "type": "cppdbg",
            "request": "launch",
            "launchOptionType": "Local",
            "miDebuggerPath": "/usr/bin/gdb",
            "targetArchitecture": "x64",
            "program": "/home/ben/git/server/core/build/debug/src/libcored/vca-cored-cxx-unit-tests",
            "args": [],
            "stopAtEntry": false,
            "cwd": "/home/ben/git/tool-chain/rootfs/host/ubuntu/16.04/opt/company/lib64",
            "environment": [{"name":"value"}],
        },
        {
            "name": "C++ Attach (GDB)",
            "type": "cppdbg",
            "request": "launch",
            "launchOptionType": "Local",
            "miDebuggerPath": "/usr/bin/gdb",
            "targetArchitecture": "x64",
            "program": "enter program name, for example ${workspaceRoot}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceRoot}",
            "environment": [],
            "processId": "enter program's process ID"
        }
    ]
}

If I remove the contents of environment it loads the process fine (I just can't debug it as I need to pass LD_LIBRARY_PATH for some shared lib dependencies).

@delmyers delmyers added bug and removed question labels May 10, 2016
@krworks
Copy link

krworks commented May 26, 2016

The correct format for this environment object is:

"environment":[{"name":"name_of_var", "value":"value_of_var"}, {"name":"var2", "value": "var2_value"}]

@pieandcakes
Copy link
Contributor

Closing as it looks like @krworks comment solves the problem

@NewProggie
Copy link

NewProggie commented Aug 31, 2016

Hey,

I have the same error on OS X (running 10.11.6), running Code Version 1.4.0. Even though I'm passing environemnt variables to the debugger (in launch.json) with

"environment": [
    {
        "name": "DYLD_FRAMEWORK_PATH",
        "value": "/my/path/here"
    },
    {
        "name": "A_VARIABLE",
        "value": "/other/stuff/here"
    }
]

it seems that the executable is run without those variable set beforehand.

Edit: My bad, it seems that in OS X lldb is started without any environment variables set, due to system protection. However, is there any way, using Visual Studio Code, to pass specific commands to the debugger, once it has been started?

For instance, on the command line I would do:

$ lldb my_executable
(lldb) env DYLD_LIBRARY_PATH=/my/foo/bar
(lldb) r

How would I achieve this, using Visual Studio Code?

Thanks in advance

@dhiraj113
Copy link

This issue still seems to exist. I am unable to set LD_LIBRARY_PATH environment variable in Ubuntu 16.04.

@pieandcakes
Copy link
Contributor

@nebjamin99 You can pass additional commands in the launch.json using "setupCommands". You can find more info at: https://github.com/Microsoft/vscode-cpptools/blob/master/launch.md

@dhiraj113 I noticed you opened another issue, #616. Please update that issue with the information requested.

@codermannn
Copy link

The correct format for this environment object is:

"environment":[{"name":"name_of_var", "value":"value_of_var"}, {"name":"var2", "value": "var2_value"}]

This doesnt seem to work on mac. Property environment is not allowed.

@pieandcakes
Copy link
Contributor

@lohiarahul Can you confirm that your type field in your launch.json is showing "type": "cppdbg" ? According to the package.json schema, there doesn't seem to be a restriction for Mac and I can't duplicate the error.

@codermannn
Copy link

't seem to be a restriction for Mac and I can't duplicate the error.

The debugging type is different. Its node. I guess it might have a different format.

@pieandcakes
Copy link
Contributor

@lohiarahul Yea, This extension provides the types of cppdbg and cppvsdbg. You probably need to find the issues page for the node extension.

@dcsan
Copy link

dcsan commented Apr 23, 2019

anyone who's looking for a node example, this worked for me.

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  // APPNAME=rap2 PORT=31000 DEBUG=*,-engine*,-socket.io*,-snapdragon*,-send,-express*,-nodemon*,-superagent,-AmpMetrics,-body-parser* nodemon server.js

  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "cwd": "${workspaceFolder}/server",
      "program": "${workspaceFolder}/server/server.js",
      "env":
        {
          "APPNAME": "rap2",
          "PORT": "31000"
        }
    }
  ]
}

it's not very well documented here:
https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

@dcsan
Copy link

dcsan commented Apr 23, 2019

also useful for node folks
https://techbrij.com/visual-studio-code-tasks-debugging

@abser
Copy link

abser commented May 8, 2019

  "env":
   {
     "APPNAME": "rap2",
     "PORT": "31000"
 }

For my Java App on Mac also works like this configuration.

@dcsan
Copy link

dcsan commented Aug 13, 2019

they should really document this, I find my way back here every time I try messing around with the debugger before usually giving up anyway.

@pieandcakes
Copy link
Contributor

@dcsan If you are using node and continually running into this problem, I would suggest you file an issue in the VS Code repository as this one is for the C/C++ extension. We don't have any control over the node extension or its documentation.

@947942434
Copy link

windows ? $env:path+=/xx/xx;/xxxx/xxx
set path=%path%;/xx/xx;/xxx/xxx
"environment":[{“name”:"path","value":"${path};/xx/xx;/xxx/xxx"}] ?

@GPhilo
Copy link

GPhilo commented Apr 22, 2021

Could you please make it more explicit in the documentation that the expected format is to pass the name of the env variable and its value as values of the respective "name" and "value" keys in the json object?

The "natural" way to describe the env var to me is {"my_env_var_name": "my_value"}, not {"name": "my_env_var_name", "value": "my_value"} and using the first causes a very cryptic error message instead of something like "name key not found in environment variable specification" or something of the sort.

sean-mcmanus added a commit to sean-mcmanus/vscode-docs that referenced this issue Apr 23, 2021
@sean-mcmanus
Copy link
Collaborator

@GPhilo Like this microsoft/vscode-docs#4492 and #7417 ?

@GPhilo
Copy link

GPhilo commented Apr 25, 2021

@sean-mcmanus That's much clearer now, thank you for the quick update!

@mkamyab
Copy link

mkamyab commented Jul 26, 2021

This works for python as well.

anyone who's looking for a node example, this worked for me.

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  // APPNAME=rap2 PORT=31000 DEBUG=*,-engine*,-socket.io*,-snapdragon*,-send,-express*,-nodemon*,-superagent,-AmpMetrics,-body-parser* nodemon server.js

  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "cwd": "${workspaceFolder}/server",
      "program": "${workspaceFolder}/server/server.js",
      "env":
        {
          "APPNAME": "rap2",
          "PORT": "31000"
        }
    }
  ]
}

it's not very well documented here:
https://code.visualstudio.com/docs/editor/debugging#_launch-configurations

@bartekpacia
Copy link

How can I reference the env var from "env" in my "args"?

@rafaeldsousa
Copy link

rafaeldsousa commented Nov 15, 2021

@bartekpacia also want to know if we can actually do this. The usage of ${env:VARNAME} as suggested in docs for args doesn't seem to work. I want to be able to use ENV vars from ENV file, so that I can commit this to my repo and don't expose any sensitive information.

@bartekpacia
Copy link

@rafaeldsousa Apparently this is not possible. You can take a look at this issue.

@rafaeldsousa
Copy link

@bartekpacia I believe I was able to use a combination of envFile + some non sensitive info on env.

"envFile": "${workspaceFolder}/.env",
"env": {
.
.
.
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests