vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

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

Open nebjamin99 opened this issue 10 years ago • 26 comments

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?

nebjamin99 avatar Apr 20 '16 16:04 nebjamin99

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.

delmyers avatar Apr 20 '16 17:04 delmyers

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

nebjamin99 avatar Apr 20 '16 17:04 nebjamin99

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).

nebjamin99 avatar Apr 28 '16 15:04 nebjamin99

The correct format for this environment object is:

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

krworks avatar May 26 '16 23:05 krworks

Closing as it looks like @krworks comment solves the problem

pieandcakes avatar Jul 23 '16 00:07 pieandcakes

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

NewProggie avatar Aug 31 '16 13:08 NewProggie

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

dhiraj113 avatar Apr 07 '17 22:04 dhiraj113

@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.

pieandcakes avatar Apr 10 '17 18:04 pieandcakes

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.

codermannn avatar Jan 10 '19 22:01 codermannn

@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.

pieandcakes avatar Jan 10 '19 23:01 pieandcakes

'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.

codermannn avatar Jan 10 '19 23:01 codermannn

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

pieandcakes avatar Jan 11 '19 00:01 pieandcakes

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 avatar Apr 23 '19 15:04 dcsan

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

dcsan avatar Apr 23 '19 15:04 dcsan

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

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

abser avatar May 08 '19 03:05 abser

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.

dcsan avatar Aug 13 '19 13:08 dcsan

@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.

pieandcakes avatar Aug 13 '19 14:08 pieandcakes

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

947942434 avatar Dec 29 '20 07:12 947942434

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.

GPhilo avatar Apr 22 '21 09:04 GPhilo

@GPhilo Like this https://github.com/microsoft/vscode-docs/pull/4492 and https://github.com/microsoft/vscode-cpptools/pull/7417 ?

sean-mcmanus avatar Apr 23 '21 20:04 sean-mcmanus