Skip to content

Commit c7183a2

Browse files
committed
update to only inject on create
1 parent 3c7a7c4 commit c7183a2

3 files changed

Lines changed: 35 additions & 36 deletions

File tree

package.nls.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +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.",
3+
"python-envs.injectEnvVarsInTerminals.description": "Whether to inject environment variables into terminals on creating new terminals through the environments extension, defaults to false.",
44
"python-envs.defaultPackageManager.description": "The default package manager for installing packages in environments.",
55
"python-envs.pythonProjects.description": "The list of Python projects.",
66
"python-envs.pythonProjects.path.description": "The path to a folder or file in the workspace to be treated as a Python project.",

src/extension.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
205205
terminalActivation,
206206
shellEnvsProviders,
207207
shellStartupProviders,
208-
context,
209208
);
210209
context.subscriptions.push(terminalActivation, terminalManager);
211210

src/features/terminal/terminalManager.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as fsapi from 'fs-extra';
22
import * as path from 'path';
3-
import { Disposable, EventEmitter, ExtensionContext, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode';
3+
import { Disposable, EventEmitter, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode';
44
import { PythonEnvironment, PythonEnvironmentApi, PythonProject, PythonTerminalCreateOptions } from '../../api';
55
import { ActivationStrings } from '../../common/localize';
6-
import { traceInfo, traceVerbose } from '../../common/logging';
6+
import { traceError, traceInfo, traceVerbose } from '../../common/logging';
77
import {
88
createTerminal,
99
onDidChangeWindowState,
@@ -84,7 +84,6 @@ 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,
8887
) {
8988
this.disposables.push(
9089
this.onTerminalOpenedEmitter,
@@ -106,7 +105,6 @@ export class TerminalManagerImpl implements TerminalManager {
106105
env = await getEnvironmentForTerminal(api, t);
107106
}
108107
if (env) {
109-
await this.injectEnvironmentVariables(env);
110108
await this.autoActivateOnTerminalOpen(t, env);
111109
}
112110
}),
@@ -214,29 +212,6 @@ export class TerminalManagerImpl implements TerminalManager {
214212
return ACT_TYPE_COMMAND;
215213
}
216214

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-
240215
private async autoActivateOnTerminalOpen(terminal: Terminal, environment: PythonEnvironment): Promise<void> {
241216
let actType = getAutoActivationType();
242217
const shellType = identifyTerminalShell(terminal);
@@ -283,6 +258,38 @@ export class TerminalManagerImpl implements TerminalManager {
283258
});
284259
}
285260

261+
const config = getConfiguration('python-envs');
262+
const isEnvVarsInjectionEnabled = config.get<boolean>('injectEnvVarsInTerminals', false);
263+
if (isEnvVarsInjectionEnabled) {
264+
// update the environment variables with project specific ones
265+
try {
266+
const api = await getPythonApi();
267+
const project = api.getPythonProject(environment.environmentPath);
268+
if (project) {
269+
const projectEnvVars = await api.getEnvironmentVariables(project.uri);
270+
if (envVars === undefined) {
271+
// If envVars is undefined, we initialize it to an empty object.
272+
envVars = {};
273+
}
274+
275+
for (const [key, value] of Object.entries(projectEnvVars)) {
276+
if (value === undefined) {
277+
// undefined as value means we want to remove the variable
278+
delete envVars[key];
279+
} else {
280+
envVars[key] = value;
281+
}
282+
}
283+
} else {
284+
traceVerbose(`No project found during terminal creation for ${environment.displayName}`);
285+
}
286+
} catch (ex) {
287+
traceError(
288+
`Failed to get environment variables for project: ${ex}, starting terminal without project environment variables.`,
289+
);
290+
}
291+
}
292+
286293
// Uncomment the code line below after the issue is resolved:
287294
// https://github.com/microsoft/vscode-python-environments/issues/172
288295
// const name = options.name ?? `Python: ${environment.displayName}`;
@@ -310,7 +317,6 @@ export class TerminalManagerImpl implements TerminalManager {
310317
// We add it to skip activation on open to prevent double activation.
311318
// We can activate it ourselves since we are creating it.
312319
this.skipActivationOnOpen.add(newTerminal);
313-
await this.injectEnvironmentVariables(environment);
314320
await this.autoActivateOnTerminalOpen(newTerminal, environment);
315321
}
316322

@@ -395,12 +401,6 @@ export class TerminalManagerImpl implements TerminalManager {
395401
}
396402

397403
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-
}
404404
const actType = getAutoActivationType();
405405
if (actType === ACT_TYPE_COMMAND) {
406406
await Promise.all(terminals().map(async (t) => this.activateUsingCommand(api, t)));

0 commit comments

Comments
 (0)