diff --git a/package.json b/package.json index 2349f04b..63b49559 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,12 @@ "contributes": { "configuration": { "properties": { + "python-envs.injectEnvVarsInTerminals": { + "type": "boolean", + "default": false, + "description": "%python-envs.injectEnvVarsInTerminals.description%", + "scope": "window" + }, "python-envs.defaultEnvManager": { "type": "string", "description": "%python-envs.defaultEnvManager.description%", diff --git a/package.nls.json b/package.nls.json index f365cfa4..b302852a 100644 --- a/package.nls.json +++ b/package.nls.json @@ -1,5 +1,6 @@ { "python-envs.defaultEnvManager.description": "The default environment manager for creating and managing environments.", + "python-envs.injectEnvVarsInTerminals.description": "Whether to inject environment variables into terminals on creating new terminals through the environments extension, defaults to false.", "python-envs.defaultPackageManager.description": "The default package manager for installing packages in environments.", "python-envs.pythonProjects.description": "The list of Python projects.", "python-envs.pythonProjects.path.description": "The path to a folder or file in the workspace to be treated as a Python project.", diff --git a/src/features/terminal/terminalManager.ts b/src/features/terminal/terminalManager.ts index bb912647..8751bf18 100644 --- a/src/features/terminal/terminalManager.ts +++ b/src/features/terminal/terminalManager.ts @@ -3,7 +3,7 @@ import * as path from 'path'; import { Disposable, EventEmitter, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode'; import { PythonEnvironment, PythonEnvironmentApi, PythonProject, PythonTerminalCreateOptions } from '../../api'; import { ActivationStrings } from '../../common/localize'; -import { traceInfo, traceVerbose } from '../../common/logging'; +import { traceError, traceInfo, traceVerbose } from '../../common/logging'; import { createTerminal, onDidChangeWindowState, @@ -258,6 +258,38 @@ export class TerminalManagerImpl implements TerminalManager { }); } + const config = getConfiguration('python-envs'); + const isEnvVarsInjectionEnabled = config.get('injectEnvVarsInTerminals', false); + if (isEnvVarsInjectionEnabled) { + // update the environment variables with project specific ones + try { + const api = await getPythonApi(); + const project = api.getPythonProject(environment.environmentPath); + if (project) { + const projectEnvVars = await api.getEnvironmentVariables(project.uri); + if (envVars === undefined) { + // If envVars is undefined, we initialize it to an empty object. + envVars = {}; + } + + for (const [key, value] of Object.entries(projectEnvVars)) { + if (value === undefined) { + // undefined as value means we want to remove the variable + delete envVars[key]; + } else { + envVars[key] = value; + } + } + } else { + traceVerbose(`No project found during terminal creation for ${environment.displayName}`); + } + } catch (ex) { + traceError( + `Failed to get environment variables for project: ${ex}, starting terminal without project environment variables.`, + ); + } + } + // Uncomment the code line below after the issue is resolved: // https://github.com/microsoft/vscode-python-environments/issues/172 // const name = options.name ?? `Python: ${environment.displayName}`;