Skip to content

armgit / Ix

Public Imported from GitHub
0

Understand any codebase instantly. System intelligence for codebases, built for humans and AI.

fix(watch): fall back to polling instead of dying on a watcher error

KageBinaryd1274266ba99
diff --git a/ix-cli/src/cli/__tests__/ingest-watch-followups.test.ts b/ix-cli/src/cli/__tests__/ingest-watch-followups.test.ts
new file mode 100644
index 00000000..60d6ccb3
--- /dev/null
+++ b/ix-cli/src/cli/__tests__/ingest-watch-followups.test.ts
@@ -0,0 +1,34 @@
+// Copyright 2026 Ix Infrastructure Inc.
+
+import { describe, expect, it } from "vitest";
+
+import { githubCommitFailure } from "../commands/ingest.js";
+import { canPollInstead } from "../commands/watch.js";
+
+describe("ix ingest --github after the base-rev retries", () => {
+ it("is a failure when the last attempt still lost the race: nothing was written", () => {
+ expect(githubCommitFailure({ status: "BaseRevMismatch" })).toMatch(/nothing was ingested/);
+ });
+
+ it("is a success otherwise", () => {
+ for (const status of ["Ok", "Idempotent", undefined]) {
+ expect(githubCommitFailure({ status })).toBeUndefined();
+ }
+ });
+});
+
+describe("ix watch falls back to polling", () => {
+ const err = (code: string) => Object.assign(new Error(code), { code });
+
+ it("when recursive watching is unavailable, or the OS is out of watches or descriptors", () => {
+ for (const code of ["ERR_FEATURE_UNAVAILABLE_ON_PLATFORM", "ENOSPC", "EMFILE", "ENFILE"]) {
+ expect(canPollInstead(err(code)), code).toBe(true);
+ }
+ });
+
+ it("but not for a real error such as a permission problem", () => {
+ for (const code of ["EACCES", "EPERM"]) expect(canPollInstead(err(code)), code).toBe(false);
+ expect(canPollInstead(new Error("boom"))).toBe(false);
+ expect(canPollInstead(undefined)).toBe(false);
+ });
+});
diff --git a/ix-cli/src/cli/commands/watch.ts b/ix-cli/src/cli/commands/watch.ts
index 1b04c1c8..ae1a7ea4 100644
--- a/ix-cli/src/cli/commands/watch.ts
+++ b/ix-cli/src/cli/commands/watch.ts
@@ -315,7 +315,13 @@ export function registerWatchCommand(program: Command): void {
if (migrated) {
console.error(chalk.dim("[watch] Workspace migrated to a stable id; re-ingesting once before watching..."));
prepareMigratedWorkspaceRefresh(root);
- await refresh();
+ // A failed first refresh used to escape and end the watch before it
+ // began; the next change retries it like any other refresh.
+ try {
+ await refresh();
+ } catch (err) {
+ console.error(`${chalk.red("[watch]")} refresh error: ${(err as Error).message}`);
+ }
}
const relative = path.relative(root, watchPath) || ".";
@@ -332,6 +338,14 @@ export function registerWatchCommand(program: Command): void {
const fullPath = path.resolve(watchPath, filename);
if (shouldWatch(root, fullPath)) batch.notify(fullPath);
});
+ // An FSWatcher reports a later failure (the watched directory removed,
+ // inotify exhausted) as an 'error' event; with no listener that is an
+ // uncaught exception and the watch dies. Poll instead.
+ watcher.on("error", (err) => {
+ watcher.close();
+ console.error(`${chalk.red("[watch]")} file watcher failed (${describeWatchError(err)}); falling back to polling (2s interval).`);
+ pollMode(watchPath, root, scheduler);
+ });
// Keep process alive
process.on("SIGINT", () => {
@@ -342,8 +356,8 @@ export function registerWatchCommand(program: Command): void {
});
} catch (err: any) {
// Fallback to polling if fs.watch with recursive isn't supported
- if (err.code === "ERR_FEATURE_UNAVAILABLE_ON_PLATFORM") {
- console.log(chalk.dim("[watch] Falling back to polling mode (2s interval)..."));
+ if (canPollInstead(err)) {
+ console.log(chalk.dim(`[watch] Falling back to polling mode (2s interval): ${describeWatchError(err)}`));
pollMode(watchPath, root, scheduler);
} else {
throw err;
@@ -353,7 +367,26 @@ export function registerWatchCommand(program: Command): void {
}
/**
- * Fallback polling mode for platforms where recursive fs.watch isn't available.
+ * Whether an fs.watch failure can be worked around by polling: recursive
+ * watching unsupported, or the OS out of watches or descriptors (ENOSPC is
+ * Linux's inotify limit, EMFILE/ENFILE open files). Anything else is a real
+ * error, e.g. a permission problem, and stays one.
+ */
+export function canPollInstead(err: unknown): boolean {
+ const code = (err as NodeJS.ErrnoException | undefined)?.code;
+ return code === "ERR_FEATURE_UNAVAILABLE_ON_PLATFORM" || code === "ENOSPC" || code === "EMFILE" || code === "ENFILE";
+}
+
+function describeWatchError(err: unknown): string {
+ const e = err as NodeJS.ErrnoException | undefined;
+ return e?.code === "ENOSPC"
+ ? "ENOSPC: the system's inotify watch limit is reached (raise fs.inotify.max_user_watches)"
+ : e?.code ?? e?.message ?? String(err);
+}
+
+/**
+ * Fallback polling mode for platforms where recursive fs.watch isn't available,
+ * or where it failed.
*/
function pollMode(
watchPath: string,