@@ -17,7 +17,7 @@ import {
1717 PROFILE_TAG_START ,
1818 shellIntegrationForActiveTerminal ,
1919} from '../common/shellUtils' ;
20- import { POWERSHELL_ENV_KEY , PWSH_SCRIPT_VERSION } from './pwshConstants' ;
20+ import { POWERSHELL_ENV_KEY , POWERSHELL_OLD_ENV_KEY , PWSH_SCRIPT_VERSION } from './pwshConstants' ;
2121
2222const PWSH_PROFILE_PATH_CACHE_KEY = 'PWSH_PROFILE_PATH_CACHE' ;
2323const PS5_PROFILE_PATH_CACHE_KEY = 'PS5_PROFILE_PATH_CACHE' ;
@@ -146,7 +146,8 @@ async function isPowerShellStartupSetup(shell: string, profile: string): Promise
146146
147147async function setupPowerShellStartup ( shell : string , profile : string ) : Promise < boolean > {
148148 if ( shellIntegrationForActiveTerminal ( shell , profile ) ) {
149- removePowerShellStartup ( shell , profile ) ;
149+ removePowerShellStartup ( shell , profile , POWERSHELL_OLD_ENV_KEY ) ;
150+ removePowerShellStartup ( shell , profile , POWERSHELL_ENV_KEY ) ;
150151 return true ;
151152 }
152153 const activationContent = getActivationContent ( ) ;
@@ -172,22 +173,22 @@ async function setupPowerShellStartup(shell: string, profile: string): Promise<b
172173 }
173174}
174175
175- async function removePowerShellStartup ( shell : string , profile : string ) : Promise < boolean > {
176+ async function removePowerShellStartup ( shell : string , profile : string , key : string ) : Promise < boolean > {
176177 if ( ! ( await fs . pathExists ( profile ) ) ) {
177178 return true ;
178179 }
179180
180181 try {
181182 const content = await fs . readFile ( profile , 'utf8' ) ;
182- if ( hasStartupCode ( content , regionStart , regionEnd , [ POWERSHELL_ENV_KEY ] ) ) {
183+ if ( hasStartupCode ( content , regionStart , regionEnd , [ key ] ) ) {
183184 await fs . writeFile ( profile , removeStartupCode ( content , regionStart , regionEnd ) ) ;
184- traceInfo ( `SHELL: Removed activation from ${ shell } profile at: ${ profile } ` ) ;
185+ traceInfo ( `SHELL: Removed activation from ${ shell } profile at: ${ profile } , for key: ${ key } ` ) ;
185186 } else {
186- traceInfo ( `SHELL: No activation code found in ${ shell } profile at: ${ profile } ` ) ;
187+ traceInfo ( `SHELL: No activation code found in ${ shell } profile at: ${ profile } , for key: ${ key } ` ) ;
187188 }
188189 return true ;
189190 } catch ( err ) {
190- traceError ( `SHELL: Failed to remove startup code for ${ shell } profile at: ${ profile } ` , err ) ;
191+ traceError ( `SHELL: Failed to remove startup code for ${ shell } profile at: ${ profile } , for key: ${ key } ` , err ) ;
191192 return false ;
192193 }
193194}
@@ -302,7 +303,9 @@ export class PwshStartupProvider implements ShellStartupScriptProvider {
302303
303304 try {
304305 const profile = await getProfileForShell ( shell ) ;
305- const success = await removePowerShellStartup ( shell , profile ) ;
306+ // Remove old environment variable if it exists
307+ await removePowerShellStartup ( shell , profile , POWERSHELL_OLD_ENV_KEY ) ;
308+ const success = await removePowerShellStartup ( shell , profile , POWERSHELL_ENV_KEY ) ;
306309 anyEdited . push ( success ? ShellScriptEditState . Edited : ShellScriptEditState . NotEdited ) ;
307310 } catch ( err ) {
308311 traceError ( `Failed to remove ${ shell } startup` , err ) ;
0 commit comments