Skip to content

Commit 6091865

Browse files
author
RioPlay
committed
fix(scripts): realpath same-path guard and skip non-file env sources
Review: resolve alone missed worktree junctions/symlinks to the project root, and a directory source could symlink-succeed on POSIX. Use realpath for identity, skip non-files without touching the destination, and cover both cases in tests.
1 parent 1cb172a commit 6091865

2 files changed

Lines changed: 53 additions & 8 deletions

File tree

‎scripts/setup-worktree.test.ts‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,26 +92,54 @@ describe("setup-worktree", () => {
9292
}
9393
});
9494

95-
it("keeps the worktree env file when staging the replacement fails", () => {
96-
const root = makeTempDir("setup-worktree-fail-root-");
97-
const worktree = makeTempDir("setup-worktree-fail-wt-");
95+
it("skips when worktree is a symlink/junction to the project root", () => {
96+
const root = makeTempDir("setup-worktree-alias-root-");
97+
const parent = makeTempDir("setup-worktree-alias-parent-");
98+
const alias = NodePath.join(parent, "wt-alias");
99+
try {
100+
NodeFs.writeFileSync(NodePath.join(root, ".env"), "ROOT=1\n", "utf8");
101+
try {
102+
NodeFs.symlinkSync(root, alias, "junction");
103+
} catch {
104+
// Junction/symlink may be denied; skip this platform-specific case.
105+
return;
106+
}
107+
expect(
108+
linkOrCopyEnvFile({
109+
projectRoot: root,
110+
worktree: alias,
111+
relativePath: ".env",
112+
}),
113+
).toBe("skipped-same-path");
114+
// Must not have replaced root/.env with a self-link/copy mess.
115+
expect(NodeFs.readFileSync(NodePath.join(root, ".env"), "utf8")).toBe("ROOT=1\n");
116+
} finally {
117+
NodeFs.rmSync(alias, { force: true });
118+
NodeFs.rmSync(root, { recursive: true, force: true });
119+
NodeFs.rmSync(parent, { recursive: true, force: true });
120+
}
121+
});
122+
123+
it("skips non-file sources without touching the worktree env", () => {
124+
const root = makeTempDir("setup-worktree-dir-root-");
125+
const worktree = makeTempDir("setup-worktree-dir-wt-");
98126
try {
99-
// Source path exists but is a directory so symlink("file")/copyFile both fail.
100127
NodeFs.mkdirSync(NodePath.join(root, ".env"));
101128
const destination = NodePath.join(worktree, ".env");
102129
NodeFs.writeFileSync(destination, "LOCAL=1\n", "utf8");
103130

104-
expect(() =>
131+
expect(
105132
linkOrCopyEnvFile({
106133
projectRoot: root,
107134
worktree,
108135
relativePath: ".env",
109136
}),
110-
).toThrow();
137+
).toBe("skipped-not-a-file");
111138
expect(NodeFs.readFileSync(destination, "utf8")).toBe("LOCAL=1\n");
112139
} finally {
113140
NodeFs.rmSync(root, { recursive: true, force: true });
114141
NodeFs.rmSync(worktree, { recursive: true, force: true });
115142
}
116143
});
144+
117145
});

‎scripts/setup-worktree.ts‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export type EnvLinkResult =
2323
| "linked"
2424
| "copied"
2525
| "skipped-same-path"
26-
| "skipped-missing-source";
26+
| "skipped-missing-source"
27+
| "skipped-not-a-file";
2728

2829
export const ENV_LINK_RELATIVE_PATHS = [".env", NodePath.join("infra", "relay", ".env")] as const;
2930

@@ -37,6 +38,15 @@ export function resolveWorktreePaths(env: NodeJS.ProcessEnv = NodeProcess.env):
3738
};
3839
}
3940

41+
/** Resolve through symlinks/junctions when the path exists; else lexical resolve. */
42+
export function resolvePathIdentity(path: string): string {
43+
try {
44+
return NodeFs.realpathSync(path);
45+
} catch {
46+
return NodePath.resolve(path);
47+
}
48+
}
49+
4050
/**
4151
* Link `projectRoot/relativePath` into the worktree. Never deletes the
4252
* destination when the source is missing (avoids wiping a local file if the
@@ -50,12 +60,19 @@ export function linkOrCopyEnvFile(input: {
5060
}): EnvLinkResult {
5161
const source = NodePath.join(input.projectRoot, input.relativePath);
5262
const destination = NodePath.join(input.worktree, input.relativePath);
53-
if (NodePath.resolve(source) === NodePath.resolve(destination)) {
63+
// realpath so a worktree that is a symlink/junction to the project root is
64+
// treated as the same path (resolve alone does not follow links).
65+
if (resolvePathIdentity(source) === resolvePathIdentity(destination)) {
5466
return "skipped-same-path";
5567
}
5668
if (!NodeFs.existsSync(source)) {
5769
return "skipped-missing-source";
5870
}
71+
// Directories (or other non-files) must not be linked/copied over a .env —
72+
// on POSIX, symlinkSync to a directory would otherwise "succeed".
73+
if (!NodeFs.statSync(source).isFile()) {
74+
return "skipped-not-a-file";
75+
}
5976

6077
const destinationDir = NodePath.dirname(destination);
6178
NodeFs.mkdirSync(destinationDir, { recursive: true });

0 commit comments

Comments
 (0)