Skip to content

fix(@angular/build): watch only explicitly added paths in chokidar watcher - #34361

Merged
dgp1130 merged 1 commit into
angular:mainfrom
clydin:fix/chokidar-selective-file-watching
Oct 16, 2026
Merged

dgp1130 merged 1 commit into
angular:mainfrom
clydin:fix/chokidar-selective-file-watching

Conversation

@clydin

@clydin clydin commented Oct 15, 2026

Copy link
Copy Markdown
Member

When preserveSymlinks or polling is enabled, the build watcher falls back to Chokidar. Previously, createChokidarWatcher initialized Chokidar on the entire workspace root directory and expanded any watched file inside node_modules to its full package root directory via ChokidarNodeModulesManager. Because Chokidar v5 recursively walks watched directories and attaches individual fs.watch handles to every subdirectory and file, projects with many workspace subdirectories or large imported packages could exceed the macOS per-process directory watch limit and crash with an unhandled EMFILE error.

This change initializes Chokidar with an empty watch set and registers only the specific paths passed to add() and remove(), eliminating recursive directory scans across the workspace root and node_modules packages. When a directory is explicitly watched (such as when NG_BUILD_WATCH_ROOT is enabled), node_modules subdirectories are pruned unless directly added to the watch set. An error listener is also attached to prevent unhandled watcher errors from terminating the process, and removed files that remain in the watch set are re-registered so subsequent file recreation is detected across multiple watched directories.

Closes #34312

@clydin clydin added the target: patch This PR is targeted for the next patch release label Oct 15, 2026
@clydin
clydin force-pushed the fix/chokidar-selective-file-watching branch from d170c55 to 6aff4ed Compare October 15, 2026 17:46
@clydin
clydin marked this pull request as ready for review October 15, 2026 18:04

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the Chokidar file watcher implementation in Angular's build utility, replacing the complex ChokidarNodeModulesManager with a simpler ignore-pattern approach and improving the handling of file deletions and rapid recreations. The reviewer noted that when a directly watched file is unlinked, calling watcher.add directly bypasses the asynchronous re-statting safety net implemented in the custom add method, which could lead to missed recreation events. They recommended extracting this safety net into a helper function to ensure robust detection across both paths.

Comment thread packages/angular/build/src/utils/watcher.ts
…tcher

When preserveSymlinks or polling is enabled, the build watcher falls back to Chokidar. Previously,
createChokidarWatcher initialized Chokidar on the entire workspace root directory and expanded any
watched file inside node_modules to its full package root directory via ChokidarNodeModulesManager.
Because Chokidar v5 recursively walks watched directories and attaches individual fs.watch handles to
every subdirectory and file, projects with many workspace subdirectories or large imported packages
could exceed the macOS per-process directory watch limit and crash with an unhandled EMFILE error.

This change initializes Chokidar with an empty watch set and registers only the specific paths passed
to add() and remove(), eliminating recursive directory scans across the workspace root and
node_modules packages. When a directory is explicitly watched (such as when NG_BUILD_WATCH_ROOT is
enabled), node_modules subdirectories are pruned unless directly added to the watch set. An error
listener is also attached to prevent unhandled watcher errors from terminating the process, and
removed files that remain in the watch set are re-registered so subsequent file recreation is
detected across multiple watched directories.

Closes angular#34312
@clydin
clydin force-pushed the fix/chokidar-selective-file-watching branch from 6aff4ed to 60b40f3 Compare October 15, 2026 18:26
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label Oct 15, 2026
@alan-agius4 alan-agius4 added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 16, 2026
@dgp1130
dgp1130 merged commit 972302a into angular:main Oct 16, 2026
66 of 69 checks passed
@dgp1130

dgp1130 commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

This PR was merged into the repository. The changes were merged into the following branches:

@clydin
clydin deleted the fix/chokidar-selective-file-watching branch October 16, 2026 16:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ng serve crashes with EMFILE: too many open files, watch on macOS when preserveSymlinks is enabled (22.2 regression)

3 participants