AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Turbopack Dev On MacOS Hits Watchpack Fs.Watch EMFILE On Ancestor Directories And App Subtrees

Turbopack's file watcher over-watches ancestor directories (e.g. /Users, /private) and many app subdirectories, exhausting file descriptors and causing EMFILE errors and false next.config.js restart detection.

highConfidence 82%Next.jsAffected V16.2.3

Origin Analysis

The watcher setup for Turbopack dev does not filter watch paths to the project root; it includes parent directories (likely due to symlink resolution or naive ancestor traversal) and does not ignore common noisy directories. This leads to fs.watch being called on /Users, /private, /tmp, and many app/ subdirectories, eventually hitting the OS file descriptor limit (EMFILE). The subsequent Watchpack errors are misinterpreted as config file changes, triggering unnecessary server restarts.
1. Clone repro repo https://github.com/wzrdgang/next-turbopack-emfile-macos-repro 2. npm install 3. npm run dev:debug 4. Observe repeated Watchpack EMFILE errors and debug logger showing fs.watch attempts on ancestor dirs and many app subdirs 5. Run npm run build and note it succeeds

Fixing Code Block

Edge Case Audit

This hotfix filters ancestors and noisy dirs, but if the project has an extremely large number of route directories, the number of watches may still exceed file descriptor limits. In that case, fallback to polling (set poll: true) or raise ulimit. The followSymlinks: false may affect symlinked files in development; test before deploying. This change must be applied to the Next.js source, not the user project, and will be overridden on package upgrade; use patch-package or wait for official fix.

Ecosystem Topology