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 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-25 21:10:58 +09:00
co-authored by Claude Opus 5.5
parent 23336f2300
commit e771e5e032
2 changed files with 37 additions and 5 deletions
+10 -3
View File
@@ -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);
}
}
+27 -2
View File
@@ -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);