Skip to content
Merged
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
19 changes: 18 additions & 1 deletion extensions/cli/src/tools/runTerminalCommand.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { runTerminalCommandTool } from "./runTerminalCommand.js";
import {
isRunningInWsl,
runTerminalCommandTool,
} from "./runTerminalCommand.js";

describe("runTerminalCommandTool", () => {
const isWindows = process.platform === "win32";
Expand Down Expand Up @@ -104,4 +107,18 @@ describe("runTerminalCommandTool", () => {
});
}
});

describe("WSL detection", () => {
it("should cache the WSL detection result", () => {
const firstResult = isRunningInWsl();
const secondResult = isRunningInWsl();
expect(firstResult).toBe(secondResult);
});

if (!isLinux) {
it("should return false on non-Linux platforms", () => {
expect(isRunningInWsl()).toBe(false);
});
}
});
});
42 changes: 37 additions & 5 deletions extensions/cli/src/tools/runTerminalCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn } from "child_process";
import fs from "fs";

import {
evaluateTerminalCommandSecurity,
Expand All @@ -21,6 +22,28 @@ import { Tool, ToolRunContext } from "./types.js";
const DEFAULT_BASH_MAX_CHARS = 50000; // ~12.5k tokens
const DEFAULT_BASH_MAX_LINES = 1000;

/**
* When running on Windows, but inside WSL, shell commands need to run using the WSL environment.
*/
export function isRunningInWsl(): boolean {
// WSL only applies when platform reports as Linux
if (process.platform !== "linux") {
return false;
}

if (process.env.WSL_DISTRO_NAME) {
return true;
}

// Check /proc/version for Microsoft/WSL indicators
try {
const procVersion = fs.readFileSync("/proc/version", "utf8").toLowerCase();
return procVersion.includes("microsoft") || procVersion.includes("wsl");
} catch {
return false;
}
}

function getBashMaxChars(): number {
return parseEnvNumber(
process.env.CONTINUE_CLI_BASH_MAX_OUTPUT_CHARS,
Expand All @@ -35,19 +58,28 @@ function getBashMaxLines(): number {
);
}

// Helper function to use login shell on Unix/macOS and PowerShell on Windows
// 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[] } {
if (process.platform === "win32") {
// Windows: Use PowerShell
return {
shell: "powershell.exe",
args: ["-NoLogo", "-ExecutionPolicy", "Bypass", "-Command", command],
};
} 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] };
}

if (isRunningInWsl()) {
// in WSL, bash is always available
const wslShell = process.env.SHELL || "/bin/bash";
return {
shell: wslShell,
args: ["-l", "-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] };
}

export const runTerminalCommandTool: Tool = {
Expand Down
Loading