Vscode-cpptools: Passing environment variables to debugger: 'env' or 'environment'?

Created on 20 Apr 2016  ·  18Comments  ·  Source: microsoft/vscode-cpptools

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?

bug debugger

Most helpful comment

The correct format for this environment object is:

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

All 18 comments

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.

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

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

The correct format for this environment object is:

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

Closing as it looks like @krworks comment solves the problem

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

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

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

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.

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

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

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

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

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

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

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

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

Was this page helpful?
0 / 5 - 0 ratings