MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / watchDirectory

Method watchDirectory

src/sync/watcher.ts:555–601  ·  view source on GitHub ↗
(dir: string, real: string, recursive: boolean)

Source from the content-addressed store, hash-verified

553 }
554
555 private watchDirectory(dir: string, real: string, recursive: boolean): void {
556 if (this.stopped || (this.degradedReason && !this.recoveringFromLock) || this.inotifyLimitWarned) return;
557 if (this.dirWatchers.has(dir)) return;
558 // Recursive streams cost more than inotify entries. Keep a separate hard
559 // ceiling while honouring a lower user-configured directory watch budget.
560 const cap = recursive ? Math.min(256, maxDirWatches()) : maxDirWatches();
561 if (this.dirWatchers.size >= cap) {
562 if (!this.dirCapWarned) {
563 this.dirCapWarned = true;
564 logWarn('File watcher hit directory-watch cap; remaining subtrees rely on manual/periodic sync', { cap });
565 }
566 return;
567 }
568
569 let w: fs.FSWatcher;
570 try {
571 w = watchImpl(dir, { persistent: true, recursive }, (_event, filename) =>
572 this.handleDirEvent(dir, filename)
573 );
574 } catch (err) {
575 // EMFILE/ENFILE means the PROCESS is out of descriptors — every further
576 // directory would fail too, so degrade the whole watcher rather than
577 // limping along with a partial watch set.
578 if (isWatchResourceExhaustion(err)) {
579 this.degrade(EXHAUSTION_REASON, { error: String(err), dir });
580 } else if (isInotifyWatchExhaustion(err)) {
581 // ENOSPC = inotify watch budget exhausted. NON-fatal: keep the watches
582 // we have and tell the user the knob to raise (warn once).
583 this.warnInotifyLimit({ error: String(err), dir });
584 }
585 // ENOENT / EACCES on a single directory stays non-fatal: skip it quietly.
586 return;
587 }
588 w.on('error', (err: unknown) => {
589 if (isWatchResourceExhaustion(err)) {
590 this.degrade(EXHAUSTION_REASON, { error: String(err), dir });
591 return;
592 }
593 if (isInotifyWatchExhaustion(err)) {
594 this.warnInotifyLimit({ error: String(err), dir });
595 }
596 this.unwatchDir(dir);
597 });
598 this.dirWatchers.set(dir, w);
599
600 this.watchedRealDirs.set(dir, real);
601 }
602
603 /** Events are relative to a watch's logical path, even for an external target. */
604 private handleDirEvent(dir: string, filename: string | Buffer | null): void {

Callers 1

refreshWatchTreeMethod · 0.95

Calls 11

handleDirEventMethod · 0.95
degradeMethod · 0.95
warnInotifyLimitMethod · 0.95
unwatchDirMethod · 0.95
logWarnFunction · 0.90
maxDirWatchesFunction · 0.85
isInotifyWatchExhaustionFunction · 0.85
hasMethod · 0.80
onMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected