Skip to content

Commit 0406f9a

Browse files
committed
fix(studio-server): undo and redo put a file back though Windows holds it for a moment
1 parent 312a8fd commit 0406f9a

3 files changed

Lines changed: 70 additions & 6 deletions

File tree

‎packages/core/src/atomicFile.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function replaceFileAtomically(
3434
): void {
3535
// Node fs.rename uses libuv uv_fs_rename; win32 calls MoveFileExW with MOVEFILE_REPLACE_EXISTING.
3636
publishSibling(filePath, content, mode, operations, (tempPath) =>
37-
renameWithRetry(operations, tempPath, filePath),
37+
renameWithRetry(tempPath, filePath, operations),
3838
);
3939
}
4040

@@ -156,10 +156,11 @@ function writeTempSibling(
156156
}
157157
}
158158

159-
function renameWithRetry(
160-
operations: Pick<typeof fs, "renameSync">,
159+
/** renameSync onto `to`, again after short waits while Windows briefly holds the target open. */
160+
export function renameWithRetry(
161161
from: string,
162162
to: string,
163+
operations: Pick<typeof fs, "renameSync"> = fs,
163164
): void {
164165
for (let attempt = 0; ; attempt++) {
165166
try {

‎packages/studio-server/src/history/blobStore.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { createHash, randomUUID } from "node:crypto";
2-
import { constants, createReadStream, createWriteStream, renameSync } from "node:fs";
2+
import { constants, createReadStream, createWriteStream } from "node:fs";
33
import { copyFile, mkdir, readdir, readFile, rename, rm, stat } from "node:fs/promises";
44
import { dirname, join } from "node:path";
55
import { pipeline } from "node:stream/promises";
6-
import { atomicTempPath } from "@hyperframes/core/atomic-file";
6+
import { atomicTempPath, renameWithRetry } from "@hyperframes/core/atomic-file";
77

88
/** File contents stored once by sha256, text and binary alike. */
99
export interface BlobStore {
@@ -53,7 +53,7 @@ async function cloneOrCopy(from: string, to: string, beforeReplace?: () => void)
5353
try {
5454
await copyFile(from, temp, constants.COPYFILE_FICLONE);
5555
beforeReplace?.();
56-
renameSync(temp, to);
56+
renameWithRetry(temp, to);
5757
} catch (error) {
5858
await rm(temp, { force: true });
5959
throw error;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// @vitest-environment node
2+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import { basename, join } from "node:path";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
6+
import type { HistoryWho } from "./historyLog";
7+
import { openProjectHistory } from "./projectHistory";
8+
9+
// Windows refuses a rename onto a file another process has open for a moment (an indexer, antivirus).
10+
const held = vi.hoisted(() => ({ name: null as string | null, refused: 0 }));
11+
vi.mock("node:fs", async (importOriginal) => {
12+
const original = await importOriginal<typeof import("node:fs")>();
13+
const renameSync = ((from: string, to: string) => {
14+
if (held.name && basename(String(to)) === held.name && held.refused === 0) {
15+
held.refused += 1;
16+
throw Object.assign(new Error(`EPERM: operation not permitted, rename '${from}'`), {
17+
code: "EPERM",
18+
syscall: "rename",
19+
});
20+
}
21+
return original.renameSync(from, to);
22+
}) as typeof original.renameSync;
23+
return { ...original, renameSync };
24+
});
25+
26+
const you: HistoryWho = { kind: "person", name: "You" };
27+
const cleanup: Array<() => unknown> = [];
28+
29+
afterEach(async () => {
30+
held.name = null;
31+
held.refused = 0;
32+
for (const step of cleanup.splice(0).reverse()) await step();
33+
});
34+
35+
function tempDir(prefix: string): string {
36+
const dir = mkdtempSync(join(tmpdir(), prefix));
37+
cleanup.push(() => rmSync(dir, { recursive: true, force: true }));
38+
return dir;
39+
}
40+
41+
describe("a project file held open for a moment", () => {
42+
it("still takes Undo and Redo: the file is put back once the hold lets go", async () => {
43+
const projectDir = tempDir("hf-history-held-");
44+
const historyRoot = tempDir("hf-history-held-root-");
45+
const file = join(projectDir, "index.html");
46+
writeFileSync(file, "v1");
47+
const history = await openProjectHistory({ projectDir, historyRoot });
48+
cleanup.push(() => history.close());
49+
const window = await history.beginWindow(you, "Your edit");
50+
writeFileSync(file, "v2");
51+
await window.close();
52+
53+
held.name = "index.html";
54+
expect(await history.step("back", you)).toMatchObject({ ok: true });
55+
expect(held.refused).toBe(1);
56+
expect(readFileSync(file, "utf-8")).toBe("v1");
57+
58+
held.refused = 0;
59+
expect(await history.step("forward", you)).toMatchObject({ ok: true });
60+
expect(held.refused).toBe(1);
61+
expect(readFileSync(file, "utf-8")).toBe("v2");
62+
});
63+
});

0 commit comments

Comments
 (0)