Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions core/tools/implementations/runTerminalCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ function getDecodedOutput(data: Buffer): string {
return data.toString();
}
} // Simple helper function to use login shell on Unix/macOS and PowerShell on Windows
function getShellCommand(command: string): { shell: string; args: string[] } {
export function getShellCommand(command: string): {
shell: string;
args: string[];
} {
if (process.platform === "win32") {
// Windows: Use PowerShell
return {
Expand All @@ -32,10 +35,20 @@ function getShellCommand(command: string): { shell: string; args: string[] } {
} else {
// Unix/macOS: Use login shell to source .bashrc/.zshrc etc.
const userShell = process.env.SHELL || "/bin/bash";
return { shell: userShell, args: ["-l", "-c", command] };
return {
shell: userShell,
args: shellSupportsLoginFlag(userShell)
? ["-l", "-c", command]
: ["-c", command],
};
}
}

function shellSupportsLoginFlag(shell: string): boolean {
const shellName = shell.split(/[\\/]/).pop()?.toLowerCase();
return shellName !== "csh" && shellName !== "tcsh";
}

import { fileURLToPath } from "node:url";
import { ToolImpl } from ".";
import {
Expand Down
21 changes: 20 additions & 1 deletion core/tools/implementations/runTerminalCommand.vitest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { IDE, ToolExtras } from "../..";
import * as processTerminalStates from "../../util/processTerminalStates";
import { runTerminalCommandTool } from "../definitions/runTerminalCommand";
import { runTerminalCommandImpl } from "./runTerminalCommand";
import { getShellCommand, runTerminalCommandImpl } from "./runTerminalCommand";

// We're using real child processes, so ensure these aren't mocked
vi.unmock("node:child_process");
Expand Down Expand Up @@ -857,4 +857,23 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {

expect(result).toBe("allowedWithPermission");
});

it.runIf(process.platform !== "win32")(
"does not pass login-shell flag to csh-family shells",
() => {
const previousShell = process.env.SHELL;
process.env.SHELL = "/bin/tcsh";
try {
const { shell, args } = getShellCommand("pwd");
expect(shell).toBe("/bin/tcsh");
expect(args).toEqual(["-c", "pwd"]);
} finally {
if (previousShell === undefined) {
delete process.env.SHELL;
} else {
process.env.SHELL = previousShell;
}
}
},
);
});
22 changes: 22 additions & 0 deletions extensions/cli/src/tools/runTerminalCommand.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getShellCommand,
isRunningInWsl,
runTerminalCommandTool,
} from "./runTerminalCommand.js";
Expand Down Expand Up @@ -121,4 +122,25 @@ describe("runTerminalCommandTool", () => {
});
}
});

describe("shell selection", () => {
it.runIf(!isWindows)(
"does not pass login-shell flag to csh-family shells",
() => {
const previousShell = process.env.SHELL;
process.env.SHELL = "/bin/tcsh";
try {
const { shell, args } = getShellCommand("pwd");
expect(shell).toBe("/bin/tcsh");
expect(args).toEqual(["-c", "pwd"]);
} finally {
if (previousShell === undefined) {
delete process.env.SHELL;
} else {
process.env.SHELL = previousShell;
}
}
},
);
});
});
21 changes: 18 additions & 3 deletions extensions/cli/src/tools/runTerminalCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ function getBashMaxLines(): number {
}

// Helper function to use login shell on Unix/macOS and PowerShell on Windows and available shell in WSL
function getShellCommand(command: string): { shell: string; args: string[] } {
export function getShellCommand(command: string): {
shell: string;
args: string[];
} {
if (process.platform === "win32") {
// Windows: Use PowerShell
return {
Expand All @@ -77,13 +80,25 @@ function getShellCommand(command: string): { shell: string; args: string[] } {
const wslShell = process.env.SHELL || "/bin/bash";
return {
shell: wslShell,
args: ["-l", "-c", command],
args: shellSupportsLoginFlag(wslShell)
? ["-l", "-c", command]
: ["-c", command],
};
}

// Unix/macOS: Use login shell to source .bashrc/.zshrc etc.
const userShell = process.env.SHELL || "/bin/bash";
return { shell: userShell, args: ["-l", "-c", command] };
return {
shell: userShell,
args: shellSupportsLoginFlag(userShell)
? ["-l", "-c", command]
: ["-c", command],
};
}

function shellSupportsLoginFlag(shell: string): boolean {
const shellName = shell.split(/[\\/]/).pop()?.toLowerCase();
return shellName !== "csh" && shellName !== "tcsh";
}

export function runCommandInBackground(command: string): {
Expand Down
Loading