@mnemonik/scanner 5.131.2 → 5.135.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/watcher.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { watch, type FSWatcher } from 'fs';
2
2
  import { join, relative } from 'path';
3
- import { readdir } from 'fs/promises';
3
+ import { readdir, stat } from 'fs/promises';
4
4
  import { isGitBoundary } from '@mnemonik/shared';
5
5
 
6
6
  const SKIP_DIRS = new Set([
@@ -27,7 +27,15 @@ export type ChangeHandler = (changedFiles: string[]) => void;
27
27
  export type ErrorHandler = (err: Error) => void;
28
28
 
29
29
  export class FileWatcher {
30
- private watchers: FSWatcher[] = [];
30
+ // Keyed by directory so a watcher can be closed and re-attached when the
31
+ // directory is deleted and recreated at the same path.
32
+ private watchers = new Map<string, FSWatcher>();
33
+ private watchedDirs = new Set<string>();
34
+ // Inode per watched directory: a delete+recreate faster than the delete
35
+ // event's stat() presents as "directory exists and is already watched",
36
+ // but the live watcher is bound to the OLD inode and is inert. Comparing
37
+ // inodes at event time detects the swap so the subtree can re-attach.
38
+ private watchedDirInodes = new Map<string, number>();
31
39
  private pendingFiles = new Set<string>();
32
40
  private flushTimer: ReturnType<typeof setTimeout> | null = null;
33
41
  private debounceMs: number;
@@ -49,10 +57,12 @@ export class FileWatcher {
49
57
  }
50
58
 
51
59
  stop(): void {
52
- for (const w of this.watchers) {
60
+ for (const w of this.watchers.values()) {
53
61
  w.close();
54
62
  }
55
- this.watchers = [];
63
+ this.watchers.clear();
64
+ this.watchedDirs.clear();
65
+ this.watchedDirInodes.clear();
56
66
  if (this.flushTimer) {
57
67
  clearTimeout(this.flushTimer);
58
68
  this.flushTimer = null;
@@ -74,14 +84,29 @@ export class FileWatcher {
74
84
  private async watchDir(dir: string): Promise<void> {
75
85
  const dirName = dir.split('/').pop() ?? '';
76
86
  if (SKIP_DIRS.has(dirName)) return;
87
+ // Dedup guard — the change callback re-enters watchDir for new
88
+ // subdirectories, and a rapid create/rename burst can resolve the same
89
+ // path twice before the first watch is registered.
90
+ if (this.watchedDirs.has(dir)) return;
91
+ this.watchedDirs.add(dir);
77
92
 
78
93
  try {
79
- const watcher = watch(dir, { persistent: true }, (_event, filename) => {
94
+ // Record the inode BEFORE attaching so watchNewDir can distinguish a
95
+ // benign event on this directory from a recreate-at-same-path.
96
+ this.watchedDirInodes.set(dir, (await stat(dir)).ino);
97
+ const watcher = watch(dir, { persistent: true }, (event, filename) => {
80
98
  if (!filename) return;
81
99
  const fullPath = join(dir, filename);
82
100
  const relPath = relative(this.rootPath, fullPath);
83
101
  this.pendingFiles.add(relPath);
84
102
  this.scheduleFlush();
103
+ // fs.watch is non-recursive: the initial recursion below only covers
104
+ // directories that existed at start(). When this event is a newly
105
+ // created directory, attach a watcher to it too — otherwise the
106
+ // subtree is a permanent blind spot until restart. Fire-and-forget;
107
+ // watchNewDir no-ops for plain files, skip dirs, git boundaries, and
108
+ // already-watched paths.
109
+ void this.watchNewDir(fullPath, event);
85
110
  });
86
111
 
87
112
  watcher.on('error', (err) => {
@@ -91,7 +116,7 @@ export class FileWatcher {
91
116
  }
92
117
  });
93
118
 
94
- this.watchers.push(watcher);
119
+ this.watchers.set(dir, watcher);
95
120
 
96
121
  const entries = await readdir(dir, { withFileTypes: true });
97
122
  for (const entry of entries) {
@@ -104,8 +129,91 @@ export class FileWatcher {
104
129
  await this.watchDir(child);
105
130
  }
106
131
  }
132
+ } catch (err) {
133
+ // Watch registration failed. Never fatal for the subtree's parent, but
134
+ // the *reason* matters: inotify/fd limit exhaustion means silently
135
+ // growing blind spots, while permission-denied is a benign property of
136
+ // the directory itself. Drop the bookkeeping (and any watcher that did
137
+ // attach before the failure) so a later retry can re-enter cleanly.
138
+ this.watchedDirs.delete(dir);
139
+ this.watchedDirInodes.delete(dir);
140
+ const stale = this.watchers.get(dir);
141
+ if (stale) {
142
+ stale.close();
143
+ this.watchers.delete(dir);
144
+ }
145
+ const code = (err as NodeJS.ErrnoException).code;
146
+ if (code === 'ENOSPC' || code === 'EMFILE' || code === 'ENFILE') {
147
+ console.warn(
148
+ `[scanner] Watch limit reached (${code}) — subtree unwatched: ${dir}. ` +
149
+ 'Raise fs.inotify.max_user_watches or trim scanner roots.'
150
+ );
151
+ // Surface to the daemon only for the root — losing the root means the
152
+ // whole project is blind; a subtree gap must not tear the project down
153
+ // (the daemon's onError removes the project entirely).
154
+ if (dir === this.rootPath && this.onError) {
155
+ this.onError(err as Error);
156
+ }
157
+ } else {
158
+ // Permission denied or inaccessible directory
159
+ console.warn(`[scanner] Cannot watch ${dir}: ${(err as Error).message}`);
160
+ }
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Attach a watcher to a directory created after start(). Called from the
166
+ * per-directory change callback with every event path; stats the path and
167
+ * only recurses when it is a genuinely new, watchable directory.
168
+ */
169
+ private async watchNewDir(fullPath: string, eventType: string): Promise<void> {
170
+ try {
171
+ // Stat before the dedup check: a delete event arrives with the same
172
+ // path as the original create, and the stale watchedDirs entry must
173
+ // not short-circuit the vanish detection below.
174
+ const s = await stat(fullPath);
175
+ if (!s.isDirectory()) return;
176
+ if (this.watchedDirs.has(fullPath)) {
177
+ // Already watched — but a delete+recreate faster than this stat
178
+ // presents exactly like this, with the live watcher bound to the
179
+ // OLD (dead) inode and inert. A 'rename' event means the entry's
180
+ // identity changed (created / deleted / moved), so re-attach
181
+ // unconditionally — comparing inodes is NOT sufficient there
182
+ // because ext4 routinely hands the freed inode straight back to
183
+ // the recreated directory. 'change' events are attrib noise (a
184
+ // write inside the child bumps its mtime, which fires on the
185
+ // parent), so the cheap inode check keeps those churn-free.
186
+ if (eventType !== 'rename' && this.watchedDirInodes.get(fullPath) === s.ino) return;
187
+ this.unwatchSubtree(fullPath);
188
+ }
189
+ if (await isGitBoundary(fullPath)) return;
190
+ await this.watchDir(fullPath);
107
191
  } catch {
108
- // Permission denied or inaccessible directory
192
+ // Path vanished between event and stat. If it (or anything under it)
193
+ // was a watched directory, drop the bookkeeping and close the dead
194
+ // watchers — otherwise the dedup guards in watchDir/watchNewDir block
195
+ // re-attachment forever when the path is recreated (build-output
196
+ // wipes, codegen, rm -rf && mkdir).
197
+ this.unwatchSubtree(fullPath);
198
+ }
199
+ }
200
+
201
+ /**
202
+ * Forget a deleted directory and everything watched beneath it. fs.watch
203
+ * emits no per-descendant events on a recursive delete, so the whole
204
+ * prefix must be purged here for a recreate to re-watch the full subtree.
205
+ */
206
+ private unwatchSubtree(root: string): void {
207
+ const prefix = root + '/';
208
+ for (const dir of this.watchedDirs) {
209
+ if (dir !== root && !dir.startsWith(prefix)) continue;
210
+ this.watchedDirs.delete(dir);
211
+ this.watchedDirInodes.delete(dir);
212
+ const w = this.watchers.get(dir);
213
+ if (w) {
214
+ w.close();
215
+ this.watchers.delete(dir);
216
+ }
109
217
  }
110
218
  }
111
219
  }