Skip to content
Closed
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
4 changes: 3 additions & 1 deletion apps/desktop/src/shell/DesktopShellEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import * as Schema from "effect/Schema";
import * as ChildProcess from "effect/unstable/process/ChildProcess";
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";

import { WINDOWS_POWERSHELL_CANDIDATES } from "@t3tools/shared/shell";

import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";

type EnvironmentPatch = Record<string, string>;
Expand Down Expand Up @@ -84,7 +86,7 @@ const LOGIN_SHELL_ENV_NAMES = [
"WAYLAND_DISPLAY",
] as const;
const WINDOWS_PROFILE_ENV_NAMES = ["PATH", "FNM_DIR", "FNM_MULTISHELL_PATH"] as const;
const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;
const WINDOWS_SHELL_CANDIDATES = WINDOWS_POWERSHELL_CANDIDATES;
const LOGIN_SHELL_TIMEOUT = Duration.seconds(5);
const LAUNCHCTL_TIMEOUT = Duration.seconds(2);
const PROCESS_TERMINATE_GRACE = Duration.seconds(1);
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/lib/threadActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ function splitExecutableAndRest(value: string): { executable: string; rest: stri

const SHELL_WRAPPER_SPECS = [
{
executables: ["pwsh", "pwsh.exe", "powershell", "powershell.exe"],
executables: ["powershell", "powershell.exe", "pwsh", "pwsh.exe"],
wrapperFlagPattern: /(?:^|\s)-command\s+/i,
},
{
Expand Down
28 changes: 14 additions & 14 deletions apps/server/src/terminal/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1298,12 +1298,17 @@ it.layer(

if (platform === "win32") {
expect(
ptyAdapter.spawnInputs.some(
(input) =>
input.shell === "pwsh.exe" ||
input.shell === "powershell.exe" ||
input.shell === "cmd.exe",
),
ptyAdapter.spawnInputs.some((input) => {
const shell = input.shell.toLowerCase();
return (
shell === "pwsh.exe" ||
shell === "powershell.exe" ||
shell === "cmd.exe" ||
shell.endsWith("\\powershell.exe") ||
shell.endsWith("\\pwsh.exe") ||
shell.endsWith("\\cmd.exe")
);
}),
).toBe(true);
} else {
expect(
Expand All @@ -1315,7 +1320,7 @@ it.layer(
}),
);

it.effect("prefers PowerShell over ComSpec for Windows terminals", () =>
it.effect("prefers Windows PowerShell 5.1 over ComSpec for Windows terminals", () =>
Effect.gen(function* () {
const { manager, ptyAdapter } = yield* createManager(5, {
env: {
Expand All @@ -1329,7 +1334,7 @@ it.layer(

expect(ptyAdapter.spawnInputs[0]).toEqual(
expect.objectContaining({
shell: "pwsh.exe",
shell: "powershell.exe",
args: ["-NoLogo"],
}),
);
Expand All @@ -1348,20 +1353,15 @@ it.layer(
SystemRoot: "C:\\Windows",
},
}).pipe(Effect.provide(withHostPlatform("win32")));
ptyAdapter.spawnFailures.push(
new Error("spawn custom-shell.exe ENOENT"),
new Error("spawn pwsh.exe ENOENT"),
);
ptyAdapter.spawnFailures.push(new Error("spawn custom-shell.exe ENOENT"));

yield* manager.open(openInput());

expect(ptyAdapter.spawnInputs.map((input) => input.shell)).toEqual([
"C:\\missing\\custom-shell.exe",
"pwsh.exe",
"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
]);
expect(ptyAdapter.spawnInputs[1]?.args).toEqual(["-NoLogo"]);
expect(ptyAdapter.spawnInputs[2]?.args).toEqual(["-NoLogo"]);
}),
);

Expand Down
10 changes: 5 additions & 5 deletions apps/server/src/terminal/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ function enqueueProcessEvent(

function defaultShellResolver(platform: NodeJS.Platform, env: NodeJS.ProcessEnv): string {
if (platform === "win32") {
return "pwsh.exe";
return "powershell.exe";
}
return env.SHELL ?? "bash";
}
Expand Down Expand Up @@ -543,9 +543,9 @@ function resolveShellCandidates(
if (platform === "win32") {
return uniqueShellCandidates([
requested,
shellCandidateFromCommand("pwsh.exe", platform),
shellCandidateFromCommand(windowsPowerShellPath(env), platform),
shellCandidateFromCommand("powershell.exe", platform),
shellCandidateFromCommand("pwsh.exe", platform),
shellCandidateFromCommand(env.ComSpec ?? null, platform),
shellCandidateFromCommand(windowsCmdPath(env), platform),
shellCandidateFromCommand("cmd.exe", platform),
Expand Down Expand Up @@ -633,9 +633,9 @@ function windowsInspectSubprocess(
return Effect.gen(function* () {
const processRunner = yield* ProcessRunner.ProcessRunner;
return yield* processRunner.run({
// powershell.exe is a real executable — never spawn it through cmd.exe
// shell mode, which would re-tokenize the `-Command` payload (pipes,
// semicolons) before PowerShell ever sees it.
// Windows PowerShell 5.1 is a real executable — never spawn it through
// cmd.exe shell mode, which would re-tokenize the `-Command` payload
// (pipes, semicolons) before PowerShell ever sees it.
command: "powershell.exe",
args: ["-NoProfile", "-NonInteractive", "-Command", command],
timeout: "1500 millis",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/session-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ function splitExecutableAndRest(value: string): { executable: string; rest: stri

const SHELL_WRAPPER_SPECS = [
{
executables: ["pwsh", "pwsh.exe", "powershell", "powershell.exe"],
executables: ["powershell", "powershell.exe", "pwsh", "pwsh.exe"],
wrapperFlagPattern: /(?:^|\s)-command\s+/i,
},
{
Expand Down
21 changes: 14 additions & 7 deletions packages/shared/src/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
resolveSpawnCommand,
resolveWindowsEnvironment,
SpawnExecutableResolution,
WINDOWS_POWERSHELL_CANDIDATES,
WindowsShellEnvironment,
type WindowsShellEnvironmentReader,
} from "./shell.ts";
Expand Down Expand Up @@ -215,6 +216,12 @@ describe("mergePathEntries", () => {
});
});

describe("WINDOWS_POWERSHELL_CANDIDATES", () => {
it("tries Windows PowerShell 5.1 before pwsh", () => {
expect(WINDOWS_POWERSHELL_CANDIDATES).toEqual(["powershell.exe", "pwsh.exe"]);
});
});

describe("readEnvironmentFromWindowsShell", () => {
it("extracts environment variables from a PowerShell command", () => {
const execFile = vi.fn<
Expand All @@ -232,7 +239,7 @@ describe("readEnvironmentFromWindowsShell", () => {
PATH: "C:\\Users\\testuser\\AppData\\Roaming\\npm",
});
expect(execFile).toHaveBeenCalledWith(
"pwsh.exe",
"powershell.exe",
expect.arrayContaining(["-NoLogo", "-NoProfile", "-NonInteractive", "-Command"]),
{ encoding: "utf8", timeout: 5000 },
);
Expand Down Expand Up @@ -268,35 +275,35 @@ describe("readEnvironmentFromWindowsShell", () => {
PATH: "C:\\Tools",
});
expect(execFile).toHaveBeenCalledWith(
"pwsh.exe",
"powershell.exe",
expect.arrayContaining(["-NoLogo", "-NonInteractive", "-Command"]),
{ encoding: "utf8", timeout: 5000 },
);
expect(execFile.mock.calls[0]?.[1]).not.toContain("-NoProfile");
});

it("falls back to Windows PowerShell when pwsh.exe is unavailable", () => {
it("falls back to PowerShell 7 when Windows PowerShell 5.1 is unavailable", () => {
const execFile = vi.fn<
(
file: string,
args: ReadonlyArray<string>,
options: { encoding: "utf8"; timeout: number },
) => string
>((file) => {
if (file === "pwsh.exe") {
throw new Error("spawn pwsh.exe ENOENT");
if (file === "powershell.exe") {
throw new Error("spawn powershell.exe ENOENT");
}
return "__T3CODE_ENV_PATH_START__\nC:\\Tools\n__T3CODE_ENV_PATH_END__\n";
});

expect(readEnvironmentFromWindowsShell(["PATH"], execFile)).toEqual({
PATH: "C:\\Tools",
});
expect(execFile).toHaveBeenNthCalledWith(1, "pwsh.exe", expect.any(Array), {
expect(execFile).toHaveBeenNthCalledWith(1, "powershell.exe", expect.any(Array), {
encoding: "utf8",
timeout: 5000,
});
expect(execFile).toHaveBeenNthCalledWith(2, "powershell.exe", expect.any(Array), {
expect(execFile).toHaveBeenNthCalledWith(2, "pwsh.exe", expect.any(Array), {
encoding: "utf8",
timeout: 5000,
});
Expand Down
7 changes: 5 additions & 2 deletions packages/shared/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const PATH_CAPTURE_END = "__T3CODE_PATH_END__";
const SHELL_ENV_NAME_PATTERN = /^[A-Z0-9_]+$/;
const WINDOWS_PATH_DELIMITER = ";";
const POSIX_PATH_DELIMITER = ":";
const WINDOWS_SHELL_CANDIDATES = ["pwsh.exe", "powershell.exe"] as const;

// Windows PowerShell 5.1 first (always present); pwsh only as fallback.
// Probe scripts must stay 5.1-compatible either way.
export const WINDOWS_POWERSHELL_CANDIDATES = ["powershell.exe", "pwsh.exe"] as const;

type ExecFileSyncLike = (
file: string,
Expand Down Expand Up @@ -381,7 +384,7 @@ export function readEnvironmentFromWindowsShell(
"-Command",
command,
];
for (const shell of WINDOWS_SHELL_CANDIDATES) {
for (const shell of WINDOWS_POWERSHELL_CANDIDATES) {
try {
const output = execFile(shell, args, { encoding: "utf8", timeout: 5000 });

Expand Down
Loading