Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions news/2 Fixes/16984.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure debug configuration env variables overwrite env variables defined in .env file.
8 changes: 6 additions & 2 deletions src/client/common/variables/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
return parseEnvFile(contents, baseVars);
}

public mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables) {
public mergeVariables(
source: EnvironmentVariables,
target: EnvironmentVariables,
options?: { overwrite?: boolean },
) {
if (!target) {
return;
}
Expand All @@ -45,7 +49,7 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
if (settingsNotToMerge.indexOf(setting) >= 0) {
return;
}
if (target[setting] === undefined) {
if (target[setting] === undefined || options?.overwrite) {
target[setting] = source[setting];
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/client/common/variables/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const IEnvironmentVariablesService = Symbol('IEnvironmentVariablesService

export interface IEnvironmentVariablesService {
parseFile(filePath?: string, baseVars?: EnvironmentVariables): Promise<EnvironmentVariables | undefined>;
mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables): void;
mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables, options?: { overwrite?: boolean }): void;
appendPythonPath(vars: EnvironmentVariables, ...pythonPaths: string[]): void;
appendPath(vars: EnvironmentVariables, ...paths: string[]): void;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export class DebugEnvironmentVariablesHelper implements IDebugEnvironmentVariabl
args.env && Object.keys(args.env).length > 0 ? ({ ...args.env } as any) : ({} as any);
const envFileVars = await this.envParser.parseFile(args.envFile, debugLaunchEnvVars);
const env = envFileVars ? { ...envFileVars } : {};
this.envParser.mergeVariables(debugLaunchEnvVars, env);

// "overwrite: true" to ensure that debug-configuration env variable values
// take precedence over env file.
this.envParser.mergeVariables(debugLaunchEnvVars, env, { overwrite: true });

// Append the PYTHONPATH and PATH variables.
this.envParser.appendPath(env, debugLaunchEnvVars[pathVariableName]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
// set the "env" debug configuration argument. This expansion should be
// done here before handing of the environment settings to the debug adapter
debugConfiguration.env = await this.debugEnvHelper.getEnvironmentVariables(debugConfiguration);

if (typeof debugConfiguration.stopOnEntry !== 'boolean') {
debugConfiguration.stopOnEntry = false;
}
Expand Down
20 changes: 19 additions & 1 deletion src/test/common/variables/envVarsService.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ PYTHON=${BINDIR}/python3\n\
verifyAll();
});

test('Ensure path variables variables in target are left untouched', async () => {
test('Ensure path variables in target are left untouched', async () => {
const vars1 = { ONE: '1', TWO: 'TWO' };
const vars2 = { ONE: 'ONE', THREE: '3', PYTHONPATH: 'PYTHONPATH' };

Expand All @@ -211,6 +211,24 @@ PYTHON=${BINDIR}/python3\n\
expect(vars2).to.have.property(pathVariable, 'PATH', 'Incorrect value');
verifyAll();
});

test('Ensure path variables in target are overwritten', async () => {
const source = { ONE: '1', TWO: 'TWO' };
const target = { ONE: 'ONE', THREE: '3', PYTHONPATH: 'PYTHONPATH' };

(target as any)[pathVariable] = 'PATH';

variablesService.mergeVariables(source, target, { overwrite: true });

expect(Object.keys(source)).lengthOf(2, 'Source variables modified');
expect(Object.keys(target)).lengthOf(5, 'Variables not merged');
expect(target).to.have.property('ONE', '1', 'Expected to be overwritten');
expect(target).to.have.property('TWO', 'TWO', 'Incorrect value');
expect(target).to.have.property('THREE', '3', 'Variable not merged');
expect(target).to.have.property('PYTHONPATH', 'PYTHONPATH', 'Incorrect value');
expect(target).to.have.property(pathVariable, 'PATH', 'Incorrect value');
verifyAll();
});
});
});

Expand Down