Skip to content

Commit 6dee9d1

Browse files
fix(cli): init creates its files where hard links are refused, with no Developer Mode needed (#6463)
1 parent 306f21a commit 6dee9d1

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

‎packages/cli/src/utils/projectConfig.create.test.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import {
1212

1313
vi.mock("node:fs", async (importOriginal) => {
1414
const original = await importOriginal<typeof fs>();
15-
return { ...original, readFileSync: vi.fn(original.readFileSync) };
15+
return {
16+
...original,
17+
readFileSync: vi.fn(original.readFileSync),
18+
linkSync: vi.fn(original.linkSync),
19+
};
1620
});
1721

1822
const dirs: string[] = [];
@@ -43,6 +47,18 @@ describe("project config creation", () => {
4347
expect(JSON.parse(fs.readFileSync(path, "utf-8"))).toEqual(updated);
4448
});
4549

50+
it("creates the config on a volume or sandbox that refuses hard links (EPERM)", () => {
51+
const dir = project();
52+
vi.mocked(fs.linkSync).mockImplementationOnce(() => {
53+
throw Object.assign(new Error("operation not permitted"), { code: "EPERM" });
54+
});
55+
createProjectConfig(dir);
56+
expect(JSON.parse(fs.readFileSync(projectConfigPath(dir), "utf-8"))).toEqual(
57+
DEFAULT_PROJECT_CONFIG,
58+
);
59+
expect(fs.readdirSync(dir)).toEqual(["hyperframes.json"]);
60+
});
61+
4662
it("preserves a config created after the skill seed reads ENOENT", () => {
4763
const dir = project();
4864
const path = projectConfigPath(dir);
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
import { linkSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2-
import { dirname, join } from "node:path";
1+
import { createFileAtomically } from "@hyperframes/core/atomic-file";
32

43
/** Publish complete content without replacing or following an existing destination entry. */
54
export function writeNewFileSync(filePath: string, content: string): void {
6-
const stagingDir = mkdtempSync(join(dirname(filePath), ".hf-create-"));
75
try {
8-
const stagedPath = join(stagingDir, "content");
9-
writeFileSync(stagedPath, content, { encoding: "utf-8", flag: "wx" });
10-
try {
11-
linkSync(stagedPath, filePath);
12-
} catch (err) {
13-
if (!(err instanceof Error && "code" in err && err.code === "EEXIST")) throw err;
14-
}
15-
} finally {
16-
rmSync(stagingDir, { recursive: true, force: true });
6+
createFileAtomically(filePath, content);
7+
} catch (err) {
8+
if (!(err instanceof Error && "code" in err && err.code === "EEXIST")) throw err;
179
}
1810
}

0 commit comments

Comments
 (0)