Fix Windows default shell for terminal creation
This commit is contained in:
@@ -2,6 +2,7 @@ import { describe, it, expect, afterEach } from "vitest";
|
|||||||
import {
|
import {
|
||||||
createTerminal,
|
createTerminal,
|
||||||
ensureNodePtySpawnHelperExecutableForCurrentPlatform,
|
ensureNodePtySpawnHelperExecutableForCurrentPlatform,
|
||||||
|
resolveDefaultTerminalShell,
|
||||||
type TerminalSession,
|
type TerminalSession,
|
||||||
} from "./terminal.js";
|
} from "./terminal.js";
|
||||||
import { chmodSync, mkdtempSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
import { chmodSync, mkdtempSync, mkdirSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||||
@@ -111,6 +112,18 @@ describe("Terminal", () => {
|
|||||||
expect(statSync(helperPath).mode & 0o111).toBe(0o111);
|
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 () => {
|
it("creates a terminal session with an id, name, and cwd", async () => {
|
||||||
const session = trackSession(
|
const session = trackSession(
|
||||||
await createTerminal({
|
await createTerminal({
|
||||||
|
|||||||
@@ -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 {
|
function extractCell(terminal: TerminalType, row: number, col: number): TerminalCell {
|
||||||
const buffer = terminal.buffer.active;
|
const buffer = terminal.buffer.active;
|
||||||
const line = buffer.getLine(row);
|
const line = buffer.getLine(row);
|
||||||
@@ -299,12 +315,13 @@ export function captureTerminalLines(
|
|||||||
export async function createTerminal(options: CreateTerminalOptions): Promise<TerminalSession> {
|
export async function createTerminal(options: CreateTerminalOptions): Promise<TerminalSession> {
|
||||||
const {
|
const {
|
||||||
cwd,
|
cwd,
|
||||||
shell = process.env.SHELL || "/bin/sh",
|
shell,
|
||||||
env = {},
|
env = {},
|
||||||
rows = 24,
|
rows = 24,
|
||||||
cols = 80,
|
cols = 80,
|
||||||
name = "Terminal",
|
name = "Terminal",
|
||||||
} = options;
|
} = options;
|
||||||
|
const resolvedShell = shell ?? resolveDefaultTerminalShell();
|
||||||
|
|
||||||
const id = randomUUID();
|
const id = randomUUID();
|
||||||
const listeners = new Set<(msg: ServerMessage) => void>();
|
const listeners = new Set<(msg: ServerMessage) => void>();
|
||||||
@@ -324,7 +341,7 @@ export async function createTerminal(options: CreateTerminalOptions): Promise<Te
|
|||||||
ensureNodePtySpawnHelperExecutableForCurrentPlatform();
|
ensureNodePtySpawnHelperExecutableForCurrentPlatform();
|
||||||
|
|
||||||
// Create PTY
|
// Create PTY
|
||||||
const ptyProcess = pty.spawn(shell, [], {
|
const ptyProcess = pty.spawn(resolvedShell, [], {
|
||||||
name: "xterm-256color",
|
name: "xterm-256color",
|
||||||
cols,
|
cols,
|
||||||
rows,
|
rows,
|
||||||
|
|||||||
Reference in New Issue
Block a user