Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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%",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -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.",
Expand Down
34 changes: 33 additions & 1 deletion src/features/terminal/terminalManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -258,6 +258,38 @@ export class TerminalManagerImpl implements TerminalManager {
});
}

const config = getConfiguration('python-envs');
const isEnvVarsInjectionEnabled = config.get<boolean>('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}`;
Expand Down