From e771e5e0324ce3b7e60de9a82fd39ff9805cd3bc Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 25 Sep 2026 21:10:58 +0900 Subject: [PATCH] fix(scripts): copy pnpm state files into fixture node_modules (00-01) Linking every top-level entry also linked .pnpm-workspace-state-v1.json, so pnpm running inside a fixture rewrote the repository's own install state. Top-level files are now copied; directories stay symlinks. Co-Authored-By: Claude Opus 5.5 --- scripts/lib/fixture-node-modules.ts | 13 ++++++++--- tests/unit/fixture-node-modules.test.ts | 29 +++++++++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/scripts/lib/fixture-node-modules.ts b/scripts/lib/fixture-node-modules.ts index 2b21f27d..69f62e76 100644 --- a/scripts/lib/fixture-node-modules.ts +++ b/scripts/lib/fixture-node-modules.ts @@ -1,4 +1,4 @@ -import { lstat, mkdir, readdir, symlink } from "node:fs/promises"; +import { copyFile, mkdir, readdir, stat, symlink } from "node:fs/promises"; import path from "node:path"; /** @@ -15,6 +15,12 @@ import path from "node:path"; * is an individual symlink. Package resolution is unchanged, but a recursive * delete unlinks the fixture's own symlinks instead of walking through one link * into the shared tree. + * + * Top-level files are pnpm's install state (`.modules.yaml`, + * `.pnpm-workspace-state-v1.json`, ...). pnpm rewrites them in place when it + * runs inside the fixture, so they are copied: a link would carry the fixture's + * path into the repository's own state and make its next pnpm command + * re-verify the install. */ export async function linkFixtureNodeModules( fixtureRoot: string, @@ -25,7 +31,8 @@ export async function linkFixtureNodeModules( await mkdir(target, { recursive: true }); for (const entry of await readdir(source)) { const from = path.join(source, entry); - const stats = await lstat(from); - await symlink(from, path.join(target, entry), stats.isDirectory() ? "dir" : "file"); + const to = path.join(target, entry); + if ((await stat(from)).isDirectory()) await symlink(from, to, "dir"); + else await copyFile(from, to); } } diff --git a/tests/unit/fixture-node-modules.test.ts b/tests/unit/fixture-node-modules.test.ts index 30590928..0f18369e 100644 --- a/tests/unit/fixture-node-modules.test.ts +++ b/tests/unit/fixture-node-modules.test.ts @@ -1,4 +1,4 @@ -import { mkdtemp, mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises"; +import { lstat, mkdtemp, mkdir, readdir, readFile, rm, stat, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; @@ -69,12 +69,37 @@ describe("fixture node_modules linking", () => { } }); + // pnpm rewrites `.pnpm-workspace-state-v1.json` in place when it runs inside + // the fixture. Through a symlink that write landed in the repository's own + // state file, so the next pnpm command there re-verified the install. + it("copies the pnpm state files instead of linking them", async () => { + const source = await sourceTree(); + const fixture = await mkdtemp(path.join(tmpdir(), "fixture-root-")); + const stateFiles = [".modules.yaml", ".package-map.json", ".pnpm-workspace-state-v1.json"]; + try { + for (const file of stateFiles) { + await writeFile(path.join(source, "node_modules", file), `{"projects":{"${source}":{}}}\n`); + } + await linkFixtureNodeModules(fixture, source); + + for (const file of stateFiles) { + const copied = path.join(fixture, "node_modules", file); + expect((await lstat(copied)).isSymbolicLink(), file).toBe(false); + await writeFile(copied, `{"projects":{"${fixture}":{}}}\n`); + expect(await readFile(path.join(source, "node_modules", file), "utf8"), file) + .toBe(`{"projects":{"${source}":{}}}\n`); + } + } finally { + await rm(source, { recursive: true, force: true }); + await rm(fixture, { recursive: true, force: true }); + } + }); + it("creates node_modules as a real directory, never a symlink", async () => { const source = await sourceTree(); const fixture = await mkdtemp(path.join(tmpdir(), "fixture-root-")); try { await linkFixtureNodeModules(fixture, source); - const { lstat } = await import("node:fs/promises"); const entry = await lstat(path.join(fixture, "node_modules")); expect(entry.isSymbolicLink()).toBe(false); expect(entry.isDirectory()).toBe(true);