Run Windows batch launchers via shell and harden test cleanup

- Launch Windows .bat/.cmd wrappers directly with `shell` set to COMSPEC to preserve argument/path handling (including spaced paths)
- Update process runner tests to cover the new Windows launch behavior
- Add `removeDirectoryBestEffort` and use it in integration/tests to reduce flaky temp-dir cleanup failures
This commit is contained in:
Julius Marminge committed 2026-03-05 16:14:36 -08:00
1 parent e5cea81fea
commit 870d6943ed
6 files changed
+89 -38

No files matched your search

@@ -57,6 +57,7 @@ import {
type TestProviderAdapterHarness,
} from "./TestProviderAdapter.integration.ts";
import { ServerConfig } from "../src/config.ts";
import { removeDirectoryBestEffort } from "../src/testUtils/removeDirectoryBestEffort.ts";
function runGit(cwd: string, args: ReadonlyArray<string>) {
return execFileSync("git", args, {
@@ -388,9 +389,7 @@ export const makeOrchestrationIntegrationHarness = Effect.gen(function* () {
yield* shutdown.pipe(
Effect.ensuring(
Effect.sync(() => {
fs.rmSync(rootDir, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 });
}),
Effect.promise(() => removeDirectoryBestEffort(rootDir)),
),
);
});
+4 -3
View File
@@ -1,4 +1,4 @@
import { existsSync, mkdtempSync, rmSync } from "node:fs";
import { existsSync, mkdtempSync } from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -13,6 +13,7 @@ import { GitCoreLive } from "./GitCore.ts";
import { GitCore, type GitCoreShape } from "../Services/GitCore.ts";
import { GitCommandError } from "../Errors.ts";
import { runProcess } from "../../processRunner.ts";
import { removeDirectoryBestEffort } from "../../testUtils/removeDirectoryBestEffort.ts";
// ── Helpers ──
@@ -24,9 +25,9 @@ const GitCoreTestLayer = GitCoreLive.pipe(
const TestLayer = Layer.mergeAll(NodeServices.layer, GitServiceTestLayer, GitCoreTestLayer);
const tempDirs = new Set<string>();
afterEach(() => {
afterEach(async () => {
for (const dir of tempDirs) {
rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 });
await removeDirectoryBestEffort(dir);
}
tempDirs.clear();
});
@@ -38,6 +38,7 @@ import {
} from "../../provider/Services/ProviderService.ts";
import { checkpointRefForThreadTurn } from "../../checkpointing/Utils.ts";
import { ServerConfig } from "../../config.ts";
import { removeDirectoryBestEffort } from "../../testUtils/removeDirectoryBestEffort.ts";
const asProjectId = (value: string): ProjectId => ProjectId.makeUnsafe(value);
const asSessionId = (value: string): ProviderSessionId => ProviderSessionId.makeUnsafe(value);
@@ -210,7 +211,7 @@ describe("CheckpointReactor", () => {
while (tempDirs.length > 0) {
const dir = tempDirs.pop();
if (dir) {
fs.rmSync(dir, { recursive: true, force: true, maxRetries: 10, retryDelay: 50 });
await removeDirectoryBestEffort(dir);
}
}
});
+38 -9
View File
@@ -102,10 +102,9 @@ describe("resolveProcessLaunchPlan", () => {
},
});
expect(plan.command).toBe("C:\\Windows\\System32\\cmd.exe");
expect(plan.args.slice(0, 3)).toEqual(["/d", "/s", "/c"]);
expect(plan.args[3]).toBe(`"${wrapperPath}" "C:\\repo\\a&b.ts"`);
expect(plan.shell).toBe(false);
expect(plan.command).toBe(wrapperPath);
expect(plan.args).toEqual(["C:\\repo\\a&b.ts"]);
expect(plan.shell).toBe("C:\\Windows\\System32\\cmd.exe");
});
});
@@ -130,8 +129,38 @@ describe("resolveProcessLaunchPlan", () => {
},
});
expect(plan.command).toBe("C:\\Windows\\System32\\cmd.exe");
expect(plan.args[3]).toBe(`"${wrapperPath}" "pkg@^1.0"`);
expect(plan.command).toBe(wrapperPath);
expect(plan.args).toEqual(["pkg@^1.0"]);
expect(plan.shell).toBe("C:\\Windows\\System32\\cmd.exe");
});
});
it("preserves spaced batch launcher paths on windows", () => {
withTempDir((rootDir) => {
const dir = path.join(rootDir, "runner admin");
fs.mkdirSync(dir);
const wrapperPath = path.join(dir, "code.CMD");
fs.writeFileSync(wrapperPath, "@echo off\r\n");
const plan = resolveProcessLaunchPlan("code", ["--status"], {
env: {
PATH: dir,
PATHEXT: ".COM;.EXE;.BAT;.CMD",
COMSPEC: "C:\\Windows\\System32\\cmd.exe",
},
inheritParentEnv: false,
runtimeEnvironment: {
platform: "windows",
pathStyle: "windows",
isWsl: false,
windowsInteropMode: "windows-native",
wslDistroName: null,
},
});
expect(plan.command).toBe(wrapperPath);
expect(plan.args).toEqual(["--status"]);
expect(plan.shell).toBe("C:\\Windows\\System32\\cmd.exe");
});
});
@@ -191,9 +220,9 @@ describe("makeRuntimeCommand", () => {
},
});
expect(command.command).toBe("C:\\Windows\\System32\\cmd.exe");
expect(command.args).toEqual(["/d", "/s", "/c", `"${wrapperPath}" "C:\\repo\\a&b.ts"`]);
expect(command.options.shell).toBe(false);
expect(command.command).toBe(wrapperPath);
expect(command.args).toEqual(["C:\\repo\\a&b.ts"]);
expect(command.options.shell).toBe("C:\\Windows\\System32\\cmd.exe");
});
});
});
+3 -22
View File
@@ -164,25 +164,6 @@ function resolveWindowsCommand(
return null;
}
function quoteWindowsBatchArgument(argument: string): string {
if (argument.length === 0) {
return '""';
}
const escaped = argument
.replaceAll("%", "%%")
.replace(/(\\*)"/g, (_match, slashes: string) => `${slashes}${slashes}\\"`)
.replace(/(\\+)$/g, "$1$1");
return `"${escaped}"`;
}
function buildWindowsBatchCommandLine(
command: string,
args: ReadonlyArray<string>,
): string {
return [quoteWindowsBatchArgument(command), ...args.map(quoteWindowsBatchArgument)].join(" ");
}
function resolveWindowsCommandShell(env: NodeJS.ProcessEnv): string {
return env.ComSpec ?? env.COMSPEC ?? process.env.ComSpec ?? process.env.COMSPEC ?? "cmd.exe";
}
@@ -224,9 +205,9 @@ export function resolveProcessLaunchPlan(
if (resolved.kind === "batch") {
return {
command: resolveWindowsCommandShell(env),
args: ["/d", "/s", "/c", buildWindowsBatchCommandLine(resolved.path, args)],
shell: false,
command: resolved.path,
args: [...args],
shell: resolveWindowsCommandShell(env),
runtimeEnvironment,
};
}
@@ -0,0 +1,40 @@
import fs from "node:fs";
const RETRYABLE_REMOVE_ERROR_CODES = new Set(["EBUSY", "ENOTEMPTY", "EPERM"]);
function isRetryableRemoveError(error: unknown): boolean {
if (!error || typeof error !== "object") {
return false;
}
const code = "code" in error ? error.code : undefined;
return typeof code === "string" && RETRYABLE_REMOVE_ERROR_CODES.has(code);
}
export async function removeDirectoryBestEffort(
dir: string,
options?: {
readonly maxRetries?: number;
readonly retryDelayMs?: number;
},
): Promise<void> {
const maxRetries = options?.maxRetries ?? 20;
const retryDelayMs = options?.retryDelayMs ?? 100;
for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
try {
fs.rmSync(dir, { recursive: true, force: true });
return;
} catch (error) {
if (!isRetryableRemoveError(error)) {
throw error;
}
if (attempt === maxRetries) {
return;
}
await new Promise((resolve) => setTimeout(resolve, retryDelayMs));
}
}
}