Skip to content

Commit 75af730

Browse files
committed
fix(shell): make getShell() provider-aware for Zoo profile and execa overrides
1 parent 9f9c544 commit 75af730

2 files changed

Lines changed: 108 additions & 14 deletions

File tree

‎src/utils/__tests__/shell.spec.ts‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as vscode from "vscode"
33
import { existsSync } from "fs"
44
import { userInfo } from "os"
55
import { getShell } from "../shell"
6+
import { BaseTerminal } from "../../integrations/terminal/BaseTerminal"
7+
import { Terminal } from "../../integrations/terminal/Terminal"
68

79
vi.mock("vscode", () => ({
810
workspace: {
@@ -77,12 +79,17 @@ describe("Shell Detection Tests", () => {
7779
vi.mocked(userInfo).mockReturnValue({ shell: null } as any)
7880
// Default: PowerShell 7 is not installed, so the probe falls back to legacy.
7981
vi.mocked(existsSync).mockReturnValue(false)
82+
// Clear Zoo profile override and execa shell path between tests.
83+
Terminal.setTerminalProfile(undefined)
84+
BaseTerminal.setExecaShellPath(undefined)
8085
})
8186

8287
afterEach(() => {
8388
Object.defineProperty(process, "platform", { value: originalPlatform })
8489
process.env = originalEnv
8590
vscode.workspace.getConfiguration = originalGetConfig
91+
Terminal.setTerminalProfile(undefined)
92+
BaseTerminal.setExecaShellPath(undefined)
8693
vi.clearAllMocks()
8794
})
8895

@@ -515,4 +522,77 @@ describe("Shell Detection Tests", () => {
515522
expect(getShell()).toBe("/bin/bash")
516523
})
517524
})
525+
526+
// --------------------------------------------------------------------------
527+
// Zoo profile override (Terminal.getProfileShell) takes precedence
528+
// --------------------------------------------------------------------------
529+
describe("Zoo profile override", () => {
530+
it("uses Zoo profile shell over VS Code default profile", () => {
531+
Object.defineProperty(process, "platform", { value: "win32" })
532+
// VS Code default profile is PowerShell
533+
vi.mocked(existsSync).mockReturnValue(true)
534+
mockVsCodeConfig("windows", "PowerShell", {
535+
PowerShell: { path: "C:\\Program Files\\PowerShell\\7\\pwsh.exe" },
536+
})
537+
// Zoo profile override points to Git Bash
538+
Terminal.setTerminalProfile("Git Bash")
539+
vi.spyOn(Terminal, "getProfileShell").mockReturnValue({
540+
shellPath: "C:\\Program Files\\Git\\bin\\bash.exe",
541+
})
542+
expect(getShell()).toBe("C:\\Program Files\\Git\\bin\\bash.exe")
543+
})
544+
545+
it("falls through to VS Code default when Zoo profile has no resolvable shell", () => {
546+
Object.defineProperty(process, "platform", { value: "win32" })
547+
vi.mocked(existsSync).mockImplementation((p) => String(p) === "C:\\Program Files\\PowerShell\\7\\pwsh.exe")
548+
mockVsCodeConfig("windows", "PowerShell", {
549+
PowerShell: { path: "C:\\Program Files\\PowerShell\\7\\pwsh.exe" },
550+
})
551+
Terminal.setTerminalProfile("Unresolvable")
552+
vi.spyOn(Terminal, "getProfileShell").mockReturnValue(undefined)
553+
expect(getShell()).toBe("C:\\Program Files\\PowerShell\\7\\pwsh.exe")
554+
})
555+
})
556+
557+
// --------------------------------------------------------------------------
558+
// Explicit execa shell path takes highest precedence
559+
// --------------------------------------------------------------------------
560+
describe("Execa shell path override", () => {
561+
it("uses explicit execa shell path over VS Code config", () => {
562+
Object.defineProperty(process, "platform", { value: "win32" })
563+
vi.mocked(existsSync).mockReturnValue(true)
564+
mockVsCodeConfig("windows", "PowerShell", {
565+
PowerShell: { path: "C:\\Program Files\\PowerShell\\7\\pwsh.exe" },
566+
})
567+
BaseTerminal.setExecaShellPath("C:\\Program Files\\Git\\bin\\bash.exe")
568+
expect(getShell()).toBe("C:\\Program Files\\Git\\bin\\bash.exe")
569+
})
570+
571+
it("uses explicit execa shell path over Zoo profile override", () => {
572+
Object.defineProperty(process, "platform", { value: "linux" })
573+
mockVsCodeConfig("linux", null, {})
574+
Terminal.setTerminalProfile("fish")
575+
vi.spyOn(Terminal, "getProfileShell").mockReturnValue({
576+
shellPath: "/usr/bin/fish",
577+
})
578+
BaseTerminal.setExecaShellPath("/bin/zsh")
579+
expect(getShell()).toBe("/bin/zsh")
580+
})
581+
582+
it("falls through to VS Code config when execa shell path is not set", () => {
583+
Object.defineProperty(process, "platform", { value: "linux" })
584+
vi.mocked(existsSync).mockImplementation((p) => String(p) === "/usr/bin/fish")
585+
mockVsCodeConfig("linux", "fish", {
586+
fish: { path: "/usr/bin/fish" },
587+
})
588+
expect(getShell()).toBe("/usr/bin/fish")
589+
})
590+
591+
it("rejects non-allowlisted execa shell path and uses fallback", () => {
592+
Object.defineProperty(process, "platform", { value: "linux" })
593+
mockVsCodeConfig("linux", null, {})
594+
BaseTerminal.setExecaShellPath("/opt/evil/shell")
595+
expect(getShell()).toBe("/bin/bash")
596+
})
597+
})
518598
})

‎src/utils/shell.ts‎

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { existsSync } from "fs"
33
import { userInfo } from "os"
44
import * as path from "path"
55

6+
import { BaseTerminal } from "../integrations/terminal/BaseTerminal"
67
import { Terminal } from "../integrations/terminal/Terminal"
78

89
// Security: Allowlist of approved shell executables to prevent arbitrary command execution
@@ -119,20 +120,27 @@ const SHELL_PATHS = {
119120
// 1) VS Code Terminal Configuration Helper
120121
// -----------------------------------------------------
121122

122-
/**
123-
* Attempts to retrieve the configured default shell path from VS Code's terminal
124-
* profile settings, using the same trusted-scope reads as Terminal (inspect()
125-
* globalValue/defaultValue only — workspace scope excluded per APPLICATION scope
126-
* restriction on terminal.integrated.defaultProfile.*).
127-
*
128-
* Returns null when no profile is configured or the profile has no resolvable path.
129-
*/
130123
function preferredWindowsPowerShell(): string {
131124
return existsSync(SHELL_PATHS.POWERSHELL_7) ? SHELL_PATHS.POWERSHELL_7 : SHELL_PATHS.POWERSHELL_LEGACY
132125
}
133126

127+
/**
128+
* Resolves the shell that VS Code's integrated terminal will use, matching the
129+
* priority order from Terminal constructor: Zoo profile override first, then
130+
* VS Code's configured default profile (trusted scopes only — workspace scope
131+
* excluded per APPLICATION scope restriction).
132+
*
133+
* Returns null when no profile is configured or the profile has no resolvable path.
134+
*/
134135
function getShellFromVSCode(): string | null {
135136
try {
137+
// Zoo profile override takes precedence — this is the same path
138+
// Terminal constructor uses to set shellPath on createTerminal().
139+
const profileShell = Terminal.getProfileShell()
140+
if (profileShell?.shellPath) {
141+
return profileShell.shellPath
142+
}
143+
136144
const profileName = Terminal.getConfiguredDefaultProfileName()
137145

138146
if (!profileName) {
@@ -262,25 +270,31 @@ function getSafeFallbackShell(): string {
262270
export function getShell(): string {
263271
let shell: string | null = null
264272

265-
// 1. Check VS Code config first (trusted scopes only — workspace excluded).
266-
shell = getShellFromVSCode()
273+
// 1. Explicit execa shell path — when set, execa uses this exact executable
274+
// regardless of VS Code profile settings.
275+
shell = BaseTerminal.getExecaShellPath() ?? null
276+
277+
// 2. VS Code profile config (Zoo override first, then default profile).
278+
if (!shell) {
279+
shell = getShellFromVSCode()
280+
}
267281

268-
// 2. If no shell from VS Code, try userInfo()
282+
// 3. If no shell from VS Code, try userInfo()
269283
if (!shell) {
270284
shell = getShellFromUserInfo()
271285
}
272286

273-
// 3. If still nothing, try environment variable
287+
// 4. If still nothing, try environment variable
274288
if (!shell) {
275289
shell = getShellFromEnv()
276290
}
277291

278-
// 4. Finally, fall back to a default
292+
// 5. Finally, fall back to a default
279293
if (!shell) {
280294
shell = getSafeFallbackShell()
281295
}
282296

283-
// 5. Validate the shell against allowlist
297+
// 6. Validate the shell against allowlist
284298
if (!isShellAllowed(shell)) {
285299
shell = getSafeFallbackShell()
286300
}

0 commit comments

Comments
 (0)