如何在VSCode中将环境variables添加到launch.json中

在node.js项目中使用新的VSCode编辑器。 我正在尝试通过编辑launch.json文件来configuration我的“启动”configuration文件进行debugging。 我需要设置一个连接string作为一个环境variables。 根据launch.json文件中的注释:

// Environment variables passed to the program. "env": { } 

我曾尝试添加我的环境variables,如下所示:

 "env": { "CONNECTION_STRING": "Data Source=server;Initial Catalog=catalog;User ID=uid;Password=pwd;MultipleActiveResultSets=true" } 

这会导致错误,当我尝试启动我的应用程序; “OpenDebug进程意外终止”。 我还没有find任何可以解释问题的日志文件等。

我知道这个应用程序工作正常,当我设置环境variables,并从标准的命令提示符启动我的应用程序。 如果我在launch.json文件中注释掉我的variables,该应用程序也可以正常运行; 我只是无法连接到数据库。

我假设我在launch.json文件中使用了错误的格式,但我还没有find任何方法来使这个工作。

有任何想法吗?

在Windows上(也可能在Linux上),似乎环境variables有问题。 它在OS X上工作,我们正在调查。 预计很快就会修复。

Andre Weinand,Visual Studio Code

作为一种解决方法,您可以在启动VSCode时设置环境variables,例如,使用这个小小的powershell脚本:

 param( $vars = @{} ) $vars.Keys | % { write-host "adding env variable: $_=$($vars[$_])" [Environment]::SetEnvironmentVariable($_, $vars[$_], "Process") } $ver = "0.1.0" & "$env:LOCALAPPDATA\Code\app-$ver\Code.exe" 

将其保存为vscode.ps1vscode.ps1调用它,如下所示:

 powershell ".\vscode.ps1 -vars @{ 'NODE_ENV'='test'; 'SOMETHING'='else' }" 

我成功地使用launch.jsonenv属性传递它们:

 { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "SLS Webpack", "protocol": "legacy", "program": "${workspaceRoot}/node_modules/.bin/sls", "cwd": "${workspaceRoot}", "args": ["webpack", "watch", "-f", "${fileBasenameNoExtension}", "-p", "${fileDirname}/event.json"], "env": {"AWS_REGION":"us-east-1", "SLS_DEBUG":"*"}, "outFiles": ["${cwd}/dist/**/*.js"], "sourceMaps": true, "smartStep": true } ] } 

像这样,在你下面操作系统:

  "osx": { "MIMode": "lldb", "environment": [{"name": "DYLD_LIBRATY_PATH", "value": "/Users/x/boost_1_63_0/stage/lib/"}] },