Skip to content

Commit bd20f1f

Browse files
authored
Ensure debug configuration env variables overwrite env variables defined in .env file (#18095)
1 parent aa5defc commit bd20f1f

File tree

6 files changed

+32
-5
lines changed

6 files changed

+32
-5
lines changed

news/2 Fixes/16984.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure debug configuration env variables overwrite env variables defined in .env file.

src/client/common/variables/environment.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
3636
return parseEnvFile(contents, baseVars);
3737
}
3838

39-
public mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables) {
39+
public mergeVariables(
40+
source: EnvironmentVariables,
41+
target: EnvironmentVariables,
42+
options?: { overwrite?: boolean },
43+
) {
4044
if (!target) {
4145
return;
4246
}
@@ -45,7 +49,7 @@ export class EnvironmentVariablesService implements IEnvironmentVariablesService
4549
if (settingsNotToMerge.indexOf(setting) >= 0) {
4650
return;
4751
}
48-
if (target[setting] === undefined) {
52+
if (target[setting] === undefined || options?.overwrite) {
4953
target[setting] = source[setting];
5054
}
5155
});

src/client/common/variables/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const IEnvironmentVariablesService = Symbol('IEnvironmentVariablesService
99

1010
export interface IEnvironmentVariablesService {
1111
parseFile(filePath?: string, baseVars?: EnvironmentVariables): Promise<EnvironmentVariables | undefined>;
12-
mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables): void;
12+
mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables, options?: { overwrite?: boolean }): void;
1313
appendPythonPath(vars: EnvironmentVariables, ...pythonPaths: string[]): void;
1414
appendPath(vars: EnvironmentVariables, ...paths: string[]): void;
1515
}

src/client/debugger/extension/configuration/resolvers/helper.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ export class DebugEnvironmentVariablesHelper implements IDebugEnvironmentVariabl
2828
args.env && Object.keys(args.env).length > 0 ? ({ ...args.env } as any) : ({} as any);
2929
const envFileVars = await this.envParser.parseFile(args.envFile, debugLaunchEnvVars);
3030
const env = envFileVars ? { ...envFileVars } : {};
31-
this.envParser.mergeVariables(debugLaunchEnvVars, env);
31+
32+
// "overwrite: true" to ensure that debug-configuration env variable values
33+
// take precedence over env file.
34+
this.envParser.mergeVariables(debugLaunchEnvVars, env, { overwrite: true });
3235

3336
// Append the PYTHONPATH and PATH variables.
3437
this.envParser.appendPath(env, debugLaunchEnvVars[pathVariableName]);

src/client/debugger/extension/configuration/resolvers/launch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
103103
// set the "env" debug configuration argument. This expansion should be
104104
// done here before handing of the environment settings to the debug adapter
105105
debugConfiguration.env = await this.debugEnvHelper.getEnvironmentVariables(debugConfiguration);
106+
106107
if (typeof debugConfiguration.stopOnEntry !== 'boolean') {
107108
debugConfiguration.stopOnEntry = false;
108109
}

src/test/common/variables/envVarsService.unit.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ PYTHON=${BINDIR}/python3\n\
194194
verifyAll();
195195
});
196196

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

@@ -211,6 +211,24 @@ PYTHON=${BINDIR}/python3\n\
211211
expect(vars2).to.have.property(pathVariable, 'PATH', 'Incorrect value');
212212
verifyAll();
213213
});
214+
215+
test('Ensure path variables in target are overwritten', async () => {
216+
const source = { ONE: '1', TWO: 'TWO' };
217+
const target = { ONE: 'ONE', THREE: '3', PYTHONPATH: 'PYTHONPATH' };
218+
219+
(target as any)[pathVariable] = 'PATH';
220+
221+
variablesService.mergeVariables(source, target, { overwrite: true });
222+
223+
expect(Object.keys(source)).lengthOf(2, 'Source variables modified');
224+
expect(Object.keys(target)).lengthOf(5, 'Variables not merged');
225+
expect(target).to.have.property('ONE', '1', 'Expected to be overwritten');
226+
expect(target).to.have.property('TWO', 'TWO', 'Incorrect value');
227+
expect(target).to.have.property('THREE', '3', 'Variable not merged');
228+
expect(target).to.have.property('PYTHONPATH', 'PYTHONPATH', 'Incorrect value');
229+
expect(target).to.have.property(pathVariable, 'PATH', 'Incorrect value');
230+
verifyAll();
231+
});
214232
});
215233
});
216234

0 commit comments

Comments
 (0)