11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License.
33
4- import * as path from 'path' ;
54import * as fsapi from 'fs-extra' ;
6- import { Disposable , Uri , workspace , GlobalEnvironmentVariableCollection } from 'vscode' ;
7- import { traceVerbose , traceError } from '../../common/logging' ;
5+ import * as path from 'path' ;
6+ import {
7+ Disposable ,
8+ EnvironmentVariableScope ,
9+ GlobalEnvironmentVariableCollection ,
10+ workspace ,
11+ WorkspaceFolder ,
12+ } from 'vscode' ;
13+ import { traceError , traceVerbose } from '../../common/logging' ;
14+ import { resolveVariables } from '../../common/utils/internalVariables' ;
815import { getConfiguration , onDidChangeConfiguration } from '../../common/workspace.apis' ;
916import { EnvVarManager } from '../execution/envVariableManager' ;
1017
@@ -58,6 +65,7 @@ export class TerminalEnvVarInjector implements Disposable {
5865 * Update environment variables in the terminal collection.
5966 */
6067 private async updateEnvironmentVariables ( ) : Promise < void > {
68+ // every time ONE is changed, we change ALL of them (not sure this is the right approach)
6169 try {
6270 // Clear existing environment variables
6371 traceVerbose ( 'TerminalEnvVarInjector: Clearing existing environment variables' ) ;
@@ -72,7 +80,7 @@ export class TerminalEnvVarInjector implements Disposable {
7280
7381 // Process environment variables for each workspace folder
7482 for ( const folder of workspaceFolders ) {
75- await this . injectEnvironmentVariablesForWorkspace ( folder . uri ) ;
83+ await this . injectEnvironmentVariablesForWorkspace ( folder ) ;
7684 }
7785
7886 traceVerbose ( 'TerminalEnvVarInjector: Environment variable injection completed' ) ;
@@ -84,17 +92,20 @@ export class TerminalEnvVarInjector implements Disposable {
8492 /**
8593 * Inject environment variables for a specific workspace.
8694 */
87- private async injectEnvironmentVariablesForWorkspace ( workspaceUri : Uri ) : Promise < void > {
95+ private async injectEnvironmentVariablesForWorkspace ( workspaceFolder : WorkspaceFolder ) : Promise < void > {
96+ const workspaceUri = workspaceFolder . uri ;
8897 try {
8998 traceVerbose ( `TerminalEnvVarInjector: Processing workspace: ${ workspaceUri . fsPath } ` ) ;
9099
91100 // Get environment variables for this workspace
92101 const envVars = await this . envVarManager . getEnvironmentVariables ( workspaceUri ) ;
93102
94103 // Track which .env file is being used for logging
95- const config = getConfiguration ( 'python' , workspaceUri ) ;
104+ const config = getConfiguration ( 'python' , workspaceUri ) ; // why did this get .env file?? // returns like all of them
96105 const envFilePath = config . get < string > ( 'envFile' ) ;
97- const resolvedEnvFilePath = envFilePath ? path . resolve ( envFilePath ) : undefined ;
106+ const resolvedEnvFilePath = envFilePath
107+ ? path . resolve ( resolveVariables ( envFilePath , workspaceUri ) )
108+ : undefined ;
98109 const defaultEnvFilePath = path . join ( workspaceUri . fsPath , '.env' ) ;
99110
100111 let activeEnvFilePath : string | undefined ;
@@ -106,17 +117,21 @@ export class TerminalEnvVarInjector implements Disposable {
106117 traceVerbose ( `TerminalEnvVarInjector: Using default .env file: ${ activeEnvFilePath } ` ) ;
107118 } else {
108119 traceVerbose ( `TerminalEnvVarInjector: No .env file found for workspace: ${ workspaceUri . fsPath } ` ) ;
120+ return ; // No .env file to inject
109121 }
110122
111123 // Inject environment variables into the collection
112124 let injectedCount = 0 ;
113125 for ( const [ key , value ] of Object . entries ( envVars ) ) {
114- if ( value !== undefined && value !== process . env [ key ] ) {
115- // Only inject if the value is different from the current process environment
116- this . envVarCollection . replace ( key , value ) ;
117- injectedCount ++ ;
118- traceVerbose ( `TerminalEnvVarInjector: Injected ${ key } =${ value } ` ) ;
126+ // inject into correctly scoped environment collection
127+ const envVarScope = this . getEnvironmentVariableCollectionScoped ( { workspaceFolder } ) ;
128+ if ( value === undefined ) {
129+ // Remove the environment variable if the value is undefined
130+ envVarScope . delete ( key ) ;
131+ } else {
132+ envVarScope . replace ( key , value ) ;
119133 }
134+ injectedCount ++ ;
120135 }
121136
122137 if ( injectedCount > 0 ) {
@@ -143,8 +158,13 @@ export class TerminalEnvVarInjector implements Disposable {
143158 traceVerbose ( 'TerminalEnvVarInjector: Disposing' ) ;
144159 this . disposables . forEach ( ( disposable ) => disposable . dispose ( ) ) ;
145160 this . disposables = [ ] ;
146-
161+
147162 // Clear all environment variables from the collection
148163 this . envVarCollection . clear ( ) ;
149164 }
150- }
165+
166+ private getEnvironmentVariableCollectionScoped ( scope : EnvironmentVariableScope = { } ) {
167+ const envVarCollection = this . envVarCollection as GlobalEnvironmentVariableCollection ;
168+ return envVarCollection . getScoped ( scope ) ;
169+ }
170+ }
0 commit comments