| 75 | |
| 76 | const envVariables = new Map<string, Promise<NodeJS.ProcessEnv | undefined>>(); |
| 77 | export async function getActivatedEnvVariables(pythonPath: Uri): Promise<NodeJS.ProcessEnv | undefined> { |
| 78 | const key = getComparisonKey(pythonPath); |
| 79 | if (envVariables.has(key)) { |
| 80 | return envVariables.get(key); |
| 81 | } |
| 82 | const promise = (async () => { |
| 83 | const cli = await getPythonCli(pythonPath); |
| 84 | const processService = new ProcessService(); |
| 85 | const separator = 'e976ee50-99ed-4aba-9b6b-9dcd5634d07d'; |
| 86 | const argv = [...cli, path.join(SCRIPTS_DIR, 'printEnvVariables.py')]; |
| 87 | const cmd = argv.reduce((p, c) => (p ? `${p} "${c}"` : `"${c.replace('\\', '/')}"`), ''); // CodeQL [SM02383] Replace just the first occurrence of \\ as this could be a UNC path. |
| 88 | const result = await processService.shellExec(cmd, { |
| 89 | timeout: executionTimeout, |
| 90 | maxBuffer: 1000 * 1000, |
| 91 | throwOnStdErr: false, |
| 92 | env: process.env, |
| 93 | shell: defaultShell |
| 94 | }); |
| 95 | if (result.stderr && result.stderr.length) { |
| 96 | logger.error(`Failed to get env vars for shell ${defaultShell} with ${argv} stderr: ${result.stderr}`); |
| 97 | return; |
| 98 | } |
| 99 | try { |
| 100 | // Sometimes when environments get activated, we get a lot of noise in the output. |
| 101 | // Having a separator allows us to filter out the noise. |
| 102 | const output = result.stdout; |
| 103 | return JSON.parse(output.substring(output.indexOf(separator) + separator.length).trim()); |
| 104 | } catch (ex) { |
| 105 | logger.error(`Failed to get env vars for shell ${defaultShell} with ${argv}`, ex); |
| 106 | } |
| 107 | })(); |
| 108 | envVariables.set(key, promise); |
| 109 | return promise; |
| 110 | } |
| 111 | |
| 112 | async function getPythonCli(pythonPath: Uri | undefined) { |
| 113 | const isConda = await isCondaEnvironment(pythonPath); |