Skip to content

Turn one illustration into an animated 2D mesh avatar with a coding agent and a local editor

fix: retry a watcher-locked rename during publish, then fall back to copy

bluzernamed929ae0fb9ec
diff --git a/.changeset/windows-publish-eperm.md b/.changeset/windows-publish-eperm.md
new file mode 100644
index 0000000..4a7b775
--- /dev/null
+++ b/.changeset/windows-publish-eperm.md
@@ -0,0 +1,5 @@
+---
+'mesh-avatar-studio': patch
+---
+
+Retry a locked rename during publish (EPERM/EBUSY) and fall back to copy and remove, so a real rebuild no longer fails while the dev server's watcher still holds the directory, mainly on Windows.
diff --git a/src/server/project-jobs.ts b/src/server/project-jobs.ts
index aa4435c..aa5861c 100644
--- a/src/server/project-jobs.ts
+++ b/src/server/project-jobs.ts
@@ -36,6 +36,23 @@ async function safeTree(path: string) {
if (info.isSymbolicLink()) throw new JobError('unsafeFiles', 'Project files must not be symbolic links.');
if (info.isDirectory()) for (const entry of await readdir(path)) await safeTree(join(path, entry));
}
+function wait(ms: number) { return new Promise(resolve => setTimeout(resolve, ms)); }
+// The dev server's file watcher holds directory handles under projects/. On Windows
+// that denies rename() on a directory it has open (EPERM/EBUSY), even though the same
+// rename succeeds once the watcher lets go moments later. Retry a few times first,
+// then fall back to copy+remove - the same workaround other file-watching tools use
+// for AV/watcher-locked paths on Windows - instead of failing the whole publish.
+export async function renameResilient(from: string, to: string) {
+ for (let attempt = 1; ; attempt++) {
+ try { await rename(from, to); return; }
+ catch (error) {
+ const code = (error as NodeJS.ErrnoException).code;
+ if (code !== 'EPERM' && code !== 'EBUSY') throw error;
+ if (attempt >= 5) { await copyTree(from, to); await rm(from, { recursive: true, force: true }); return; }
+ await wait(attempt * 20);
+ }
+ }
+}
async function publish(project: string, stage: string, names: string[]) {
// Validate all targets before moving anything. Keep old directories until all
// replacements succeed, and roll back on an I/O error.
@@ -45,13 +62,13 @@ async function publish(project: string, stage: string, names: string[]) {
for (const name of names) {
const target = join(project, name), previous = join(stage, `previous-${name}`);
const item = { name, hadPrevious: await exists(target), installed: false }; moved.push(item);
- if (item.hadPrevious) await rename(target, previous);
- await rename(join(stage, name), target); item.installed = true;
+ if (item.hadPrevious) await renameResilient(target, previous);
+ await renameResilient(join(stage, name), target); item.installed = true;
}
} catch (error) {
for (const item of moved.reverse()) {
if (item.installed) await rm(join(project, item.name), { recursive: true, force: true });
- if (item.hadPrevious && await exists(join(stage, `previous-${item.name}`))) await rename(join(stage, `previous-${item.name}`), join(project, item.name));
+ if (item.hadPrevious && await exists(join(stage, `previous-${item.name}`))) await renameResilient(join(stage, `previous-${item.name}`), join(project, item.name));
}
throw error;
}
diff --git a/tests/rename-resilient.test.ts b/tests/rename-resilient.test.ts
new file mode 100644
index 0000000..9b3d878
--- /dev/null
+++ b/tests/rename-resilient.test.ts
@@ -0,0 +1,71 @@
+import { afterEach, beforeEach, expect, test, vi } from 'vitest';
+import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
+import { join, resolve } from 'node:path';
+
+let failuresRemaining = 0;
+let failureCode: 'EPERM' | 'EBUSY' | 'ENOENT' = 'EPERM';
+let renameCalls = 0;
+
+vi.mock('node:fs/promises', async importOriginal => {
+ const actual = await importOriginal<typeof import('node:fs/promises')>();
+ return {
+ ...actual,
+ rename: async (from: string, to: string) => {
+ renameCalls++;
+ if (failuresRemaining > 0) {
+ failuresRemaining--;
+ const error = new Error('locked') as NodeJS.ErrnoException;
+ error.code = failureCode;
+ throw error;
+ }
+ return actual.rename(from, to);
+ },
+ };
+});
+
+const { renameResilient } = await import('../src/server/project-jobs');
+
+let root: string;
+beforeEach(async () => {
+ await mkdir(resolve('projects'), { recursive: true });
+ root = await mkdtemp(join(resolve('projects'), '.rename-test-'));
+ failuresRemaining = 0; failureCode = 'EPERM'; renameCalls = 0;
+});
+afterEach(async () => { await rm(root, { recursive: true, force: true }); });
+
+async function makeTree(path: string) {
+ await mkdir(join(path, 'nested'), { recursive: true });
+ await writeFile(join(path, 'base.png'), 'layer data');
+ await writeFile(join(path, 'nested', 'extra.png'), 'nested data');
+}
+async function readTree(path: string) {
+ return { base: await readFile(join(path, 'base.png'), 'utf8'), nested: await readFile(join(path, 'nested', 'extra.png'), 'utf8') };
+}
+
+test('retries past a transient EPERM and completes a plain rename', async () => {
+ const from = join(root, 'stage'), to = join(root, 'built');
+ await makeTree(from);
+ failuresRemaining = 2; failureCode = 'EPERM';
+ await renameResilient(from, to);
+ expect(renameCalls).toBe(3);
+ await expect(readTree(to)).resolves.toEqual({ base: 'layer data', nested: 'nested data' });
+ await expect(readFile(join(from, 'base.png'), 'utf8')).rejects.toMatchObject({ code: 'ENOENT' });
+});
+
+test('falls back to copy and remove once a watcher-locked EPERM outlasts the retries', async () => {
+ const from = join(root, 'stage'), to = join(root, 'built');
+ await makeTree(from);
+ failuresRemaining = 999; failureCode = 'EBUSY';
+ await renameResilient(from, to);
+ expect(renameCalls).toBe(5);
+ await expect(readTree(to)).resolves.toEqual({ base: 'layer data', nested: 'nested data' });
+ await expect(readFile(join(from, 'base.png'), 'utf8')).rejects.toMatchObject({ code: 'ENOENT' });
+});
+
+test('an unrelated error is not retried or papered over', async () => {
+ const from = join(root, 'stage'), to = join(root, 'built');
+ await makeTree(from);
+ failuresRemaining = 1; failureCode = 'ENOENT';
+ await expect(renameResilient(from, to)).rejects.toMatchObject({ code: 'ENOENT' });
+ expect(renameCalls).toBe(1);
+});