diff --git a/packages/cli/src/utils/privateProjectFile.test.ts b/packages/cli/src/utils/privateProjectFile.test.ts index d270bf4031..6d601d4f5f 100644 --- a/packages/cli/src/utils/privateProjectFile.test.ts +++ b/packages/cli/src/utils/privateProjectFile.test.ts @@ -29,6 +29,7 @@ import { const zeroFileIds = vi.hoisted(() => ({ on: false })); const opens = vi.hoisted(() => ({ path: "", seen: 0, replacing: 0 })); +const refusals = vi.hoisted(() => ({ ending: "", left: 0 })); vi.mock("node:fs", async (importOriginal) => { const actual = await importOriginal(); @@ -37,6 +38,12 @@ vi.mock("node:fs", async (importOriginal) => { return zeroFileIds.on && stat ? Object.assign(stat, { dev: 0n, ino: 0n }) : stat; }) as typeof actual.lstatSync; const openSync = ((...args: Parameters) => { + const opened = String(args[0]).replaceAll("\\", "/"); + if (refusals.left > 0 && refusals.ending && opened.endsWith(refusals.ending)) { + refusals.left -= 1; + const code = process.platform === "win32" ? "EPERM" : "EBUSY"; + throw Object.assign(new Error(`${code}: open '${opened}'`), { code }); + } const fd = actual.openSync(...args); if (args[0] !== opens.path) return fd; opens.seen += 1; @@ -55,6 +62,7 @@ let dir: string | undefined; afterEach(() => { zeroFileIds.on = false; Object.assign(opens, { path: "", seen: 0, replacing: 0 }); + Object.assign(refusals, { ending: "", left: 0 }); vi.unstubAllEnvs(); if (dir) rmSync(dir, { recursive: true, force: true }); dir = undefined; @@ -551,6 +559,17 @@ describe("recordReceivedFiles", () => { expect(listed(root)).toEqual(["hyperframes.json", "mine.html", "theirs.html"]); }); + // fails without the fix: Windows refuses a new lock with EPERM while the last writer's is still being deleted, and + // that failed the write instead of waiting (CI: a Windows run of the test above). + it("waits while the lock file is held (EPERM on Windows, EBUSY elsewhere), then takes it", () => { + const root = project(); + // Matched by its ending: Windows may spell the temp folder short (RUNNER~1) where the code resolves it long. + Object.assign(refusals, { ending: "/.hyperframes/received-files.json.lock", left: 2 }); + expect(recordReceivedFiles(root, ["mine.html"])).toEqual(["mine.html"]); + expect(refusals.left).toBe(0); + expect(listed(root)).toEqual(["hyperframes.json", "mine.html"]); + }); + it("takes a project folder however its path is spelled", () => { const root = join(project(), "Project"); mkdirSync(root); diff --git a/packages/cli/src/utils/privateProjectFile.ts b/packages/cli/src/utils/privateProjectFile.ts index 2f21bb02ac..c3ed729141 100644 --- a/packages/cli/src/utils/privateProjectFile.ts +++ b/packages/cli/src/utils/privateProjectFile.ts @@ -26,6 +26,7 @@ import { } from "@hyperframes/core/safe-path"; import { registryRoot } from "../registry/publication.js"; import { readPlainTail, recordFolder, writeRecord } from "./projectRecords.js"; +import { isHeldError } from "./renderLockOwners.js"; import { walkBudget } from "./walkBudget.js"; import { contentHash, @@ -283,13 +284,20 @@ function sleepSync(ms: number): void { const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); -function tryRecordLock(lock: string, deadline: number): "taken" | "wait" { +function openRecordLock(lock: string, deadline: number): "taken" | "held" | "exists" { try { closeSync(openSync(lock, "wx")); return "taken"; } catch (error) { + if (isHeldError(error) && Date.now() < deadline) return "held"; if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error; + return "exists"; } +} + +function tryRecordLock(lock: string, deadline: number): "taken" | "wait" { + const opened = openRecordLock(lock, deadline); + if (opened !== "exists") return opened === "taken" ? "taken" : "wait"; const heldFor = Date.now() - (statSync(lock, { throwIfNoEntry: false })?.mtimeMs ?? Date.now()); if (heldFor > LOCK_STALE_MS || heldFor < -LOCK_CLOCK_SLACK_MS) rmSync(lock, { force: true }); else if (Date.now() >= deadline)