Skip to content

Commit

Permalink
move env consolidation after loading envFile (golang#935)
Browse files Browse the repository at this point in the history
* fix for golang#934

* fix travis build, forgot to tsc

* ignore DS_Store and some clean up on env consolidation

* precedence: launch env -> file env -> process env
  • Loading branch information
goenning authored and ramya-rao-a committed Apr 20, 2017
1 parent 6ea85f0 commit 5fab6f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
out
node_modules
.vscode-test
*.vsix
*.vsix
.DS_Store
51 changes: 20 additions & 31 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ class Delve {
this.program = program;
this.remotePath = remotePath;
let mode = launchArgs.mode;
let env = launchArgs.env || {};
let dlvCwd = dirname(program);
let isProgramDirectory = false;
this.connection = new Promise((resolve, reject) => {
Expand All @@ -207,20 +206,29 @@ class Delve {
return reject('The program attribute must point to valid directory, .go file or executable.');
}

// Consolidate env vars
let finalEnv: Object = null;
if (Object.keys(env).length > 0) {
finalEnv = {};
for (let k in process.env) {
finalEnv[k] = process.env[k];
}
for (let k in env) {
finalEnv[k] = env[k];
// read env from disk and merge into envVars
let fileEnv = {};
if (launchArgs.envFile) {
try {
const buffer = stripBOM(FS.readFileSync(launchArgs.envFile, 'utf8'));
buffer.split('\n').forEach( line => {
const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (r !== null) {
let value = r[2] || '';
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
value = value.replace(/\\n/gm, '\n');
}
fileEnv[r[1]] = value.replace(/(^['"]|['"]$)/g, '');
}
});
} catch (e) {
return reject('Cannot load environment variables from file');
}
}

let env = Object.assign({}, process.env, fileEnv, launchArgs.env);
if (!!launchArgs.noDebug && mode === 'debug' && !isProgramDirectory) {
this.debugProcess = spawn(getGoRuntimePath(), ['run', program], {env: finalEnv});
this.debugProcess = spawn(getGoRuntimePath(), ['run', program], { env });
this.debugProcess.stderr.on('data', chunk => {
let str = chunk.toString();
if (this.onstderr) { this.onstderr(str); }
Expand Down Expand Up @@ -250,25 +258,6 @@ class Delve {
return;
}

// read env from disk and merge into envVars
if (launchArgs.envFile) {
try {
const buffer = stripBOM(FS.readFileSync(launchArgs.envFile, 'utf8'));
buffer.split('\n').forEach( line => {
const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
if (r !== null) {
let 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, '');
}
});
} catch (e) {
return reject('Cannot load environment variables from file');
}
}

let dlv = getBinPathWithPreferredGopath('dlv', resolvePath(env['GOPATH']));

if (!existsSync(dlv)) {
Expand Down Expand Up @@ -301,7 +290,7 @@ class Delve {

this.debugProcess = spawn(dlv, dlvArgs, {
cwd: dlvCwd,
env: finalEnv,
env,
});

function connectClient(port: number, host: string) {
Expand Down

0 comments on commit 5fab6f6

Please sign in to comment.