diff --git a/src/features/terminal/terminalEnvVarInjector.ts b/src/features/terminal/terminalEnvVarInjector.ts index 039c4b3c..aeb1f97b 100644 --- a/src/features/terminal/terminalEnvVarInjector.ts +++ b/src/features/terminal/terminalEnvVarInjector.ts @@ -22,6 +22,8 @@ import { EnvVarManager } from '../execution/envVariableManager'; */ export class TerminalEnvVarInjector implements Disposable { private disposables: Disposable[] = []; + // Track which .env variables we've set for each workspace to avoid clearing shell activation variables + private envVarKeys: Map> = new Map(); constructor( private readonly envVarCollection: GlobalEnvironmentVariableCollection, @@ -134,6 +136,8 @@ export class TerminalEnvVarInjector implements Disposable { */ private async injectEnvironmentVariablesForWorkspace(workspaceFolder: WorkspaceFolder): Promise { const workspaceUri = workspaceFolder.uri; + const workspaceKey = workspaceUri.fsPath; + try { const envVars = await this.envVarManager.getEnvironmentVariables(workspaceUri); @@ -149,6 +153,8 @@ export class TerminalEnvVarInjector implements Disposable { traceVerbose( `TerminalEnvVarInjector: Env file injection disabled for workspace: ${workspaceUri.fsPath}`, ); + // Clear only the .env variables we previously set, not shell activation variables + this.clearTrackedEnvVariables(envVarScope, workspaceKey); return; } @@ -165,17 +171,35 @@ 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 only the .env variables we previously set, not shell activation variables + this.clearTrackedEnvVariables(envVarScope, workspaceKey); + return; } - for (const [key, value] of Object.entries(envVars)) { - if (value === undefined) { - // Remove the environment variable if the value is undefined + // Get previously tracked keys for this workspace + const previousKeys = this.envVarKeys.get(workspaceKey) || new Set(); + const currentKeys = new Set(); + + // Delete variables that were previously set but are no longer in the .env file. + // This ensures that when variables are commented out or removed from .env, + // they are properly removed from the terminal environment without affecting + // shell activation variables set by ShellStartupActivationVariablesManager. + for (const key of previousKeys) { + if (!(key in envVars)) { envVarScope.delete(key); - } else { + } + } + + // Set/update current variables + for (const [key, value] of Object.entries(envVars)) { + if (value !== undefined) { envVarScope.replace(key, value); + currentKeys.add(key); } } + + // Update tracking with current keys + this.envVarKeys.set(workspaceKey, currentKeys); } catch (error) { traceError( `TerminalEnvVarInjector: Error injecting environment variables for workspace ${workspaceUri.fsPath}:`, @@ -212,4 +236,20 @@ export class TerminalEnvVarInjector implements Disposable { traceError(`Failed to clear environment variables for workspace ${workspaceFolder.uri.fsPath}:`, error); } } + + /** + * Clear only the .env variables we've tracked, not shell activation variables. + */ + private clearTrackedEnvVariables( + envVarScope: ReturnType, + workspaceKey: string, + ): void { + const trackedKeys = this.envVarKeys.get(workspaceKey); + if (trackedKeys) { + for (const key of trackedKeys) { + envVarScope.delete(key); + } + this.envVarKeys.delete(workspaceKey); + } + } }