Multiplayer agent harness for work.
Launch Windows batch shims through cmd.exe in the dev runner (#787) (#1703)
Sina Matianbac780bd9e56
diff --git a/scripts/dev/lib/proc.ts b/scripts/dev/lib/proc.tsindex 3f55c0ae1..7d8a46ef2 100644--- a/scripts/dev/lib/proc.ts+++ b/scripts/dev/lib/proc.ts@@ -1,17 +1,38 @@import { spawn, spawnSync } from "node:child_process";-import { openSync } from "node:fs";+import { openSync, statSync } from "node:fs";import { connect, createServer } from "node:net";+import { join } from "node:path";import { bestEffort, sleep } from "./util.ts";+export function spawnCommand(+ cmd: string,+ args: string[],+ env: NodeJS.ProcessEnv = process.env,+ platform: NodeJS.Platform = process.platform,+): { cmd: string; args: string[]; windowsVerbatimArguments: boolean } {+ if (platform !== "win32") return { cmd, args, windowsVerbatimArguments: false };+ const dirs = /[\\/]/.test(cmd) ? [""] : (env.PATH ?? env.Path ?? "").split(";").filter(Boolean);+ const exts = [...(env.PATHEXT ?? ".COM;.EXE;.BAT;.CMD").split(";").filter(Boolean), ""];+ const resolved =+ dirs+ .flatMap((dir) => exts.map((ext) => (dir ? join(dir, cmd + ext) : cmd + ext)))+ .find((candidate) => statSync(candidate, { throwIfNoEntry: false })?.isFile()) ?? cmd;+ if (!/\.(cmd|bat)$/i.test(resolved)) return { cmd: resolved, args, windowsVerbatimArguments: false };+ const line = [resolved, ...args].map((part) => `"${part.replaceAll('"', '""')}"`).join(" ");+ return { cmd: env.ComSpec ?? "cmd.exe", args: ["/d", "/s", "/c", `"${line}"`], windowsVerbatimArguments: true };+}+export function run(cmd: string,args: string[],opts: { cwd?: string; env?: NodeJS.ProcessEnv; input?: string; timeoutMs?: number } = {},): Promise<{ code: number; stdout: string; stderr: string }> {return new Promise((resolve) => {- const child = spawn(cmd, args, {+ const command = spawnCommand(cmd, args, opts.env);+ const child = spawn(command.cmd, command.args, {cwd: opts.cwd,env: opts.env,+ windowsVerbatimArguments: command.windowsVerbatimArguments,timeout: opts.timeoutMs ?? 120_000,killSignal: "SIGKILL",stdio: [opts.input !== undefined ? "pipe" : "ignore", "pipe", "pipe"],@@ -58,8 +79,10 @@ export function spawnDetached(opts: {const [cmd, ...rest] = opts.argv;if (!cmd) throw new Error("spawnDetached: empty argv");const fd = openSync(opts.logFile, "a");- const child = spawn(cmd, rest, {+ const command = spawnCommand(cmd, rest, opts.env);+ const child = spawn(command.cmd, command.args, {cwd: opts.cwd,+ windowsVerbatimArguments: command.windowsVerbatimArguments,detached: true,stdio: ["ignore", fd, fd],env: opts.env,diff --git a/scripts/dev/supervisor/children.ts b/scripts/dev/supervisor/children.tsindex 79a2b45f1..b40c6977c 100644--- a/scripts/dev/supervisor/children.ts+++ b/scripts/dev/supervisor/children.ts@@ -2,7 +2,7 @@ import { spawn, type ChildProcess } from "node:child_process";import { openSync, readFileSync, statSync } from "node:fs";import { join } from "node:path";import { writePidFile } from "../lib/lease.ts";-import { tcpPortOpen, waitPortFree } from "../lib/proc.ts";+import { spawnCommand, tcpPortOpen, waitPortFree } from "../lib/proc.ts";import { bestEffort, bestEffortValue, sleep } from "../lib/util.ts";import type { ChildSpec, ChildState, ChildStatus } from "../lib/types.ts";@@ -53,8 +53,10 @@ export class Child {if (!cmd) return { ok: false, detail: "empty argv" };this.logOffset = bestEffortValue(() => statSync(this.logFile()).size) ?? 0;const fd = openSync(this.logFile(), "a");- const proc = spawn(cmd, rest, {+ const command = spawnCommand(cmd, rest, this.spec.env);+ const proc = spawn(command.cmd, command.args, {cwd: this.spec.cwd,+ windowsVerbatimArguments: command.windowsVerbatimArguments,detached: true,stdio: ["ignore", fd, fd],env: this.spec.env,diff --git a/test/dev-cli-lib.test.ts b/test/dev-cli-lib.test.tsindex ec022746a..722bff65f 100644--- a/test/dev-cli-lib.test.ts+++ b/test/dev-cli-lib.test.ts@@ -4,6 +4,7 @@ import { chmodSync, existsSync, mkdtempSync, mkdirSync, rmSync, writeFileSync }import { createServer } from "node:net";import { spawnSync } from "node:child_process";import { ensureDeps } from "../scripts/dev/lib/deps.ts";+import { spawnCommand } from "../scripts/dev/lib/proc.ts";import { tmpdir } from "node:os";import { join } from "node:path";import { envSha, errMessage, formatAge, readEnvFile } from "../scripts/dev/lib/util.ts";@@ -629,3 +630,21 @@ test("dev CLI rejects invalid or conflicting surface selection before startup",assert.match(result.stderr, /surface/);}});++test("spawnCommand routes Windows batch shims through cmd.exe with quoted arguments", () => {+ const dir = mkdtempSync(join(tmpdir(), "qm dev shim "));+ try {+ const shim = join(dir, "npm.cmd");+ writeFileSync(shim, "");+ const env = { PATH: dir, PATHEXT: ".EXE;.cmd", ComSpec: "C:\\Windows\\system32\\cmd.exe" };+ const args = ["run", "build", "--", "a b", 'say "hi"'];+ assert.deepEqual(spawnCommand("npm", args, env, "win32"), {+ cmd: "C:\\Windows\\system32\\cmd.exe",+ args: ["/d", "/s", "/c", `""${shim}" "run" "build" "--" "a b" "say ""hi""""`],+ windowsVerbatimArguments: true,+ });+ assert.deepEqual(spawnCommand("npm", args, env, "darwin"), { cmd: "npm", args, windowsVerbatimArguments: false });+ } finally {+ rmSync(dir, { recursive: true, force: true });+ }+});