Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

move env consolidation after loading envFile #935

Merged
merged 4 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
50 changes: 19 additions & 31 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class Delve {
this.program = program;
this.remotePath = remotePath;
let mode = launchArgs.mode;
let env = launchArgs.env || {};
let env = Object.assign({}, launchArgs.env, process.env);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this way the process.env overrides launchArgs.env
It should be the other way around

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check last commit, I changed it.

let dlvCwd = dirname(program);
let isProgramDirectory = false;
this.connection = new Promise((resolve, reject) => {
Expand All @@ -207,20 +207,27 @@ 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
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');
}
}

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 +257,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, '');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, just changing the env in this line to finalEnv should also do the trick. Can you try that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or we can get rid of finalEnv altogether and instead just replace that with

env = Object.assign({}, process.env, env)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great idea! I was focusing only on getting it done 😄 Can you see how it looks now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I'm suggesting that we use the following precedence order
launch env -> file env -> process env so we can override file env values with launch specific values.

What do you think? Check the last commit.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed on the order.

}
});
} 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 +289,7 @@ class Delve {

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

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