diff --git a/news/2 Fixes/16984.md b/news/2 Fixes/16984.md new file mode 100644 index 000000000000..3672244c69c9 --- /dev/null +++ b/news/2 Fixes/16984.md @@ -0,0 +1 @@ +Ensure debug configuration env variables overwrite env variables defined in .env file. diff --git a/src/client/common/variables/environment.ts b/src/client/common/variables/environment.ts index 60042c9b452c..bce6d2ca17e9 100644 --- a/src/client/common/variables/environment.ts +++ b/src/client/common/variables/environment.ts @@ -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; } @@ -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]; } }); diff --git a/src/client/common/variables/types.ts b/src/client/common/variables/types.ts index b9148d052c54..d5a5e76e4d85 100644 --- a/src/client/common/variables/types.ts +++ b/src/client/common/variables/types.ts @@ -9,7 +9,7 @@ export const IEnvironmentVariablesService = Symbol('IEnvironmentVariablesService export interface IEnvironmentVariablesService { parseFile(filePath?: string, baseVars?: EnvironmentVariables): Promise; - 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; } diff --git a/src/client/debugger/extension/configuration/resolvers/helper.ts b/src/client/debugger/extension/configuration/resolvers/helper.ts index 510792c3c72a..711d1c3ce4fc 100644 --- a/src/client/debugger/extension/configuration/resolvers/helper.ts +++ b/src/client/debugger/extension/configuration/resolvers/helper.ts @@ -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]); diff --git a/src/client/debugger/extension/configuration/resolvers/launch.ts b/src/client/debugger/extension/configuration/resolvers/launch.ts index a3f8e8bd7e58..4d95cde387fa 100644 --- a/src/client/debugger/extension/configuration/resolvers/launch.ts +++ b/src/client/debugger/extension/configuration/resolvers/launch.ts @@ -103,6 +103,7 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver { + test('Ensure path variables in target are left untouched', async () => { const vars1 = { ONE: '1', TWO: 'TWO' }; const vars2 = { ONE: 'ONE', THREE: '3', PYTHONPATH: 'PYTHONPATH' }; @@ -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(); + }); }); });