Environment variables in Linux are supported now in VSCode (although I don't know since when or which version exactly). I have VSCode for Linux 1.73.1.
You can now use the following (just like in the question above):
{
"java.home": "${env:HOME}/.sdkman/candidates/java/8.0.222.hs-adpt/"
}
Answer from Hechi on Stack OverflowVideos
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"}
]
Could anyone tell me why this doesn't work?
{
"roc-lang.language-server.exe": "${env:ROC_LANGUAGE_SERVER_PATH}",
// If you'd like to format Roc files on save
"editor.formatOnSave": true
}This is in the workspace settings.json for a project. When I look at the plugin's output, it's treating that exact string as the path, rather than resolving it to the value of the environmental variable. A few other things to note:
If I instead use the literal string that is the value of the variable, it works fine.
I am able to echo that variable in vs code's terminal, so I know it's available.
I am on on linux with a reasonably recent version of vs code.
Thanks.
EDIT: Maybe this is just a limitation of the roc language plugin, that it isn't programmed to resolve the variable?
I am trying to override the default python interpreter for a specific workspace. I have created a .vscode directory in the folder with the following two files:
settings.json:
{
"python.envFile": "${workspaceFolder}/.vscode/.env",
"python.defaultInterpreterPath": "${env:MY_PYTHON_PATH}/bin/python",
}.vscode/.env:
MY_PYTHON_PATH=/path/to/my/python/interpreter
But the python interpreter is actually getting set to /bin/python - which implies that ${env:MY_PYTHON_PATH} is just empty.
Any ideas what is going wrong here?
Assuming you mean for a debugging session(?) then you can include a env property in your launch configuration.
If you open the .vscode/launch.json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env property with a dictionary of string:string.
Here is an example for an ASP.NET Core app from their standard web template setting the ASPNETCORE_ENVIRONMENT to Development :
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/vscode-env.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
You can load an environment file by setting the envFile property like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env", // HERE
"args": [],
"showLog": true
}
]
}
Place the .env file in your folder and add vars like this:
KEY1="TEXT_VAL1"
KEY2='{"key1":val1","key2":"val2"}'
Further Reading: Debugging go in vscode with environment variables
Currently you cannot set variables in the settings.json file.
The current open issue for VS Code to implement this feature is here: https://github.com/microsoft/vscode/issues/2809
It has yet to have a PR and the opening of the issue occurred Feb 2016, but with comments within the last 2 months.
Quoting from the same docs which were linked in the question post (asker just needed to scroll down) https://code.visualstudio.com/docs/editor/variables-reference#_is-variable-substitution-supported-in-user-and-workspace-settings :
Is variable substitution supported in User and Workspace settings?
The predefined variables are supported in a select number of setting keys in
settings.jsonfiles such as the terminalcwd,env,shellandshellArgsvalues. Some settings likewindow.titlehave their own variables:"window.title": "${dirty}${activeEditorShort}${separator}${rootName}${separator}${appName}"Refer to the comments in the Settings editor (
Ctrl+,) to learn about setting specific variables.
Even quoting from the very first sentence in that entire document:
Visual Studio Code supports variable substitution in Debugging and Task configuration files as well as some select settings. Variable substitution is supported inside some key and value strings in
launch.jsonandtasks.jsonfiles using${variableName}syntax.
If you want to be able to use variables freely in settings.json, then see Support variables when resolving values in settings #2809, give it a thumbs up to show support for it, and subscribe to it to get notified about discussion and progress. Please avoid making comments there like "+1" / "bump" / "still not implemented??!!1?!?1".