diff --git a/packages/cli/src/commands/templates.test.ts b/packages/cli/src/commands/templates.test.ts index be007d4a96..a09f865669 100644 --- a/packages/cli/src/commands/templates.test.ts +++ b/packages/cli/src/commands/templates.test.ts @@ -205,7 +205,7 @@ describe("pullTemplate", () => { }); const pulling = pullTemplate("sample", dir).catch(() => undefined); await unpacking; - const templates = join(dir, "templates"); + const templates = join(dir, ".hyperframes", "template-pulls"); const own = readdirSync(templates).find((name) => /^sample\.part-\d+-/.test(name))!; const copy = join(templates, `sample.part-${pid}-${suffix}`); cpSync(join(templates, own), copy, { recursive: true }); @@ -229,7 +229,7 @@ describe("pullTemplate", () => { it("leaves every sibling a pull did not mark: folders, files, symlinks and unmarked old parts", async () => { serve({}); - const templates = join(dir, "templates"); + const templates = join(dir, ".hyperframes", "template-pulls"); const outside = await partLeftBy(deadPid(), "Ou7sid"); const target = join(dir, "outside-target"); cpSync(outside, target, { recursive: true }); @@ -263,7 +263,9 @@ describe("pullTemplate", () => { void pullTemplate("sample", dir); await unpacking; const parts = () => - readdirSync(join(dir, "templates")).filter((name) => name.includes(".part-")); + readdirSync(join(dir, ".hyperframes", "template-pulls")).filter((name) => + name.includes(".part-"), + ); expect(parts()).toHaveLength(1); const stop = process.listeners("SIGINT").find((listener) => !before.has(listener)); stop?.("SIGINT"); @@ -272,6 +274,28 @@ describe("pullTemplate", () => { expect(process.listeners("SIGINT").filter((listener) => !before.has(listener))).toEqual([]); }); + // Feedback, Windows: Desktop's project history read the half-unpacked files beside templates/, and the final + // rename failed with EPERM while it held one. The unpack now happens where history never looks. + it("unpacks under .hyperframes, so templates/ holds nothing but finished templates", async () => { + serve({}); + let stop!: ExecFileCallback; + const unpacking = new Promise((started) => { + unpackGate.hold = (done) => { + stop = done; + started(); + }; + }); + const pulling = pullTemplate("sample", dir).catch(() => undefined); + await unpacking; + const parts = (folder: string) => + existsSync(folder) ? readdirSync(folder).filter((name) => name.includes(".part-")) : []; + expect(parts(join(dir, "templates"))).toEqual([]); + expect(parts(join(dir, ".hyperframes", "template-pulls"))).toHaveLength(1); + unpackGate.hold = null; + stop(new Error("stopped"), "", ""); + await pulling; + }); + it("names the unreachable host instead of a bare fetch failure when offline", async () => { vi.stubGlobal( "fetch", diff --git a/packages/cli/src/commands/templates.ts b/packages/cli/src/commands/templates.ts index e8b4d4bba8..c8d68b9363 100644 --- a/packages/cli/src/commands/templates.ts +++ b/packages/cli/src/commands/templates.ts @@ -179,22 +179,27 @@ function pullOwner(path: string, name: string, dest: string): number | null { return pid !== null && markedBy(readPullMarker(path), pid) ? pid : null; } -function removeAbandonedParts(dest: string): void { - const parent = dirname(dest); - if (!existsSync(parent)) return; - for (const entry of readdirSync(parent, { withFileTypes: true })) { +function removeAbandonedParts(stage: string, dest: string): void { + if (!existsSync(stage)) return; + for (const entry of readdirSync(stage, { withFileTypes: true })) { if (!entry.isDirectory()) continue; - const path = join(parent, entry.name); + const path = join(stage, entry.name); const pid = pullOwner(path, entry.name, dest); if (pid !== null && !processIsAlive(pid)) rmSync(path, { recursive: true, force: true }); } } -/** Unpacks the pack beside `dest` and moves it in whole, so `dest` only ever holds a complete template. */ -async function unpack(data: Buffer, dest: string, pack: TemplatePack): Promise { +/** Unpacks in `stage`, which watchers skip (a held file fails the rename on Windows), then moves it in whole. */ +async function unpack( + data: Buffer, + dest: string, + stage: string, + pack: TemplatePack, +): Promise { makeOutputFolder(dirname(dest)); - removeAbandonedParts(dest); - const work = createStagingDir(dirname(dest), `${partPrefix(dest)}${process.pid}-`); + makeOutputFolder(stage); + removeAbandonedParts(stage, dest); + const work = createStagingDir(stage, `${partPrefix(dest)}${process.pid}-`); const stopWatching = removeOnStop(work); try { writeFileSync(join(work, PULL_MARKER), JSON.stringify({ owner: PULL_OWNER, pid: process.pid })); @@ -220,7 +225,8 @@ export async function pullTemplate(slug: string, dir: string): Promise { ); const dest = resolve(dir, "templates", slug); if (alreadyPulled(dest, pack)) return dest; - await unpack(await download(await bundleUrl(pack), pack), dest, pack); + const stage = resolve(dir, ".hyperframes", "template-pulls"); + await unpack(await download(await bundleUrl(pack), pack), dest, stage, pack); trackSkillCommandRan({ skill: STYLE_SAMPLING, command: "templates pull" }); return dest; }