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 Overflow
🌐
Visual Studio Code
code.visualstudio.com › docs › reference › variables-reference
Variables reference
November 3, 2021 - You can reference environment variables with the ${env:Name} syntax. For example, ${env:USERNAME} references the USERNAME environment variable. { "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceFolder}/app.js", ...
Discussions

launch.json - dynamic args and better env support
The current options available in vscode around the launch.json file do not cater for some more advanced scenarios. When debugging, it is useful to extract any sensitive credentials into an external file (e.g. secrets.env) so that these can be excluded via .gitignore and not committed to the repo. These variables should be easy to access in the args attribute if the application cannot accept them via environment ... More on github.com
🌐 github.com
4
February 8, 2020
How to Set up Environment Variables in "launch.json" Configuration When Using GDB Integration in VS Code - Unix & Linux Stack Exchange
The purpose of this setup is to use GDB integration in vscode. In particular, I specified the following pair to ensure all environment variables set up in my bash session in terminal of vscode are inherited when debugging: "externalConsole": false. I also notice that there is a pair named ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
February 16, 2022
visual studio code - vscode passing environment var to args on launch.json - Stack Overflow
I've created task (tasks.json) for setting environment variable as below export SOME_IP_VARIABLE=1.2.3.4 my application needs IP parameters so I added args in launch.json as below ... "configurat... More on stackoverflow.com
🌐 stackoverflow.com
How to forward environment variables from shell I to launch.json
One way to do it could be to set the environment variable in the environment your program launches in. For example when running inside a dev container, in the devcontainer.json. There the syntax is something lik ${var_on_host} More on reddit.com
🌐 r/vscode
3
4
May 4, 2023
🌐
GitHub
github.com › microsoft › vscode › issues › 91053
Resolve Environment Variables in Args defined in launch.json for Debugging · Issue #91053 · microsoft/vscode
February 20, 2020 - where the user may wish to alter ... PRETRAIN_BERT_DIR. In current Version of VSCode, using ${VARIABLE} or ${env:VARIABLE} syntax in args will return empty strings in current version of VSCode....
Author   BorisPolonsky
🌐
GitHub
github.com › microsoft › vscode › issues › 93823
launch.json - dynamic args and better env support · Issue #93823 · microsoft/vscode
February 8, 2020 - The --attributes argument under args is built on each debug execution by dynamically serialising an editable yaml file to json and substituting a number of environment variables in the contents.
Author   paulbouwer
🌐
Visual Studio Code
code.visualstudio.com › docs › debugtest › debugging-configuration
Visual Studio Code debug configuration
November 3, 2021 - For complex debugging scenarios or applications, you need to create a launch.json file to specify the debugger configuration. For example, to specify the application entry point, attach to a running application, or to set environment variables.
🌐
Visual Studio Code
code.visualstudio.com › docs › cpp › launch-json-reference
Configure C/C++ debugging
November 3, 2021 - JSON array of command-line arguments to pass to the program when it is launched. Example ["arg1", "arg2"]. If you are escaping characters, you will need to double escape them. For example, ["{\\\"arg1\\\": true}"] will send {"arg1": true} to ...
Top answer
1 of 3
4

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",
       
    },
]

}

2 of 3
3

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"}
               ]
🌐
Stack Overflow
stackoverflow.com › questions › 61723358 › vscode-passing-environment-var-to-args-on-launch-json
visual studio code - vscode passing environment var to args on launch.json - Stack Overflow
I've created task (tasks.json) for setting environment variable as below export SOME_IP_VARIABLE=1.2.3.4 my application needs IP parameters so I added args in launch.json as below ... "configurat...
Find elsewhere
🌐
Reddit
reddit.com › r/vscode › how to forward environment variables from shell i to launch.json
r/vscode on Reddit: How to forward environment variables from shell I to launch.json
May 4, 2023 -

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.

🌐
GitHub
github.com › microsoft › vscode › issues › 89825
launch.json: values from envFile not substituted in args · Issue #89825 · microsoft/vscode
November 3, 2021 - { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "skipFiles": ["<node_internals>/**"], "program": "${workspaceFolder}/index.js", "args": ["--user=${env:USER}", "--foo=${env:FOO}"], "envFile": "${workspaceFolder}/.env" } ] } ... The foo variable defined in .env isn't passed through.
Author   endquote
🌐
Medium
reuvenharrison.medium.com › using-visual-studio-code-to-debug-a-go-program-with-environment-variables-523fea268271
Debugging go in vscode with environment variables | by Reuven Harrison | Medium
August 30, 2020 - For more advanced scenarios, for example, requiring json values, you can load an external environment file, like this: { "version": "0.2.0", "configurations": [ { "name": "Launch", "type": "go", "request": "launch", "mode": "debug", "remotePath": "", "port": 2345, "host": "127.0.0.1", "${workspaceFolder}/subdir/http_test.go", "args": [ "-test.run", "TestWithHTTP" ], "envFile": "${workspaceFolder}/.env", "showLog": true } ] } Place the .env file in your directory and add more variables like this:
🌐
YouTube
youtube.com › watch
How to Add Environment Variables to launch.json in VSCODE - YouTube
👍 Don't forget to like, comment, and subscribe to stay updated with more coding tips and tricks! 🚀 In this video, I’ll walk you through the steps to add en...
Published   May 26, 2023
🌐
GitHub
github.com › microsoft › vscode › issues › 95371
Allow `launch.json` configuration to access environment specified by `"envFile"` · Issue #95371 · microsoft/vscode
April 15, 2020 - # The purpose of this file is to provide an environment which can be read # by configurations in launch.json, such as remote-host socket. REMOTE_HOST_SOCKET=192.168.1.155:1234 · I should be able to create a launch config that specifies these values: { "envFile": "${workspaceFolder}/.vscode/remote_debug.env", "miDebuggerServerAddress": "${env:REMOTE_HOST_SOCKET}", } ...where this launch config reads the environment variables in remote_debug.env, and they can be referenced by other parts of the launch configuration such as miDebuggerServerAddress.
Author   jnickg
🌐
Reddit
reddit.com › r/vscode › pass parameters to launch.json's program field
r/vscode on Reddit: Pass parameters to launch.json's program field
July 24, 2023 -

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

🌐
Visual Studio Code
code.visualstudio.com › docs › csharp › debugger-settings
List of configurable options
March 1, 2024 - To edit them in this case: ... Edit the args property. This can either be a string, or an array of strings · With C# Dev Kit, you can bring your launchSettings.json from Visual Studio to work with Visual Studio Code ...
🌐
CodeGenes
codegenes.net › blog › use-environment-variables-in-vs-code-launch-configurations
How to Use Environment Variables in VS Code Launch Configurations (launch.json) – Simplify Sharing with Coworkers
November 3, 2021 - Defining Environment Variables Directly in launch.json · Using .env Files for Flexible, Shareable Configuration ... launch.json is VS Code’s debugging configuration file. It lives in the .vscode folder at the root of your project and defines how VS Code should launch and debug your application (e.g., which interpreter to use, command-line arguments, or environment variables).
🌐
GitHub
github.com › microsoft › vscode-dotnettools › issues › 1544
How to set environment variables in launch.json? · Issue #1544 · microsoft/vscode-dotnettools
January 31, 2020 - I'm having a problem with this launch.json below. launch.json { "configurations": [ { "name": "C#: csharp-semantic-kernel [Default Configuration]", "type": "dotnet", "request": "launch", "projectPath": "${workspaceFolder}/csharp-semantic...
Author   lucasmsoares96
Top answer
1 of 4
22
  • 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 ]

2 of 4
4

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