Skip to content
Open
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
16 changes: 11 additions & 5 deletions src/features/terminal/terminalEnvVarInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ export class TerminalEnvVarInjector implements Disposable {
traceVerbose(
`TerminalEnvVarInjector: Env file injection disabled for workspace: ${workspaceUri.fsPath}`,
);
// Clear any previously set variables when injection is disabled
envVarScope.clear();
Copy link
Contributor

@anthonykim1 anthonykim1 Jan 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you test this with python-envs.terminal.autoActivationType set to shell Startup?
We don't want to clear variables from shell startup as that is what allows auto-activation for shell startup.

My concern is that calling clear on this will also clear activation related variable as well. These are variables with VSCODE_PYTHON_(SHELL)_ACTIVATE

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a potential issue here since when envVarScope.clear() is called on the scoped collection, it clears ALL variables in that workspace scope - not just the .env variables, but also the shell activation variables set by ShellStartupActivationVariablesManager. If you could review and confirm if this is the case or not that would be great!

return;
}

Expand All @@ -165,14 +167,18 @@ export class TerminalEnvVarInjector implements Disposable {
traceVerbose(
`TerminalEnvVarInjector: No .env file found for workspace: ${workspaceUri.fsPath}, not injecting environment variables.`,
);
return; // No .env file to inject
// Clear any previously set variables when no .env file exists
envVarScope.clear();
return;
}

// Clear all previously set variables before re-injecting.
// This ensures that when variables are commented out or removed from .env,
// they are properly removed from the terminal environment.
envVarScope.clear();

for (const [key, value] of Object.entries(envVars)) {
if (value === undefined) {
// Remove the environment variable if the value is undefined
envVarScope.delete(key);
} else {
if (value !== undefined) {
envVarScope.replace(key, value);
}
}
Expand Down