Skip to content

Commit 1cb172a

Browse files
author
RioPlay
committed
fix(scripts): stage env link/copy before replacing worktree file
Review: rmSync before symlink/copy could delete a locally edited worktree .env if the replacement then failed. Stage to a temp path and only rename over the destination after the operation succeeds; restore from backup on swap failure.
1 parent 33c0326 commit 1cb172a

2 files changed

Lines changed: 83 additions & 9 deletions

File tree

‎scripts/setup-worktree.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,27 @@ describe("setup-worktree", () => {
9191
NodeFs.rmSync(root, { recursive: true, force: true });
9292
}
9393
});
94+
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-");
98+
try {
99+
// Source path exists but is a directory so symlink("file")/copyFile both fail.
100+
NodeFs.mkdirSync(NodePath.join(root, ".env"));
101+
const destination = NodePath.join(worktree, ".env");
102+
NodeFs.writeFileSync(destination, "LOCAL=1\n", "utf8");
103+
104+
expect(() =>
105+
linkOrCopyEnvFile({
106+
projectRoot: root,
107+
worktree,
108+
relativePath: ".env",
109+
}),
110+
).toThrow();
111+
expect(NodeFs.readFileSync(destination, "utf8")).toBe("LOCAL=1\n");
112+
} finally {
113+
NodeFs.rmSync(root, { recursive: true, force: true });
114+
NodeFs.rmSync(worktree, { recursive: true, force: true });
115+
}
116+
});
94117
});

‎scripts/setup-worktree.ts‎

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export function resolveWorktreePaths(env: NodeJS.ProcessEnv = NodeProcess.env):
4040
/**
4141
* Link `projectRoot/relativePath` into the worktree. Never deletes the
4242
* destination when the source is missing (avoids wiping a local file if the
43-
* root has no `.env` yet).
43+
* root has no `.env` yet). Staging + rename keeps a locally edited worktree
44+
* file if symlink/copy fails mid-flight.
4445
*/
4546
export function linkOrCopyEnvFile(input: {
4647
readonly projectRoot: string;
@@ -56,15 +57,65 @@ export function linkOrCopyEnvFile(input: {
5657
return "skipped-missing-source";
5758
}
5859

59-
NodeFs.mkdirSync(NodePath.dirname(destination), { recursive: true });
60-
NodeFs.rmSync(destination, { force: true });
60+
const destinationDir = NodePath.dirname(destination);
61+
NodeFs.mkdirSync(destinationDir, { recursive: true });
62+
63+
// Same directory as destination so rename is same-volume (atomic on POSIX;
64+
// on Windows we still only remove the old file after staging succeeds).
65+
const staging = NodePath.join(
66+
destinationDir,
67+
`.${NodePath.basename(destination)}.${NodeProcess.pid}.${Date.now()}.tmp`,
68+
);
69+
const backup = `${staging}.bak`;
70+
71+
let result: "linked" | "copied";
6172
try {
62-
NodeFs.symlinkSync(source, destination, "file");
63-
return "linked";
64-
} catch {
65-
NodeFs.copyFileSync(source, destination);
66-
console.log(`[setup-worktree] copied ${input.relativePath} (symlink unavailable)`);
67-
return "copied";
73+
try {
74+
NodeFs.symlinkSync(source, staging, "file");
75+
result = "linked";
76+
} catch {
77+
NodeFs.copyFileSync(source, staging);
78+
result = "copied";
79+
console.log(`[setup-worktree] copied ${input.relativePath} (symlink unavailable)`);
80+
}
81+
82+
let hadDestination = false;
83+
try {
84+
NodeFs.renameSync(destination, backup);
85+
hadDestination = true;
86+
} catch (error) {
87+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") {
88+
throw error;
89+
}
90+
}
91+
92+
try {
93+
NodeFs.renameSync(staging, destination);
94+
} catch (error) {
95+
if (hadDestination) {
96+
try {
97+
NodeFs.renameSync(backup, destination);
98+
} catch {
99+
// Leave backup on disk for manual recovery.
100+
}
101+
}
102+
throw error;
103+
}
104+
105+
if (hadDestination) {
106+
NodeFs.rmSync(backup, { force: true });
107+
}
108+
return result;
109+
} catch (error) {
110+
NodeFs.rmSync(staging, { force: true });
111+
if (NodeFs.existsSync(backup) && !NodeFs.existsSync(destination)) {
112+
try {
113+
NodeFs.renameSync(backup, destination);
114+
} catch {
115+
// Leave backup on disk for manual recovery.
116+
}
117+
}
118+
throw error;
68119
}
69120
}
70121

0 commit comments

Comments
 (0)