@@ -16,7 +16,7 @@ import { getConfiguration, onDidChangeConfiguration } from '../../common/workspa
1616import { isActivatableEnvironment } from '../common/activation' ;
1717import { identifyTerminalShell } from '../common/shellDetector' ;
1818import { getPythonApi } from '../pythonApi' ;
19- import { shellIntegrationForActiveTerminal } from './shells/common/shellUtils' ;
19+ import { isWsl , shellIntegrationForActiveTerminal } from './shells/common/shellUtils' ;
2020import { ShellEnvsProvider , ShellSetupState , ShellStartupScriptProvider } from './shells/startupProvider' ;
2121import { handleSettingUpShellProfile } from './shellStartupSetupHandlers' ;
2222import {
@@ -129,7 +129,9 @@ export class TerminalManagerImpl implements TerminalManager {
129129 await this . handleSetupCheck ( shells ) ;
130130 }
131131 } else {
132- traceVerbose ( `Auto activation type changed to ${ actType } ` ) ;
132+ traceVerbose ( `Auto activation type changed to ${ actType } , we are cleaning up shell startup setup` ) ;
133+ // Teardown scripts when switching away from shell startup activation
134+ await Promise . all ( this . startupScriptProviders . map ( ( p ) => p . teardownScripts ( ) ) ) ;
133135 this . shellSetup . clear ( ) ;
134136 }
135137 }
@@ -143,32 +145,47 @@ export class TerminalManagerImpl implements TerminalManager {
143145 private async handleSetupCheck ( shellType : string | Set < string > ) : Promise < void > {
144146 const shellTypes = typeof shellType === 'string' ? new Set ( [ shellType ] ) : shellType ;
145147 const providers = this . startupScriptProviders . filter ( ( p ) => shellTypes . has ( p . shellType ) ) ;
146- if ( providers . length > 0 ) {
148+ if ( providers . length > 0 ) {
147149 const shellsToSetup : ShellStartupScriptProvider [ ] = [ ] ;
148150 await Promise . all (
149151 providers . map ( async ( p ) => {
152+ const state = await p . isSetup ( ) ;
153+ const currentSetup = ( state === ShellSetupState . Setup ) ;
154+ // Check if we already processed this shell and the state hasn't changed
150155 if ( this . shellSetup . has ( p . shellType ) ) {
151- traceVerbose ( `Shell profile for ${ p . shellType } already checked.` ) ;
152- return ;
156+ const cachedSetup = this . shellSetup . get ( p . shellType ) ;
157+ if ( currentSetup === cachedSetup ) {
158+ traceVerbose ( `Shell profile for ${ p . shellType } already checked, state unchanged.` ) ;
159+ return ;
160+ }
161+ traceVerbose ( `Shell profile for ${ p . shellType } state changed from ${ cachedSetup } to ${ currentSetup } , re-evaluating.` ) ;
153162 }
154163 traceVerbose ( `Checking shell profile for ${ p . shellType } .` ) ;
155- const state = await p . isSetup ( ) ;
156164 if ( state === ShellSetupState . NotSetup ) {
157- // Check if shell integration is available before marking for setup
158- if ( shellIntegrationForActiveTerminal ( p . name ) ) {
165+ traceVerbose ( `WSL detected: ${ isWsl ( ) } , Shell integration available: ${ shellIntegrationForActiveTerminal ( p . name ) } ` ) ;
166+
167+ if ( shellIntegrationForActiveTerminal ( p . name ) && ! isWsl ( ) ) {
168+ // Shell integration available and NOT in WSL - skip setup
169+ await p . teardownScripts ( ) ;
159170 this . shellSetup . set ( p . shellType , true ) ;
160171 traceVerbose (
161- `Shell integration available for ${ p . shellType } , skipping prompt, and profile modification.` ,
172+ `Shell integration available for ${ p . shellType } (not WSL) , skipping prompt, and profile modification.` ,
162173 ) ;
163174 } else {
164- // No shell integration, mark for setup
175+ // WSL (regardless of integration) OR no shell integration - needs setup
165176 this . shellSetup . set ( p . shellType , false ) ;
166177 shellsToSetup . push ( p ) ;
167178 traceVerbose (
168- `Shell integration is NOT avaoiable . Shell profile for ${ p . shellType } is not setup.` ,
179+ `Shell integration is NOT available . Shell profile for ${ p . shellType } is not setup.` ,
169180 ) ;
170181 }
171182 } else if ( state === ShellSetupState . Setup ) {
183+ if ( shellIntegrationForActiveTerminal ( p . name ) && ! isWsl ( ) ) {
184+ await p . teardownScripts ( ) ;
185+ traceVerbose (
186+ `Shell integration available for ${ p . shellType } , removed profile script in favor of shell integration.` ,
187+ ) ;
188+ }
172189 this . shellSetup . set ( p . shellType , true ) ;
173190 traceVerbose ( `Shell profile for ${ p . shellType } is setup.` ) ;
174191 } else if ( state === ShellSetupState . NotInstalled ) {
@@ -228,6 +245,7 @@ export class TerminalManagerImpl implements TerminalManager {
228245 let actType = getAutoActivationType ( ) ;
229246 const shellType = identifyTerminalShell ( terminal ) ;
230247 if ( actType === ACT_TYPE_SHELL ) {
248+ await this . handleSetupCheck ( shellType ) ;
231249 actType = await this . getEffectiveActivationType ( shellType ) ;
232250 }
233251
0 commit comments