Skip to content

Commit 960d576

Browse files
t3-code-bot[bot]nateEcUtkarshUsername
authored
fix(cli): restore Windows desktop launcher console input (#24870)
Co-authored-by: Nathan Shan <[email protected]> Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com> Co-authored-by: Utkarsh Patil <[email protected]>
1 parent 37fbefd commit 960d576

2 files changed

Lines changed: 148 additions & 10 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
2+
3+
const io = vi.hoisted(() => ({
4+
platform: vi.fn(() => "win32"),
5+
stat: vi.fn(() => ({ isCharacterDevice: (): boolean => true })),
6+
open: vi.fn(() => 42),
7+
close: vi.fn(),
8+
isatty: vi.fn((fd: number) => fd !== 0),
9+
}));
10+
11+
vi.mock("node:fs", () => ({ fstatSync: io.stat, openSync: io.open, closeSync: io.close }));
12+
vi.mock("node:tty", async () => {
13+
const { EventEmitter } = await import("node:events");
14+
return {
15+
isatty: io.isatty,
16+
ReadStream: class extends EventEmitter {
17+
readonly isTTY = true;
18+
readonly _readableState = { reading: true };
19+
_handle: { reading: boolean; readStop: () => void } | undefined = {
20+
reading: true,
21+
readStop: vi.fn(),
22+
};
23+
},
24+
};
25+
});
26+
vi.mock("@t3tools/shared/HostProcess", () => ({ Platform: { defaultValue: io.platform } }));
27+
28+
const originalStdin = Object.getOwnPropertyDescriptor(process, "stdin")!;
29+
30+
beforeEach(() => {
31+
vi.resetModules();
32+
vi.resetAllMocks();
33+
vi.stubEnv("T3CODE_DESKTOP_CLI_LAUNCHER", "1");
34+
});
35+
36+
afterEach(() => {
37+
Object.defineProperty(process, "stdin", originalStdin);
38+
vi.unstubAllEnvs();
39+
});
40+
41+
describe("attachConsoleStdin", () => {
42+
it("reopens the console when Node replaces missing Windows stdin with NUL", async () => {
43+
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
44+
45+
expect(attachConsoleStdin()).toBe(42);
46+
expect(process.stdin.isTTY).toBe(true);
47+
expect(interactiveStdin()).toBe(42);
48+
expect(process.env.T3CODE_DESKTOP_CLI_LAUNCHER).toBeUndefined();
49+
});
50+
51+
it("releases console reads when the picker pauses stdin before launching an agent", async () => {
52+
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
53+
attachConsoleStdin();
54+
const stdin = process.stdin as unknown as {
55+
emit(event: string): boolean;
56+
_readableState: { reading: boolean };
57+
_handle: { reading: boolean; readStop: () => void };
58+
};
59+
stdin.emit("pause");
60+
expect(stdin._readableState.reading).toBe(false);
61+
expect(stdin._handle.reading).toBe(false);
62+
expect(stdin._handle.readStop).toHaveBeenCalledOnce();
63+
expect(interactiveStdin()).toBe(42);
64+
});
65+
66+
it("can pause after the reopened stream's handle has been closed", async () => {
67+
const { attachConsoleStdin } = await import("./consoleStdin.ts");
68+
attachConsoleStdin();
69+
const stdin = process.stdin as unknown as {
70+
emit(event: string): boolean;
71+
_handle: undefined;
72+
};
73+
stdin._handle = undefined;
74+
expect(() => stdin.emit("pause")).not.toThrow();
75+
});
76+
77+
it("still reopens a closed stdin", async () => {
78+
io.stat.mockImplementation(() => {
79+
throw new Error("EBADF");
80+
});
81+
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
82+
expect(attachConsoleStdin()).toBe(42);
83+
expect(interactiveStdin()).toBe(42);
84+
});
85+
86+
it.each(["pipe", "file"])("preserves redirected %s input", async () => {
87+
io.stat.mockReturnValue({ isCharacterDevice: () => false });
88+
const stdin = process.stdin;
89+
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
90+
expect(attachConsoleStdin()).toBeUndefined();
91+
expect(process.stdin).toBe(stdin);
92+
expect(interactiveStdin()).toBe("inherit");
93+
expect(io.open).not.toHaveBeenCalled();
94+
});
95+
96+
it.each(["console input", "redirected output", "unmarked process", "non-Windows"])(
97+
"leaves %s alone",
98+
async (scenario) => {
99+
if (scenario === "console input") io.isatty.mockReturnValue(true);
100+
if (scenario === "redirected output") io.isatty.mockReturnValue(false);
101+
if (scenario === "unmarked process") delete process.env.T3CODE_DESKTOP_CLI_LAUNCHER;
102+
if (scenario === "non-Windows") io.platform.mockReturnValue("darwin");
103+
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
104+
expect(attachConsoleStdin()).toBeUndefined();
105+
expect(interactiveStdin()).toBe("inherit");
106+
expect(io.open).not.toHaveBeenCalled();
107+
expect(process.env.T3CODE_DESKTOP_CLI_LAUNCHER).toBeUndefined();
108+
},
109+
);
110+
111+
it("keeps inherited input when CONIN$ cannot be opened", async () => {
112+
io.open.mockImplementation(() => {
113+
throw new Error("ENOENT");
114+
});
115+
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
116+
expect(attachConsoleStdin()).toBeUndefined();
117+
expect(interactiveStdin()).toBe("inherit");
118+
});
119+
120+
it("closes a reopened descriptor that is not a console", async () => {
121+
io.isatty.mockImplementation((fd) => fd === 1);
122+
const { attachConsoleStdin, interactiveStdin } = await import("./consoleStdin.ts");
123+
expect(attachConsoleStdin()).toBeUndefined();
124+
expect(interactiveStdin()).toBe("inherit");
125+
expect(io.close).toHaveBeenCalledWith(42);
126+
});
127+
});

‎apps/server/src/cli/consoleStdin.ts‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,30 @@ import * as NodeTty from "node:tty";
55
import * as HostProcess from "@t3tools/shared/HostProcess";
66

77
/**
8-
* Electron's Windows executable is a GUI program, so `t3.cmd` under
9-
* `ELECTRON_RUN_AS_NODE` gets the console for stdout and stderr but no stdin.
10-
* Piped or redirected input and runs whose output is not a console are left alone.
8+
* Electron's Windows launcher has no stdin; Node substitutes a non-console NUL device.
9+
* Preserve pipes, files, console input, and runs without console output.
10+
* Explicit `< NUL` is indistinguishable from missing launcher stdin and also reattaches.
1111
*/
1212
function needsConsoleStdin(input: {
1313
readonly platform: NodeJS.Platform;
1414
readonly env: NodeJS.ProcessEnv;
15-
readonly stdinOpen: boolean;
15+
readonly stdinMissing: boolean;
1616
readonly stdoutIsConsole: boolean;
1717
}): boolean {
1818
return (
1919
input.platform === "win32" &&
2020
// Set only by the launcher (DesktopCliShim), never for the desktop backend.
2121
input.env.T3CODE_DESKTOP_CLI_LAUNCHER === "1" &&
22-
!input.stdinOpen &&
22+
input.stdinMissing &&
2323
input.stdoutIsConsole
2424
);
2525
}
2626

27-
const isOpen = (fd: number) => {
27+
const isMissingInput = (fd: number) => {
2828
try {
29-
NodeFS.fstatSync(fd);
30-
return true;
29+
return NodeFS.fstatSync(fd).isCharacterDevice() && !NodeTty.isatty(fd);
3130
} catch {
32-
return false;
31+
return true;
3332
}
3433
};
3534

@@ -38,7 +37,7 @@ export function attachConsoleStdin(): number | undefined {
3837
const needed = needsConsoleStdin({
3938
platform: HostProcess.Platform.defaultValue(),
4039
env: process.env,
41-
stdinOpen: isOpen(0),
40+
stdinMissing: isMissingInput(0),
4241
stdoutIsConsole: NodeTty.isatty(1),
4342
});
4443
// The launcher marker must not propagate to child shells or t3 processes.
@@ -55,6 +54,18 @@ export function attachConsoleStdin(): number | undefined {
5554
return undefined;
5655
}
5756
const stdin = new NodeTty.ReadStream(fd);
57+
// Like Node's built-in stdin, release pending console reads before a child takes over.
58+
stdin.on("pause", () => {
59+
const internal = stdin as unknown as {
60+
_handle?: { reading: boolean; readStop(): void };
61+
_readableState: { reading: boolean };
62+
};
63+
const handle = internal._handle;
64+
if (!handle) return;
65+
internal._readableState.reading = false;
66+
handle.reading = false;
67+
handle.readStop();
68+
});
5869
Object.defineProperty(process, "stdin", { value: stdin, configurable: true, enumerable: true });
5970
consoleStdinFd = fd;
6071
return fd;

0 commit comments

Comments
 (0)