Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
support loading env vars from file; fixes microsoft/vscode#14523
Browse files Browse the repository at this point in the history
  • Loading branch information
weinand committed Nov 10, 2016
1 parent 92954cc commit e9ef5be
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
8 changes: 4 additions & 4 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "node-debug",
"displayName": "Node Debug",
"version": "1.8.0",
"version": "1.8.1",
"publisher": "ms-vscode",
"description": "%extension.description%",
"icon": "images/node-debug-icon.svg",
Expand All @@ -25,8 +25,8 @@
"node": "^5.10.0"
},
"dependencies": {
"vscode-debugprotocol": "^1.15.0-pre.1",
"vscode-debugadapter": "^1.15.0-pre.1",
"vscode-debugprotocol": "^1.15.0-pre.2",
"vscode-debugadapter": "^1.15.0-pre.2",
"source-map": "^0.5.3",
"vscode-nls": "^2.0.1",
"request-light": "^0.1.0",
Expand Down Expand Up @@ -144,6 +144,11 @@
"description": "%node.launch.env.description%",
"default": {}
},
"envFile": {
"type": "string",
"description": "%node.launch.envFile.description%",
"default": "${workspaceRoot}/.env"
},
"sourceMaps": {
"type": "boolean",
"description": "%node.sourceMaps.description%",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"node.launch.runtimeExecutable.description": "Runtime to use. Either an absolute path or the name of a runtime available on the PATH. If ommitted 'node' is assumed.",
"node.launch.runtimeArgs.description": "Optional arguments passed to the runtime executable.",
"node.launch.env.description": "Environment variables passed to the program.",
"node.launch.envFile.description": "Absolute path to a file containing environment variable definitions.",

"node.launch.config.name": "Launch",

Expand Down
33 changes: 29 additions & 4 deletions src/node/nodeDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ interface LaunchRequestArguments extends DebugProtocol.LaunchRequestArguments, C
runtimeArgs?: string[];
/** Optional environment variables to pass to the debuggee. The string valued properties of the 'environmentVariables' are used as key/value pairs. */
env?: { [key: string]: string; };
/** Optional path to .env file. */
envFile?: string;
/** Deprecated: if true launch the target in an external console. */
externalConsole?: boolean;
/** Where to launch the debug target. */
Expand Down Expand Up @@ -836,14 +838,38 @@ export class NodeDebugSession extends DebugSession {
const address = args.address;
const timeout = args.timeout;

let envVars = args.env;

// read env from disk and merge into envVars
if (args.envFile) {
try {
const buffer = FS.readFileSync(args.envFile, 'utf8');
const env = {}
buffer.split('\n').forEach( line => {
var r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (r != null) {
var value = r[2] || '';
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length-1) === '"') {
value = value.replace(/\\n/gm, '\n')
}
env[r[1]] = value.replace(/(^['"]|['"]$)/g, '');
}
});
envVars = PathUtils.extendObject(env, args.env) // launch config env vars overwrite .env vars
} catch (e) {
this.sendErrorResponse(response, 2029, localize('VSND2029', "Can't load environment variables from file ({0}).", '{_error}'), { _error: e.message });
return;
}
}

if (this._supportsRunInTerminalRequest && (this._console === 'externalTerminal' || this._console === 'integratedTerminal')) {

const termArgs : DebugProtocol.RunInTerminalRequestArguments = {
kind: this._console === 'integratedTerminal' ? 'integrated' : 'external',
title: localize('node.console.title', "Node Debug Console"),
cwd: workingDirectory,
args: launchArgs,
env: args.env
env: envVars
};

this.runInTerminalRequest(termArgs, NodeDebugSession.RUNINTERMINAL_TIMEOUT, runResponse => {
Expand All @@ -859,7 +885,6 @@ export class NodeDebugSession extends DebugSession {
this._attach(response, port, address, timeout);
}
} else {

this.sendErrorResponse(response, 2011, localize('VSND2011', "Cannot launch debug target in terminal ({0}).", '{_error}'), { _error: runResponse.message } );
this._terminated('terminal error: ' + runResponse.message);
}
Expand All @@ -870,11 +895,11 @@ export class NodeDebugSession extends DebugSession {
this._sendLaunchCommandToConsole(launchArgs);

// merge environment variables into a copy of the process.env
const env = PathUtils.extendObject(PathUtils.extendObject( { }, process.env), args.env);
envVars = PathUtils.extendObject(PathUtils.extendObject( {}, process.env), envVars);

const options = {
cwd: workingDirectory,
env: env
env: envVars
};

const nodeProcess = CP.spawn(runtimeExecutable, launchArgs.slice(1), options);
Expand Down

0 comments on commit e9ef5be

Please sign in to comment.