@@ -22,6 +22,8 @@ import { EnvVarManager } from '../execution/envVariableManager';
2222 */
2323export class TerminalEnvVarInjector implements Disposable {
2424 private disposables : Disposable [ ] = [ ] ;
25+ // Track which .env variables we've set for each workspace to avoid clearing shell activation variables
26+ private envVarKeys : Map < string , Set < string > > = new Map ( ) ;
2527
2628 constructor (
2729 private readonly envVarCollection : GlobalEnvironmentVariableCollection ,
@@ -134,6 +136,8 @@ export class TerminalEnvVarInjector implements Disposable {
134136 */
135137 private async injectEnvironmentVariablesForWorkspace ( workspaceFolder : WorkspaceFolder ) : Promise < void > {
136138 const workspaceUri = workspaceFolder . uri ;
139+ const workspaceKey = workspaceUri . fsPath ;
140+
137141 try {
138142 const envVars = await this . envVarManager . getEnvironmentVariables ( workspaceUri ) ;
139143
@@ -149,8 +153,8 @@ export class TerminalEnvVarInjector implements Disposable {
149153 traceVerbose (
150154 `TerminalEnvVarInjector: Env file injection disabled for workspace: ${ workspaceUri . fsPath } ` ,
151155 ) ;
152- // Clear any previously set variables when injection is disabled
153- envVarScope . clear ( ) ;
156+ // Clear only the .env variables we previously set, not shell activation variables
157+ this . clearTrackedEnvVariables ( envVarScope , workspaceKey ) ;
154158 return ;
155159 }
156160
@@ -167,21 +171,35 @@ export class TerminalEnvVarInjector implements Disposable {
167171 traceVerbose (
168172 `TerminalEnvVarInjector: No .env file found for workspace: ${ workspaceUri . fsPath } , not injecting environment variables.` ,
169173 ) ;
170- // Clear any previously set variables when no .env file exists
171- envVarScope . clear ( ) ;
174+ // Clear only the .env variables we previously set, not shell activation variables
175+ this . clearTrackedEnvVariables ( envVarScope , workspaceKey ) ;
172176 return ;
173177 }
174178
175- // Clear all previously set variables before re-injecting.
179+ // Get previously tracked keys for this workspace
180+ const previousKeys = this . envVarKeys . get ( workspaceKey ) || new Set < string > ( ) ;
181+ const currentKeys = new Set < string > ( ) ;
182+
183+ // Delete variables that were previously set but are no longer in the .env file.
176184 // This ensures that when variables are commented out or removed from .env,
177- // they are properly removed from the terminal environment.
178- envVarScope . clear ( ) ;
185+ // they are properly removed from the terminal environment without affecting
186+ // shell activation variables set by ShellStartupActivationVariablesManager.
187+ for ( const key of previousKeys ) {
188+ if ( ! ( key in envVars ) ) {
189+ envVarScope . delete ( key ) ;
190+ }
191+ }
179192
193+ // Set/update current variables
180194 for ( const [ key , value ] of Object . entries ( envVars ) ) {
181195 if ( value !== undefined ) {
182196 envVarScope . replace ( key , value ) ;
197+ currentKeys . add ( key ) ;
183198 }
184199 }
200+
201+ // Update tracking with current keys
202+ this . envVarKeys . set ( workspaceKey , currentKeys ) ;
185203 } catch ( error ) {
186204 traceError (
187205 `TerminalEnvVarInjector: Error injecting environment variables for workspace ${ workspaceUri . fsPath } :` ,
@@ -218,4 +236,20 @@ export class TerminalEnvVarInjector implements Disposable {
218236 traceError ( `Failed to clear environment variables for workspace ${ workspaceFolder . uri . fsPath } :` , error ) ;
219237 }
220238 }
239+
240+ /**
241+ * Clear only the .env variables we've tracked, not shell activation variables.
242+ */
243+ private clearTrackedEnvVariables (
244+ envVarScope : ReturnType < GlobalEnvironmentVariableCollection [ 'getScoped' ] > ,
245+ workspaceKey : string ,
246+ ) : void {
247+ const trackedKeys = this . envVarKeys . get ( workspaceKey ) ;
248+ if ( trackedKeys ) {
249+ for ( const key of trackedKeys ) {
250+ envVarScope . delete ( key ) ;
251+ }
252+ this . envVarKeys . delete ( workspaceKey ) ;
253+ }
254+ }
221255}
0 commit comments