Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

fix(preview): fall back to polling after watcher errors (#275)

Mohammedthowfiq0b636d9f2f41
diff --git a/archify.zip b/archify.zip
index 8d7011a4..abbbc111 100644
Binary files a/archify.zip and b/archify.zip differ
diff --git a/archify/bin/preview.mjs b/archify/bin/preview.mjs
index 449383f8..16ed94ed 100644
--- a/archify/bin/preview.mjs
+++ b/archify/bin/preview.mjs
@@ -591,6 +591,11 @@ export async function startPreview(options) {
watcher = fs.watch(path.dirname(inputPath), (event, filename) => {
if (!filename || filename.toString() === path.basename(inputPath)) observeSource();
});
+ watcher.on('error', () => {
+ const failedWatcher = watcher;
+ watcher = undefined;
+ failedWatcher?.close();
+ });
} catch (error) {
await stop();
throw new Error(`Could not watch the input directory: ${error.message}`);
diff --git a/archify/test/preview.test.mjs b/archify/test/preview.test.mjs
index fc381fbe..6ffd8cde 100644
--- a/archify/test/preview.test.mjs
+++ b/archify/test/preview.test.mjs
@@ -1,4 +1,5 @@
import { createHash } from 'node:crypto';
+import { EventEmitter } from 'node:events';
import fs from 'node:fs';
import http from 'node:http';
import os from 'node:os';
@@ -497,3 +498,33 @@ test('preview: all five typed renderers reach a verified first revision', { time
}
}
});
+
+
+test('preview: polling continues after an asynchronous watcher error', { timeout: 30000 }, async (t) => {
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'archify-preview-watch-error-'));
+ t.after(() => fs.rmSync(tmp, { recursive: true, force: true }));
+ const input = path.join(tmp, 'diagram.architecture.json');
+ const output = path.join(tmp, 'diagram.html');
+ const source = JSON.parse(fs.readFileSync(path.join(skillRoot, 'examples/web-app.architecture.json'), 'utf8'));
+ fs.writeFileSync(input, JSON.stringify(source));
+
+ const watcher = new EventEmitter();
+ let closeCount = 0;
+ watcher.close = () => { closeCount += 1; };
+ t.mock.method(fs, 'watch', () => watcher);
+ const preview = await startPreview({ type: 'architecture', input, output, open: false, pollMs: 40, debounceMs: 20 });
+ try {
+ await waitForState(preview.url, (state) => state.status === 'verified' && state.revision === 1, 'initial artifact did not verify');
+ watcher.emit('error', Object.assign(new Error('watch limit reached'), { code: 'EMFILE' }));
+ assert.equal(closeCount, 1, 'the failed watcher must be closed');
+ source.meta.title = 'Recovered using polling';
+ fs.writeFileSync(input, JSON.stringify(source));
+ await waitForState(preview.url, (state) => state.status === 'verified' && state.revision === 2, 'polling did not publish the edited source');
+ const artifact = await (await fetch(new URL('/artifact.html', preview.url))).text();
+ assert.match(artifact, /Recovered using polling/);
+ } finally {
+ await preview.stop();
+ }
+ assert.equal(closeCount, 1, 'shutdown must not close the failed watcher again');
+ await assert.rejects(fetch(preview.url));
+});