Skip to content

Commit 393d72a

Browse files
committed
fix(windows): stop the service wrapper killer using the argv Bun rejects
src/service.ts spawned PowerShell with "-WindowStyle", "Hidden" as argv elements. src/codex/user-identity.ts:222-224 already forbids exactly that: Bun 1.3.14 can fail the direct CLI pair before the command runs (#1589), and the process-level windowsHide flag is what actually suppresses the window. The call is not decorative. stopServiceIfInstalled() uses it because schtasks /end can leave the wscript.exe/cmd.exe wrapper alive to respawn the proxy, and it ignores spawnSync's exit status. Under #1589 wrapper termination silently does nothing, so ocx stop, restart and update report success without sticking. The invariant survived as prose plus a single-file assertion: tests/windows-deploy-close-regressions.test.ts:43 pins the argv only for src/update/job.ts (the variable is bound at line 13), so service.ts was never covered. Replaced with a sweep over every src/**/*.ts, which found exactly one offender. The sweep matches the argv form only. Six call sites legitimately pass -WindowStyle Hidden inside a PowerShell script string handed to Start-Process (windows-elevation.ts 622/660/687/736, tray/windows.ts:489, update/job.ts:574); Bun never parses those. A second test pins that discrimination in both directions so the sweep cannot quietly become vacuous or start failing correct code. Driven red first: the sweep reported ["service.ts"] before the fix. Verification: bun test tests/windows-popup-fix.test.ts (7 pass), bun test tests/service.test.ts tests/windows-deploy-close-regressions.test.ts (131 pass), bun run typecheck clean.
1 parent 4ef1fca commit 393d72a

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎src/service.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,10 @@ function killWindowsServiceWrapperProcesses(): void {
23582358
"} | ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }",
23592359
].join(" ");
23602360
spawnSync(resolveTrustedWindowsPowerShellExe(), [
2361-
"-NoProfile", "-NoLogo", "-NonInteractive", "-WindowStyle", "Hidden",
2361+
// No `-WindowStyle Hidden` here: Bun 1.3.14 can fail that direct CLI pair
2362+
// before the command runs (#1589). `windowsHide` below is what actually
2363+
// suppresses the console window, and it is sufficient.
2364+
"-NoProfile", "-NoLogo", "-NonInteractive",
23622365
"-Command", ps,
23632366
], { stdio: "ignore", timeout: 5000, windowsHide: true });
23642367
} catch { /* best-effort */ }

‎tests/windows-popup-fix.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* and under a bounded timeout so a hung child cannot wedge those paths.
1010
*/
1111
import { afterEach, describe, expect, test } from "bun:test";
12+
import { readdirSync, readFileSync } from "node:fs";
13+
import { join } from "node:path";
1214

1315
import { readProcessStartMsBatch } from "../src/codex/app-server-processes";
1416
import {
@@ -85,3 +87,47 @@ describe("Windows process-lookup popup fix (#1278)", () => {
8587
}
8688
});
8789
});
90+
91+
describe("no direct PowerShell argv carries the Bun-incompatible window flag (#1589)", () => {
92+
// src/codex/user-identity.ts records the invariant in prose: PowerShell's
93+
// `-WindowStyle Hidden` CLI pair can fail under Bun 1.3.14 before the command
94+
// runs, and process-level `windowsHide` is what actually suppresses the window.
95+
// The prose held for the file that carried it and drifted everywhere else, so
96+
// this sweeps the whole runtime instead of one file.
97+
//
98+
// Only the ARGV form is forbidden. Passing `-WindowStyle Hidden` inside a
99+
// PowerShell script string (`Start-Process ... -WindowStyle Hidden`) is a
100+
// different construct that Bun never parses, and six legitimate call sites
101+
// rely on it.
102+
const runtimeFiles = (dir: string): string[] => {
103+
const out: string[] = [];
104+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
105+
const full = join(dir, entry.name);
106+
if (entry.isDirectory()) out.push(...runtimeFiles(full));
107+
else if (entry.name.endsWith(".ts")) out.push(full);
108+
}
109+
return out;
110+
};
111+
112+
// "-WindowStyle" and "Hidden" as adjacent quoted argv elements, in either
113+
// quote style, tolerating whitespace or a line break between them.
114+
const FORBIDDEN_ARGV = /["']-WindowStyle["']\s*,\s*["']Hidden["']/;
115+
116+
const srcRoot = join(import.meta.dir, "..", "src");
117+
118+
test("no src/**/*.ts passes -WindowStyle Hidden as an argv pair", () => {
119+
const offenders = runtimeFiles(srcRoot)
120+
.filter(file => FORBIDDEN_ARGV.test(readFileSync(file, "utf8")))
121+
.map(file => file.slice(srcRoot.length + 1).replaceAll("\\", "/"));
122+
expect(offenders).toEqual([]);
123+
});
124+
125+
test("the pattern accepts the script-string form and rejects the argv form", () => {
126+
// Guard the guard: if this ever stops discriminating, the sweep above is
127+
// either vacuous or about to fail six correct call sites.
128+
expect(FORBIDDEN_ARGV.test('"-NonInteractive", "-WindowStyle", "Hidden",')).toBe(true);
129+
expect(FORBIDDEN_ARGV.test("'-WindowStyle', 'Hidden'")).toBe(true);
130+
expect(FORBIDDEN_ARGV.test('" -Verb RunAs -WindowStyle Hidden -PassThru -Wait;"')).toBe(false);
131+
expect(FORBIDDEN_ARGV.test('"$startInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden"')).toBe(false);
132+
});
133+
});

0 commit comments

Comments
 (0)