Skip to content

Commit aa4c490

Browse files
authored
Merge branch 'main' into zsol/jj-xxwzpntmsuom
2 parents cad0567 + 15d364d commit aa4c490

File tree

4 files changed

+46
-12
lines changed

4 files changed

+46
-12
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/cmd/cmdStartup.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import * as path from 'path';
55
import { promisify } from 'util';
66
import which from 'which';
77
import { traceError, traceInfo, traceVerbose } from '../../../../common/logging';
8+
import { StopWatch } from '../../../../common/stopWatch';
89
import { isWindows } from '../../../../common/utils/platformUtils';
910
import { ShellConstants } from '../../../common/shellConstants';
1011
import { hasStartupCode, insertStartupCode, removeStartupCode } from '../common/editUtils';
1112
import { ShellScriptEditState, ShellSetupState, ShellStartupScriptProvider } from '../startupProvider';
1213
import { CMD_ENV_KEY, CMD_SCRIPT_VERSION } from './cmdConstants';
13-
import { StopWatch } from '../../../../common/stopWatch';
1414

1515
function execCommand(command: string) {
1616
const timer = new StopWatch();
@@ -222,6 +222,13 @@ async function setupCmdStartup(cmdFiles: CmdFilePaths, key: string): Promise<boo
222222
`SHELL: Updated existing main batch file at: ${cmdFiles.mainBatchFile}\r\n${mainBatchContent}`,
223223
);
224224
}
225+
} else {
226+
const mainBatchContent = getMainBatchFileContent(cmdFiles.regStartupFile);
227+
await fs.writeFile(
228+
cmdFiles.mainBatchFile,
229+
insertStartupCode(getHeader(), regionStart, regionEnd, mainBatchContent),
230+
);
231+
traceInfo(`SHELL: Created new main batch file at: ${cmdFiles.mainBatchFile}\r\n${mainBatchContent}`);
225232
}
226233

227234
// Step 4: Setup registry AutoRun to call our main batch file

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)