Skip to content

fix(@angular/build): prevent chokidar from scanning unimported node_modules in watch mode - #34168

Merged
clydin merged 1 commit into
angular:mainfrom
clydin:fix-chokidar-node-modules-watch
Sep 25, 2026
Merged

clydin merged 1 commit into
angular:mainfrom
clydin:fix-chokidar-node-modules-watch

Conversation

@clydin

@clydin clydin commented Sep 24, 2026

Copy link
Copy Markdown
Member

When using Chokidar (such as when preserveSymlinks: true is configured or polling is active), Chokidar's initial filesystem scan previously walked the entire workspaceRoot, including all of node_modules. Because await once(watcher, 'ready') blocks until the initial scan completes, this caused watch mode in commands like ng test, ng serve, and ng build --watch to hang for minutes before emitting the first build result.

Fixes #34162

@clydin clydin added the target: patch This PR is targeted for the next patch release label Sep 24, 2026
@clydin
clydin marked this pull request as ready for review September 24, 2026 22:31

@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 enhances the file watcher to support detecting changes in linked packages inside node_modules (such as those created via npm link) when preserveSymlinks is enabled, while continuing to ignore unimported packages. The feedback focuses on addressing a potential watcher leak when packages are removed by implementing reference counting, and optimizing performance by avoiding expensive path normalization in isNodeModulesIgnored through a quick initial string check.

Comment thread packages/angular/build/src/utils/watcher.ts Outdated
Comment thread packages/angular/build/src/utils/watcher.ts Outdated
Comment thread packages/angular/build/src/utils/watcher.ts Outdated
@clydin
clydin force-pushed the fix-chokidar-node-modules-watch branch 2 times, most recently from a797fb2 to df9dfab Compare September 24, 2026 23:13
@clydin clydin added the action: review The PR is still awaiting reviews from at least one requested reviewer label Sep 24, 2026
@clydin
clydin requested a review from alan-agius4 September 24, 2026 23:15
Comment thread packages/angular/build/src/utils/watcher_spec.ts Outdated
@clydin
clydin force-pushed the fix-chokidar-node-modules-watch branch from df9dfab to 3db30e3 Compare September 25, 2026 14:18
@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 Sep 25, 2026
…odules in watch mode

When using Chokidar (such as when `preserveSymlinks: true` is configured or polling is active), Chokidar's initial filesystem scan previously walked the entire `workspaceRoot`, including all of `node_modules`. Because `await once(watcher, 'ready')` blocks until the initial scan completes, this caused watch mode in commands like `ng test`, `ng serve`, and `ng build --watch` to hang for minutes before emitting the first build result.

This commit introduces selective `node_modules` filtering in Chokidar:
- `node_modules` packages corresponding to imported build files (e.g. `npm link` libraries, both unscoped and scoped) are allowed and their directories watched, retaining native symlink traversal.
- All other unimported subdirectories in `node_modules` are pruned and ignored during the initial scan and subsequent directory visits.
- Packages added dynamically via `watcher.add()` are registered and watched on the fly.

Fixes angular#34162
@clydin
clydin force-pushed the fix-chokidar-node-modules-watch branch from 3db30e3 to dcd5c67 Compare September 25, 2026 14:21
@clydin
clydin merged commit 91b0947 into angular:main Sep 25, 2026
39 checks passed
@clydin

clydin commented Sep 25, 2026

Copy link
Copy Markdown
Member Author

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

@clydin
clydin deleted the fix-chokidar-node-modules-watch branch September 25, 2026 15:06
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

2 participants