Skip to content

Commit dfdf29f

Browse files
authored
ShellStartup autoActivation on CMD does not work (#1018)
Resolves: #1000 Allow cmd to work again with shell startup. Also make sure to switch activation method to command if user declines modification (if applicable) when trying to switch to shell startup. Should add tests covering various scenarios with #1019
1 parent 4e34705 commit dfdf29f

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-11
lines changed

src/features/terminal/shellStartupSetupHandlers.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ export async function handleSettingUpShellProfile(
5555
});
5656
providers.forEach((provider) => callback(provider, false));
5757
}
58+
} else {
59+
traceInfo(`User declined shell profile setup for ${shells}, switching to command activation`);
60+
await Promise.all(providers.map((provider) => provider.teardownScripts()));
61+
await setAutoActivationType(ACT_TYPE_COMMAND);
5862
}
5963
}
6064

src/features/terminal/shells/common/shellUtils.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,23 @@ export async function getShellIntegrationEnabledCache(): Promise<boolean> {
161161
await persistentState.set(SHELL_INTEGRATION_STATE_KEY, shellIntegrationEnabled);
162162
return shellIntegrationEnabled;
163163
}
164+
165+
// Shells that support shell integration way of environment activation.
166+
// CMD is not listed here, but we still want to support activation via profile modification.
167+
export const shellIntegrationSupportedShells = [
168+
ShellConstants.PWSH,
169+
ShellConstants.BASH,
170+
ShellConstants.GITBASH,
171+
ShellConstants.FISH,
172+
ShellConstants.ZSH,
173+
];
174+
175+
/**
176+
* Determines whether profile-based activation should be used instead of shell integration.
177+
* Profile activation is preferred when:
178+
* - Running in WSL
179+
* - The shell type doesn't support shell integration (e.g., cmd)
180+
*/
181+
export function shouldUseProfileActivation(shellType: string): boolean {
182+
return isWsl() || !shellIntegrationSupportedShells.includes(shellType);
183+
}

src/features/terminal/terminalManager.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ import { getConfiguration, onDidChangeConfiguration } from '../../common/workspa
1616
import { isActivatableEnvironment } from '../common/activation';
1717
import { identifyTerminalShell } from '../common/shellDetector';
1818
import { getPythonApi } from '../pythonApi';
19-
import { getShellIntegrationEnabledCache, isWsl, shellIntegrationForActiveTerminal } from './shells/common/shellUtils';
19+
import {
20+
getShellIntegrationEnabledCache,
21+
isWsl,
22+
shellIntegrationForActiveTerminal,
23+
shouldUseProfileActivation,
24+
} from './shells/common/shellUtils';
2025
import { ShellEnvsProvider, ShellSetupState, ShellStartupScriptProvider } from './shells/startupProvider';
2126
import { handleSettingUpShellProfile } from './shellStartupSetupHandlers';
2227
import {
@@ -166,19 +171,20 @@ export class TerminalManagerImpl implements TerminalManager {
166171
await Promise.all(
167172
providers.map(async (p) => {
168173
const state = await p.isSetup();
169-
const shellIntegrationEnabled = await getShellIntegrationEnabledCache();
174+
const shellIntegrationEnabledSetting = await getShellIntegrationEnabledCache();
175+
const shellIntegrationActiveTerminal = await shellIntegrationForActiveTerminal(p.name);
176+
const shellIntegrationLikelyAvailable =
177+
shellIntegrationEnabledSetting || shellIntegrationActiveTerminal;
170178
traceVerbose(`Checking shell profile for ${p.shellType}, with state: ${state}`);
179+
171180
if (state === ShellSetupState.NotSetup) {
172181
traceVerbose(
173-
`WSL detected: ${isWsl()}, Shell integration available from setting, or active terminal: ${shellIntegrationEnabled}, or ${await shellIntegrationForActiveTerminal(
182+
`WSL detected: ${isWsl()}, Shell integration available from setting, or active terminal: ${shellIntegrationEnabledSetting}, or ${await shellIntegrationForActiveTerminal(
174183
p.name,
175184
)}`,
176185
);
177186

178-
if (
179-
(shellIntegrationEnabled || (await shellIntegrationForActiveTerminal(p.name))) &&
180-
!isWsl()
181-
) {
187+
if (shellIntegrationLikelyAvailable && !shouldUseProfileActivation(p.shellType)) {
182188
// Shell integration available and NOT in WSL - skip setup
183189
await p.teardownScripts();
184190
this.shellSetup.set(p.shellType, true);
@@ -194,10 +200,7 @@ export class TerminalManagerImpl implements TerminalManager {
194200
);
195201
}
196202
} else if (state === ShellSetupState.Setup) {
197-
if (
198-
(shellIntegrationEnabled || (await shellIntegrationForActiveTerminal(p.name))) &&
199-
!isWsl()
200-
) {
203+
if (shellIntegrationLikelyAvailable && !shouldUseProfileActivation(p.shellType)) {
201204
await p.teardownScripts();
202205
traceVerbose(
203206
`Shell integration available for ${p.shellType}, removed profile script in favor of shell integration.`,

0 commit comments

Comments
 (0)