I'm successfully passing them using the env property in launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js",
"env": {
"TEST_VAR": "foo"
}
}
]
}
Answer from btburton42 on Stack OverflowI'm successfully passing them using the env property in launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}/index.js",
"env": {
"TEST_VAR": "foo"
}
}
]
}
This is working

just add the following:
"env": {
"NODE_ENV": "development"
}
As shown below:
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program", //TODO: cmd as launch program
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\index.js",
"env": {
"NODE_ENV": "development"
}
}
]
launch.json - dynamic args and better env support
How to Set up Environment Variables in "launch.json" Configuration When Using GDB Integration in VS Code - Unix & Linux Stack Exchange
visual studio code - vscode passing environment var to args on launch.json - Stack Overflow
How to forward environment variables from shell I to launch.json
Here is an example, you set the value in the environment block.
{
// 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
"version": "0.2.0",
"configurations": [
{
"name": "g++ - (GDB 9.2) Build and debug active file with RepoCodeInspection",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/bin/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"description": "same as commands below by using 'setenv ...",
"info": "cant debug b/c of libBase/libRecipe now requiring dependency to boost for stacktrace dumps",
"name": "LD_LIBRARY_PATH",
"value": "/libs/:./"
}
],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": {
"trace": false,
"traceResponse": false
},
"preLaunchTask": "RepoCodeInspection",
},
{
"name": "g++ - (GDB 9.2) Attach to a running process",
"type": "cppdbg",
"request": "attach",
"processId":"${command:pickProcess}",
"program": "${fileDirname}/bin/${fileBasenameNoExtension}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"logging": {
"trace": false,
"traceResponse": false
},
"preLaunchTask": "RepoCodeInspection",
},
]
}
To set multiple environment variables, use comma-separated "name", "value" pairs.
Example:
"environment": [{"name": "LD_LIBRARY_PATH", "value": "/ld/library/path/"},
{"name": "CUDA_VISIBLE_DEVICES", "value": "0"}
]
Hi, I already know how to set environment variables in launch.json.
What I can’t figure out is how to have it read environment variables from my shell. My AWS credentials get cycled a lot and I was to do something like
“env” : { AWS_SESSION_TOKEN: “${AWS_SESSION_TOKEN}”
So the debugger pulls the variables from the current shell. Is there any way to do that?
It’s not finding them currently and I’m losing my mind running thing without a debugger.
I followed this page #VSCode: C++ Development and Debugging using containers, successfully setup vscode env to debug a c program running inside a docker without a problem. Below is my launch.json content inside .vscode dir.
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "./main",
"args": [],
"stopAtEntry": true,
"cwd": "/myproject",
"environment": [],
"externalConsole": true,
"sourceFileMap": { "/myproject": "${workspaceFolder}" },
"pipeTransport": {
"debuggerPath": "/usr/bin/gdb",
"pipeProgram": "/usr/bin/sshpass",
"pipeArgs": [
"-p",
"root",
"ssh",
"root@localhost",
"-p",
"2222"
],
"pipeCwd": ""
},
"MIMode": "gdb"
}
]
}The problem is the program field in the launch.json only accepts binary blob i.e. ./main without passing any parameters. My executable program needs some parameters such as `-i eth0 -c /path/to/my.conf ...`. However, if I provide the command, for instance, `./main -i eth0 -c /path/to/my.conf`. VSCode complains
Unable to start debugging. Program path `./main -i eth0 -c /path/to/my.conf` is missing or invalid. GDB failed with message: ./main -i eth0 -c /path/to/my.conf: No such file or directory. ...
How should I configure so that I can dynamically pass different parameters to the program for debugging? Thanks
- Doesn't work, why?
According to this post from user weinand...
The ".env" file is read and processed after VS Code has substituted variables in the launch config. So your debugged program will indeed see the environment variable "FOO" with the correct value but VS Code's variable substitution in the launch.json will not see it.
The reason for this is that ".env" files are a node.js concept and not a generic platform mechanism. So VS Code does not know anything about .env files, but the node.js debugger knows about .env files.
... this functionality in launch.json is specific for applications running on Node.js, although that's not what M$ explains in their documentations for VSCode.
- Possible solution
For Python applications (possibly for other platforms as well) environment variables defined in a .env file (or whatever name you like) will be available for your application as long as the following configuration is present in launch.json...
{
"version": "0.2.0",
"configurations": [
{
[...]
"envFile": "${workspaceFolder}/.env", // Path to the ".env" file.
[...]
}
]
}
Note that just exporting a variable...
export SOMEVAR_A=1234
... will not make the environment variable SOMEVAR_A available for the application being executed by the VSCode debugger nor for the settings - especially inside "env" and "args" ("configurations") - in launch.json as, for example, in this case...
{
"version": "0.2.0",
"configurations": [
{
[...]
"env": {
"SOMEVAR_A": "${env:SOMEVAR_A}"
},
"args": [
"${env:SOMEVAR_A}"
]
[...]
}
]
}
NOTE: In our tests the ${env:SOMEVAR_A} syntax did not work in any scenario. That is, didn't work for the application ("env") and didn't work for the settings ("args") in launch.json.
PLUS I: Dirt Hack
For values present in "args" ("configurations") you can use the hack below...
{
"version": "0.2.0",
"configurations": [
{
[...]
"envFile": "${workspaceFolder}/.env",
"args": [
"`source \"${workspaceFolder}/.env\";echo ${SOMEVAR_A}`"
]
[...]
}
]
}
... as the configuration in "envFile" doesn't work.
Notice, although, that the following construction...
[...]
"args": [
"`echo ${SOMEVAR_A}`"
]
[...]
... would also work for "args" as long as the environment variable "SOMEVAR_A" has been previously exported in the conventional way.
The same reasoning would work for a tasks (tasks.json), but in both cases we can't guarantee that.
TIP: An .env File Example
SOMEVAR_A="abcd"
SOMEVAR_B="efgh"
SOMEVAR_C=123456
PLUS II: Export Variables
There are cases where you will need to export variables (eg. export SOMEVAR_A="abcd") so that they can be consumed by certain resources. In these cases there may be problems, because the fact that we export variables prevents (we don't know why) that they are seen in the context of the "envFile" configuration "envFile": "${workspaceFolder}/.env".
A workaround to get around these limitations is to add set -a before the variables set and set +a after it. With this we were able to meet the two scenarios as this example...
#!/usr/bin/env bash
set -a
SOMEVAR_A="abcd"
SOMEVAR_B="efgh"
SOMEVAR_C=123456
set +a
... or in a more compatible and safe way use set -a/set +a as in this example...
[...]
"args": [
"`set -a;source \"${workspaceFolder}/.env\";set +a;echo ${SOMEVAR_A}`"
[...]
VSCode's support for environment variables is a mess!
- Conclusion
We don't know if the limitations we are dealing with here are from VSCode's own design or are bugs. Anyway, it doesn't seem to make much sense.
These procedures were tested on Manjaro Linux (Arch based).
[Ref(s).: https://unix.stackexchange.com/a/79077/61742 , https://stackoverflow.com/a/30969768/3223785 ]
Looking at the issue comment quoted below, it seems this is currently not possible.
${env:...} only expands environment variables that were set in the parent shell that ran code. It doesn't expand variables set in the tasks.json env options.
https://github.com/Microsoft/vscode/issues/47985#issuecomment-460678885
As described in the documentation, you need to use the args attribute. E.g.
{
// 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
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug App",
"program": "${workspaceFolder}/main.js",
"args": ["arg1", "arg2", "arg3"]
}
]
}
I pass arguments by this way for the python program, it may work for nodejs:
{
"type": "node",
"request": "launch",
"name": "Debug App",
"program": "${workspaceFolder}/main.js",
"args": ["--arg1", "value1", "--arg2", "value2"]
}