From b97610af06f8912af79bd4ca6ecd5c773d07adfb Mon Sep 17 00:00:00 2001 From: Kartik Raj Date: Mon, 11 Sep 2023 16:39:17 -0700 Subject: [PATCH] Do not assume casing of activated environment variables Python returns --- .../terminalEnvVarCollectionService.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/client/interpreter/activation/terminalEnvVarCollectionService.ts b/src/client/interpreter/activation/terminalEnvVarCollectionService.ts index 9015dd7b9388..6c82811d4500 100644 --- a/src/client/interpreter/activation/terminalEnvVarCollectionService.ts +++ b/src/client/interpreter/activation/terminalEnvVarCollectionService.ts @@ -133,12 +133,13 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ traceVerbose('Activating environments in terminal is disabled for', resource?.fsPath); return; } - const env = await this.environmentActivationService.getActivatedEnvironmentVariables( + const activatedEnv = await this.environmentActivationService.getActivatedEnvironmentVariables( resource, undefined, undefined, shell, ); + const env = activatedEnv ? normCaseKeys(activatedEnv) : undefined; if (!env) { const shellType = identifyShellFromShellPath(shell); const defaultShell = defaultShells[this.platform.osType]; @@ -158,7 +159,7 @@ export class TerminalEnvVarCollectionService implements IExtensionActivationServ shell, ); } - const processEnv = this.processEnvVars; + const processEnv = normCaseKeys(this.processEnvVars); // PS1 in some cases is a shell variable (not an env variable) so "env" might not contain it, calculate it in that case. env.PS1 = await this.getPS1(shell, resource, env); @@ -376,3 +377,14 @@ function getPromptForEnv(interpreter: PythonEnvironment | undefined) { } return undefined; } + +function normCaseKeys(env: EnvironmentVariables): EnvironmentVariables { + const result: EnvironmentVariables = {}; + Object.keys(env).forEach((key) => { + // `os.environ` script used to get env vars normalizes keys to upper case: + // https://github.com/python/cpython/issues/101754 + // So convert `process.env` keys to upper case to match. + result[key.toUpperCase()] = env[key]; + }); + return result; +}