Skip to content

Commit 502cf21

Browse files
authored
Detect shells ending in .exe (for Windows/Cygwin) (#17431)
* Detect shells ending in .exe (for Windows/Cygwin) Strip `.exe` extension before detecting shells, so all detections work in Windows / Cygwin. Fixes part of #17426. * Add news item * Add tests Fix #17426
1 parent d6b16b1 commit 502cf21

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

news/2 Fixes/17426.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve pattern matching for shell detection on Windows.
2+
(thanks [Erik Demaine](https://github.com/edemaine/))

src/client/common/terminal/shellDetector.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ export class ShellDetector {
2929
/**
3030
* Logic is as follows:
3131
* 1. Try to identify the type of the shell based on the name of the terminal.
32-
* 2. Try to identify the type of the shell based on the usettigs in VSC.
32+
* 2. Try to identify the type of the shell based on the settings in VSC.
3333
* 3. Try to identify the type of the shell based on the user environment (OS).
3434
* 4. If all else fail, use defaults hardcoded (cmd for windows, bash for linux & mac).
35-
* More information here See solution here https://github.com/microsoft/vscode/issues/74233#issuecomment-497527337
35+
* More information here: https://github.com/microsoft/vscode/issues/74233#issuecomment-497527337
3636
*
3737
* @param {Terminal} [terminal]
3838
* @returns {TerminalShellType}

src/client/common/terminal/shellDetectors/baseShellDetector.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ When identifying the shell use the following algorithm:
1919

2020
// Types of shells can be found here:
2121
// 1. https://wiki.ubuntu.com/ChangingShells
22-
const IS_GITBASH = /(gitbash.exe$)/i;
23-
const IS_BASH = /(bash.exe$|bash$)/i;
24-
const IS_WSL = /(wsl.exe$)/i;
22+
const IS_GITBASH = /(gitbash$)/i;
23+
const IS_BASH = /(bash$)/i;
24+
const IS_WSL = /(wsl$)/i;
2525
const IS_ZSH = /(zsh$)/i;
2626
const IS_KSH = /(ksh$)/i;
27-
const IS_COMMAND = /(cmd.exe$|cmd$)/i;
28-
const IS_POWERSHELL = /(powershell.exe$|powershell$)/i;
29-
const IS_POWERSHELL_CORE = /(pwsh.exe$|pwsh$)/i;
27+
const IS_COMMAND = /(cmd$)/i;
28+
const IS_POWERSHELL = /(powershell$)/i;
29+
const IS_POWERSHELL_CORE = /(pwsh$)/i;
3030
const IS_FISH = /(fish$)/i;
3131
const IS_CSHELL = /(csh$)/i;
3232
const IS_TCSHELL = /(tcsh$)/i;
@@ -54,17 +54,21 @@ export abstract class BaseShellDetector implements IShellDetector {
5454
terminal?: Terminal,
5555
): TerminalShellType | undefined;
5656
public identifyShellFromShellPath(shellPath: string): TerminalShellType {
57+
// Remove .exe extension so shells can be more consistently detected
58+
// on Windows (including Cygwin).
59+
const basePath = shellPath.replace(/\.exe$/, '');
60+
5761
const shell = Array.from(detectableShells.keys()).reduce((matchedShell, shellToDetect) => {
5862
if (matchedShell === TerminalShellType.other) {
5963
const pat = detectableShells.get(shellToDetect);
60-
if (pat && pat.test(shellPath)) {
64+
if (pat && pat.test(basePath)) {
6165
return shellToDetect;
6266
}
6367
}
6468
return matchedShell;
6569
}, TerminalShellType.other);
6670

67-
traceVerbose(`Shell path '${shellPath}'`);
71+
traceVerbose(`Shell path '${shellPath}', base path '${basePath}'`);
6872
traceVerbose(`Shell path identified as shell '${shell}'`);
6973
return shell;
7074
}

src/test/common/terminals/shellDetectors/shellDetectors.unit.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ suite('Shell Detectors', () => {
3333
shellPathsAndIdentification.set('c:\\windows\\system32\\wsl.exe', TerminalShellType.wsl);
3434
shellPathsAndIdentification.set('c:\\windows\\system32\\gitbash.exe', TerminalShellType.gitbash);
3535
shellPathsAndIdentification.set('/usr/bin/bash', TerminalShellType.bash);
36+
shellPathsAndIdentification.set('c:\\cygwin\\bin\\bash.exe', TerminalShellType.bash);
37+
shellPathsAndIdentification.set('c:\\cygwin64\\bin\\bash.exe', TerminalShellType.bash);
3638
shellPathsAndIdentification.set('/usr/bin/zsh', TerminalShellType.zsh);
39+
shellPathsAndIdentification.set('c:\\cygwin\\bin\\zsh.exe', TerminalShellType.zsh);
40+
shellPathsAndIdentification.set('c:\\cygwin64\\bin\\zsh.exe', TerminalShellType.zsh);
3741
shellPathsAndIdentification.set('/usr/bin/ksh', TerminalShellType.ksh);
3842
shellPathsAndIdentification.set('c:\\windows\\system32\\powershell.exe', TerminalShellType.powershell);
3943
shellPathsAndIdentification.set('c:\\windows\\system32\\pwsh.exe', TerminalShellType.powershellCore);

0 commit comments

Comments
 (0)