@@ -12,7 +12,7 @@ import {
1212} from 'vscode' ;
1313import { traceError , traceVerbose } from '../../common/logging' ;
1414import { resolveVariables } from '../../common/utils/internalVariables' ;
15- import { getConfiguration , onDidChangeConfiguration } from '../../common/workspace.apis' ;
15+ import { getConfiguration , getWorkspaceFolder } from '../../common/workspace.apis' ;
1616import { EnvVarManager } from '../execution/envVariableManager' ;
1717
1818/**
@@ -35,25 +35,34 @@ export class TerminalEnvVarInjector implements Disposable {
3535 private async initialize ( ) : Promise < void > {
3636 traceVerbose ( 'TerminalEnvVarInjector: Initializing environment variable injection' ) ;
3737
38- // Listen for configuration changes to python.envFile setting
38+ // Listen for environment variable changes from the manager
3939 this . disposables . push (
40- onDidChangeConfiguration ( ( e ) => {
41- if ( e . affectsConfiguration ( 'python.envFile' ) ) {
42- traceVerbose ( 'TerminalEnvVarInjector: python.envFile setting changed, reloading env vars' ) ;
40+ this . envVarManager . onDidChangeEnvironmentVariables ( ( args ) => {
41+ if ( ! args . uri ) {
42+ // No specific URI, reload all workspaces
4343 this . updateEnvironmentVariables ( ) . catch ( ( error ) => {
44- traceError ( 'TerminalEnvVarInjector: Error updating env vars after setting change :' , error ) ;
44+ traceError ( 'Failed to update environment variables :' , error ) ;
4545 } ) ;
46+ return ;
4647 }
47- } ) ,
48- ) ;
4948
50- // Listen for environment variable changes from the manager
51- this . disposables . push (
52- this . envVarManager . onDidChangeEnvironmentVariables ( ( ) => {
53- traceVerbose ( 'TerminalEnvVarInjector: Environment variables changed, reloading' ) ;
54- this . updateEnvironmentVariables ( ) . catch ( ( error ) => {
55- traceError ( 'TerminalEnvVarInjector: Error updating env vars after change event:' , error ) ;
56- } ) ;
49+ const affectedWorkspace = getWorkspaceFolder ( args . uri ) ;
50+ if ( ! affectedWorkspace ) {
51+ // No workspace folder found for this URI, reloading all workspaces
52+ this . updateEnvironmentVariables ( ) . catch ( ( error ) => {
53+ traceError ( 'Failed to update environment variables:' , error ) ;
54+ } ) ;
55+ return ;
56+ }
57+
58+ if ( args . changeType === 2 ) {
59+ // FileChangeType.Deleted
60+ this . clearWorkspaceVariables ( affectedWorkspace ) ;
61+ } else {
62+ this . updateEnvironmentVariables ( affectedWorkspace ) . catch ( ( error ) => {
63+ traceError ( 'Failed to update environment variables:' , error ) ;
64+ } ) ;
65+ }
5766 } ) ,
5867 ) ;
5968
@@ -64,23 +73,29 @@ export class TerminalEnvVarInjector implements Disposable {
6473 /**
6574 * Update environment variables in the terminal collection.
6675 */
67- private async updateEnvironmentVariables ( ) : Promise < void > {
68- // every time ONE is changed, we change ALL of them (not sure this is the right approach)
76+ private async updateEnvironmentVariables ( workspaceFolder ?: WorkspaceFolder ) : Promise < void > {
6977 try {
70- // Clear existing environment variables
71- traceVerbose ( 'TerminalEnvVarInjector: Clearing existing environment variables' ) ;
72- this . envVarCollection . clear ( ) ;
73-
74- // Get environment variables for all workspace folders
75- const workspaceFolders = workspace . workspaceFolders ;
76- if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
77- traceVerbose ( 'TerminalEnvVarInjector: No workspace folders found, skipping env var injection' ) ;
78- return ;
79- }
78+ if ( workspaceFolder ) {
79+ // Update only the specified workspace
80+ traceVerbose (
81+ `TerminalEnvVarInjector: Updating environment variables for workspace: ${ workspaceFolder . uri . fsPath } ` ,
82+ ) ;
83+ await this . injectEnvironmentVariablesForWorkspace ( workspaceFolder ) ;
84+ } else {
85+ // Initial load - update all workspaces
86+ traceVerbose ( 'TerminalEnvVarInjector: Clearing existing environment variables for initial load' ) ;
87+ this . envVarCollection . clear ( ) ;
88+
89+ const workspaceFolders = workspace . workspaceFolders ;
90+ if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
91+ traceVerbose ( 'TerminalEnvVarInjector: No workspace folders found, skipping env var injection' ) ;
92+ return ;
93+ }
8094
81- // Process environment variables for each workspace folder
82- for ( const folder of workspaceFolders ) {
83- await this . injectEnvironmentVariablesForWorkspace ( folder ) ;
95+ traceVerbose ( 'TerminalEnvVarInjector: Updating environment variables for all workspaces' ) ;
96+ for ( const folder of workspaceFolders ) {
97+ await this . injectEnvironmentVariablesForWorkspace ( folder ) ;
98+ }
8499 }
85100
86101 traceVerbose ( 'TerminalEnvVarInjector: Environment variable injection completed' ) ;
@@ -95,9 +110,6 @@ export class TerminalEnvVarInjector implements Disposable {
95110 private async injectEnvironmentVariablesForWorkspace ( workspaceFolder : WorkspaceFolder ) : Promise < void > {
96111 const workspaceUri = workspaceFolder . uri ;
97112 try {
98- traceVerbose ( `TerminalEnvVarInjector: Processing workspace: ${ workspaceUri . fsPath } ` ) ;
99-
100- // Get environment variables for this workspace
101113 const envVars = await this . envVarManager . getEnvironmentVariables ( workspaceUri ) ;
102114
103115 // Track which .env file is being used for logging
@@ -120,28 +132,18 @@ export class TerminalEnvVarInjector implements Disposable {
120132 return ; // No .env file to inject
121133 }
122134
135+ const envVarScope = this . getEnvironmentVariableCollectionScoped ( { workspaceFolder } ) ;
136+ envVarScope . clear ( ) ; // Clear existing variables for this workspace
137+
123138 // Inject environment variables into the collection
124- let injectedCount = 0 ;
125139 for ( const [ key , value ] of Object . entries ( envVars ) ) {
126140 // inject into correctly scoped environment collection
127- const envVarScope = this . getEnvironmentVariableCollectionScoped ( { workspaceFolder } ) ;
128141 if ( value === undefined ) {
129142 // Remove the environment variable if the value is undefined
130143 envVarScope . delete ( key ) ;
131144 } else {
132145 envVarScope . replace ( key , value ) ;
133146 }
134- injectedCount ++ ;
135- }
136-
137- if ( injectedCount > 0 ) {
138- traceVerbose (
139- `TerminalEnvVarInjector: Injected ${ injectedCount } environment variables for workspace: ${ workspaceUri . fsPath } ` ,
140- ) ;
141- } else {
142- traceVerbose (
143- `TerminalEnvVarInjector: No environment variables to inject for workspace: ${ workspaceUri . fsPath } ` ,
144- ) ;
145147 }
146148 } catch ( error ) {
147149 traceError (
@@ -167,4 +169,16 @@ export class TerminalEnvVarInjector implements Disposable {
167169 const envVarCollection = this . envVarCollection as GlobalEnvironmentVariableCollection ;
168170 return envVarCollection . getScoped ( scope ) ;
169171 }
172+
173+ /**
174+ * Clear all environment variables for a workspace.
175+ */
176+ private clearWorkspaceVariables ( workspaceFolder : WorkspaceFolder ) : void {
177+ try {
178+ const scope = this . getEnvironmentVariableCollectionScoped ( { workspaceFolder } ) ;
179+ scope . clear ( ) ;
180+ } catch ( error ) {
181+ traceError ( `Failed to clear environment variables for workspace ${ workspaceFolder . uri . fsPath } :` , error ) ;
182+ }
183+ }
170184}
0 commit comments