Skip to content

Commit f07c8d5

Browse files
committed
Add setting to inject environment variables in terminals
1 parent 11bb928 commit f07c8d5

3 files changed

Lines changed: 40 additions & 1 deletion

File tree

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
"contributes": {
4141
"configuration": {
4242
"properties": {
43+
"python-envs.injectEnvVarsInTerminals": {
44+
"type": "boolean",
45+
"default": false,
46+
"description": "%python-envs.injectEnvVarsInTerminals.description%",
47+
"scope": "window"
48+
},
4349
"python-envs.defaultEnvManager": {
4450
"type": "string",
4551
"description": "%python-envs.defaultEnvManager.description%",

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"python-envs.defaultEnvManager.description": "The default environment manager for creating and managing environments.",
3+
"python-envs.injectEnvVarsInTerminals.description": "This setting defaults to false and toggles if environment variables found in the workspace are set for new terminals.",
34
"python-envs.defaultPackageManager.description": "The default package manager for installing packages in environments.",
45
"python-envs.pythonProjects.description": "The list of Python projects.",
56
"python-envs.pythonProjects.path.description": "The path to a folder or file in the workspace to be treated as a Python project.",

src/features/terminal/terminalManager.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as fsapi from 'fs-extra';
22
import * as path from 'path';
3-
import { Disposable, EventEmitter, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode';
3+
import { Disposable, EventEmitter, ExtensionContext, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode';
44
import { PythonEnvironment, PythonEnvironmentApi, PythonProject, PythonTerminalCreateOptions } from '../../api';
55
import { ActivationStrings } from '../../common/localize';
66
import { traceInfo, traceVerbose } from '../../common/logging';
@@ -84,6 +84,7 @@ export class TerminalManagerImpl implements TerminalManager {
8484
private readonly ta: TerminalActivationInternal,
8585
private readonly startupEnvProviders: ShellEnvsProvider[],
8686
private readonly startupScriptProviders: ShellStartupScriptProvider[],
87+
private readonly context: ExtensionContext,
8788
) {
8889
this.disposables.push(
8990
this.onTerminalOpenedEmitter,
@@ -105,6 +106,7 @@ export class TerminalManagerImpl implements TerminalManager {
105106
env = await getEnvironmentForTerminal(api, t);
106107
}
107108
if (env) {
109+
await this.injectEnvironmentVariables(env);
108110
await this.autoActivateOnTerminalOpen(t, env);
109111
}
110112
}),
@@ -212,6 +214,29 @@ export class TerminalManagerImpl implements TerminalManager {
212214
return ACT_TYPE_COMMAND;
213215
}
214216

217+
private async injectEnvironmentVariables(environment: PythonEnvironment): Promise<void> {
218+
const config = getConfiguration('python-envs');
219+
const inject = config.get<boolean>('injectEnvVarsInTerminals', false);
220+
if (!inject) {
221+
traceInfo('Skipping environment variable injection based on `injectEnvVarsInTerminals` setting.');
222+
return;
223+
}
224+
const api = await getPythonApi();
225+
const project = api.getPythonProject(environment.environmentPath);
226+
if (project) {
227+
const envVars = await api.getEnvironmentVariables(project.uri);
228+
const collection = this.context.environmentVariableCollection;
229+
// clear the env var collection to remove any existing env vars
230+
collection.clear();
231+
for (const [key, value] of Object.entries(envVars)) {
232+
if (value === undefined) {
233+
continue; // Skip undefined values
234+
}
235+
collection.append(key, value);
236+
}
237+
}
238+
}
239+
215240
private async autoActivateOnTerminalOpen(terminal: Terminal, environment: PythonEnvironment): Promise<void> {
216241
let actType = getAutoActivationType();
217242
const shellType = identifyTerminalShell(terminal);
@@ -285,6 +310,7 @@ export class TerminalManagerImpl implements TerminalManager {
285310
// We add it to skip activation on open to prevent double activation.
286311
// We can activate it ourselves since we are creating it.
287312
this.skipActivationOnOpen.add(newTerminal);
313+
await this.injectEnvironmentVariables(environment);
288314
await this.autoActivateOnTerminalOpen(newTerminal, environment);
289315
}
290316

@@ -369,6 +395,12 @@ export class TerminalManagerImpl implements TerminalManager {
369395
}
370396

371397
public async initialize(api: PythonEnvironmentApi): Promise<void> {
398+
for (const t of terminals()) {
399+
const env = this.ta.getEnvironment(t) ?? (await getEnvironmentForTerminal(api, t));
400+
if (env) {
401+
await this.injectEnvironmentVariables(env);
402+
}
403+
}
372404
const actType = getAutoActivationType();
373405
if (actType === ACT_TYPE_COMMAND) {
374406
await Promise.all(terminals().map(async (t) => this.activateUsingCommand(api, t)));

0 commit comments

Comments
 (0)