Fix Windows default shell for terminal creation

This commit is contained in:
Mohamed Boudra
2026-04-01 17:25:41 +07:00
parent 58d72bea87
commit cb60f2a596
2 changed files with 32 additions and 2 deletions
@@ -2,6 +2,7 @@ import { describe, it, expect, afterEach } from "vitest";
import {
createTerminal,
ensureNodePtySpawnHelperExecutableForCurrentPlatform,
resolveDefaultTerminalShell,
type TerminalSession,
} from "./terminal.js";
import { chmodSync, mkdtempSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
@@ -111,6 +112,18 @@ describe("Terminal", () => {
expect(statSync(helperPath).mode & 0o111).toBe(0o111);
});
it("uses cmd.exe-compatible default shell on Windows", () => {
expect(resolveDefaultTerminalShell({ platform: "win32", env: {} })).toBe(
"C:\\Windows\\System32\\cmd.exe",
);
expect(
resolveDefaultTerminalShell({
platform: "win32",
env: { ComSpec: "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" },
}),
).toBe("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe");
});
it("creates a terminal session with an id, name, and cwd", async () => {
const session = trackSession(
await createTerminal({
+19 -2
View File
@@ -119,6 +119,22 @@ export function ensureNodePtySpawnHelperExecutableForCurrentPlatform(
}
}
export function resolveDefaultTerminalShell(
options: {
platform?: NodeJS.Platform;
env?: NodeJS.ProcessEnv;
} = {},
): string {
const platform = options.platform ?? process.platform;
const env = options.env ?? process.env;
if (platform === "win32") {
return env.ComSpec || env.COMSPEC || "C:\\Windows\\System32\\cmd.exe";
}
return env.SHELL || "/bin/sh";
}
function extractCell(terminal: TerminalType, row: number, col: number): TerminalCell {
const buffer = terminal.buffer.active;
const line = buffer.getLine(row);
@@ -299,12 +315,13 @@ export function captureTerminalLines(
export async function createTerminal(options: CreateTerminalOptions): Promise<TerminalSession> {
const {
cwd,
shell = process.env.SHELL || "/bin/sh",
shell,
env = {},
rows = 24,
cols = 80,
name = "Terminal",
} = options;
const resolvedShell = shell ?? resolveDefaultTerminalShell();
const id = randomUUID();
const listeners = new Set<(msg: ServerMessage) => void>();
@@ -324,7 +341,7 @@ export async function createTerminal(options: CreateTerminalOptions): Promise<Te
ensureNodePtySpawnHelperExecutableForCurrentPlatform();
// Create PTY
const ptyProcess = pty.spawn(shell, [], {
const ptyProcess = pty.spawn(resolvedShell, [], {
name: "xterm-256color",
cols,
rows,