Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 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,16 @@ describe("runTerminalCommandTool.evaluateToolCallPolicy", () => {

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

it("does not pass login-shell flag to csh-family shells", () => {
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
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 {
process.env.SHELL = previousShell;
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
}
});
});
15 changes: 15 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 @@ -70,7 +71,7 @@
});

describe("basic error handling", () => {
it("should handle non-existent commands", async () => {

Check failure on line 74 in extensions/cli/src/tools/runTerminalCommand.test.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

src/tools/runTerminalCommand.test.ts > runTerminalCommandTool > basic error handling > should handle non-existent commands

Error: Test timed out in 30000ms. If this is a long-running test, pass a timeout value as the last argument or configure it globally with "testTimeout". ❯ src/tools/runTerminalCommand.test.ts:74:5
const command = "definitely-not-a-real-command-xyz123";

await expect(runTerminalCommandTool.run({ command })).rejects.toMatch(
Expand Down Expand Up @@ -121,4 +122,18 @@
});
}
});

describe("shell selection", () => {
it("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");

Check failure on line 132 in extensions/cli/src/tools/runTerminalCommand.test.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 24)

src/tools/runTerminalCommand.test.ts > runTerminalCommandTool > shell selection > does not pass login-shell flag to csh-family shells

AssertionError: expected 'powershell.exe' to be '/bin/tcsh' // Object.is equality Expected: "/bin/tcsh" Received: "powershell.exe" ❯ src/tools/runTerminalCommand.test.ts:132:23

Check failure on line 132 in extensions/cli/src/tools/runTerminalCommand.test.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 22)

src/tools/runTerminalCommand.test.ts > runTerminalCommandTool > shell selection > does not pass login-shell flag to csh-family shells

AssertionError: expected 'powershell.exe' to be '/bin/tcsh' // Object.is equality Expected: "/bin/tcsh" Received: "powershell.exe" ❯ src/tools/runTerminalCommand.test.ts:132:23

Check failure on line 132 in extensions/cli/src/tools/runTerminalCommand.test.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 18)

src/tools/runTerminalCommand.test.ts > runTerminalCommandTool > shell selection > does not pass login-shell flag to csh-family shells

AssertionError: expected 'powershell.exe' to be '/bin/tcsh' // Object.is equality Expected: "/bin/tcsh" Received: "powershell.exe" ❯ src/tools/runTerminalCommand.test.ts:132:23

Check failure on line 132 in extensions/cli/src/tools/runTerminalCommand.test.ts

View workflow job for this annotation

GitHub Actions / test (windows-latest, 20)

src/tools/runTerminalCommand.test.ts > runTerminalCommandTool > shell selection > does not pass login-shell flag to csh-family shells

AssertionError: expected 'powershell.exe' to be '/bin/tcsh' // Object.is equality Expected: "/bin/tcsh" Received: "powershell.exe" ❯ src/tools/runTerminalCommand.test.ts:132:23
expect(args).toEqual(["-c", "pwd"]);
} finally {
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