From 9a0414637e2964bc1363e5d6cd7ba648301caa02 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 16 Nov 2026 15:09:02 +0800 Subject: [PATCH] scripts: the opencode2 and proc tests pass on Windows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit unisong reported in #1206 that tests under scripts/ fail on Windows on main. Reproduced on the Windows box at origin/main (bun test scripts): 23 pass, 8 fail. - opencode2.test.mjs took the packages folder from a file URL's pathname, "/C:/…", and join made it "\C:\…", so seven imports failed ("Cannot find module"). It is fileURLToPath now. - scripts/sync-proc.mjs had the same pathname root. proc.test.mjs imports it, so the whole file failed to load (the eighth red). - With it loading, proc.test.mjs showed five more on Windows: its fake readFile matched "/fake-proc/123/stat" while proc.mjs joins with the system's separator, and the kill(pid, 0) walk's skip list (written with "/") skipped nothing, so it walked test folders and passed its 5s bound. The fake now matches join()'s path, and the walk compares "/" paths. proc.mjs itself is unchanged: it reads /proc on Linux only. Only scripts/ changes; no package's files, so no version. Run: - win-run (Windows 11) bun test scripts: 39 pass, 0 fail (origin/main: 23 pass, 8 fail). - mica-run (macOS) bun test scripts: 39 pass; node scripts/sync-proc.mjs ok, and on Windows too. Not tried: Linux (CI runs it). --- scripts/opencode2.test.mjs | 5 ++++- scripts/proc.test.mjs | 9 ++++++--- scripts/sync-proc.mjs | 3 ++- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/scripts/opencode2.test.mjs b/scripts/opencode2.test.mjs index 6e9b2f0..7e4e09c 100644 --- a/scripts/opencode2.test.mjs +++ b/scripts/opencode2.test.mjs @@ -7,8 +7,11 @@ import { afterAll, beforeAll, expect, test } from "bun:test" import { readFileSync } from "node:fs" import { join } from "node:path" +import { fileURLToPath } from "node:url" -const packages = new URL("../packages/", import.meta.url).pathname +// a path, not a URL's pathname: on Windows that is "/C:/…", and +// join made it "\C:\…", which no import finds +const packages = fileURLToPath(new URL("../packages/", import.meta.url)) // no request leaves these tests; fetch is put back after them, since a // root `bun test` runs every package's files in this one process const realFetch = globalThis.fetch diff --git a/scripts/proc.test.mjs b/scripts/proc.test.mjs index db16363..2b4793e 100644 --- a/scripts/proc.test.mjs +++ b/scripts/proc.test.mjs @@ -3,6 +3,7 @@ const require = createRequire(import.meta.url); import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { test } from "node:test"; +import { join } from "node:path"; import { fileURLToPath } from "node:url"; import { processAlive, processExists, processProbe, processStatus, processZombie } from "./proc.mjs"; import { procTargets } from "./sync-proc.mjs"; @@ -17,7 +18,8 @@ const deps = (content, killImpl = () => {}) => ({ platform: "linux", readFile: (file) => { if (content === undefined) err("EACCES"); - if (file === "/fake-proc/123/stat") return content; + // proc.mjs joins with the system's separator, "\" on Windows + if (file === join("/fake-proc", "123", "stat")) return content; err("ENOENT"); }, kill: killImpl, @@ -125,13 +127,14 @@ test("runtime proc copies match the canonical implementation", () => { // package that asks whether a pid lives goes through its proc.mjs copy. test("no package asks kill(pid, 0) beside its proc.mjs", () => { const { readdirSync } = require("node:fs"); - const { join, relative } = require("node:path"); + const { relative, sep } = require("node:path"); const skip = /(^|\/)(node_modules|dist|test|tests|scripts|\.git)(\/|$)|\.test\.|(^|\/)proc\.mjs$|\.min\.js$|vendor/; const found = []; const walk = (dir) => { for (const entry of readdirSync(dir, { withFileTypes: true })) { const path = join(dir, entry.name); - const rel = relative(root, path); + // the skip list is written with "/", as relative() gives off Windows + const rel = relative(root, path).split(sep).join("/"); if (skip.test(rel)) continue; if (entry.isDirectory()) walk(path); else if (/\.(mjs|js|cjs|ts|mts)$/.test(entry.name) && /process\.kill\(\s*[\w.$]+\s*,\s*0\s*\)/.test(readFileSync(path, "utf8"))) found.push(rel); diff --git a/scripts/sync-proc.mjs b/scripts/sync-proc.mjs index 137a60e..82d1c42 100644 --- a/scripts/sync-proc.mjs +++ b/scripts/sync-proc.mjs @@ -1,8 +1,9 @@ #!/usr/bin/env node import { copyFileSync, mkdirSync, readFileSync } from "node:fs"; import { dirname, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; -const root = resolve(new URL("..", import.meta.url).pathname); +const root = resolve(fileURLToPath(new URL("..", import.meta.url))); const source = "scripts/proc.mjs"; export const procTargets = [ "packages/agent-console/lib/proc.mjs",