Skip to content

fix(cli): pass ignore option to chokidar watcher to prevent watching ignored files#17878

Open
sudip-kumar-prasad wants to merge 11 commits intobabel:mainfrom
sudip-kumar-prasad:fix-cli-watcher-ignore
Open

fix(cli): pass ignore option to chokidar watcher to prevent watching ignored files#17878
sudip-kumar-prasad wants to merge 11 commits intobabel:mainfrom
sudip-kumar-prasad:fix-cli-watcher-ignore

Conversation

@sudip-kumar-prasad
Copy link
Copy Markdown

FFixes #9565 by passing the parsed babel ignore options to chokidar's ignored configuration, preventing system limit errors for wide ignore scopes like node_modules.
Currently, when @babel/cli is run with the --watch flag, chokidar is initialized without knowing which files Babel is configured to ignore. This causes it to unnecessarily track large directories (such as node_modules), potentially leading to ENOSPC file watcher limit errors. This PR ensures that babelOptions.ignore is properly propagated down to chokidar's ignored array.

Fixes babel#9565 by passing the parsed babel ignore options to chokidar's ignored
configuration, preventing system limit errors for wide ignore scopes like node_modules.
Copilot AI review requested due to automatic review settings March 17, 2026 18:31
@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Mar 17, 2026

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61285

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to fix @babel/cli --watch watching files that Babel is configured to ignore (e.g. node_modules), reducing the risk of ENOSPC “too many file watchers” errors by passing ignore patterns down to chokidar.

Changes:

  • Extend watcher.enable to accept an ignore list.
  • Pass babelOptions.ignore into the chokidar watcher options (ignored) for both file and directory watch modes.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
packages/babel-cli/src/babel/watcher.ts Adds ignore parameter to enable and forwards it to chokidar’s ignored option.
packages/babel-cli/src/babel/file.ts Passes babelOptions.ignore into watcher setup for --watch on file inputs.
packages/babel-cli/src/babel/dir.ts Passes babelOptions.ignore into watcher setup for --watch on directory inputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread packages/babel-cli/src/babel/watcher.ts Outdated
Comment thread packages/babel-cli/src/babel/watcher.ts
Comment thread packages/babel-cli/src/babel/file.ts Outdated
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 18, 2026

Open in StackBlitz

commit: 53a1f5d

@sudip-kumar-prasad
Copy link
Copy Markdown
Author

Hi 👋 just following up on this PR.

All checks are passing, and I’ve addressed the review feedback.
Would appreciate a review whenever you have time.

Thanks!

Comment thread packages/babel-cli/src/babel/watcher.ts Outdated
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

Prior to 7.0, Babel-core uses micromatch to handle the ignore option. With #8327, we migrated to a much simpler match patterns.

The chokida 3.4 depended by Babel-cli also implements its own match item: https://github.com/paulmillr/chokidar/blob/c6d772a92f30004fdc437d00ecd54fbed041f30e/src/index.ts#L53.

Could you analyzes any discrepancies between these two and ensure that passing through the ignore option must not lead to uncompiled files? It is acceptable if a file ignored by Babel-core is accepted by chokidar, but it is not acceptable if a compilable file is ignored by chokidar due to different ignore interpretation.

Out of curiosity, did you use LLM to assist opening this PR?

@sudip-kumar-prasad
Copy link
Copy Markdown
Author

Thanks for the detailed feedback!

I’ve updated the implementation to rely on loadPartialConfigSync, so the watcher now uses the same ignore logic as Babel core instead of duplicating it.

This ensures that chokidar only ignores files when Babel itself would ignore them (i.e., when it returns null), so there’s no risk of skipping compilable files. It also correctly respects .babelignore and config-based ignore rules.

I’ve also added a small safety fallback so config errors don’t break the watcher.

Regarding your question — yes, I did use AI to assist while working on this, but I verified the changes manually.

Let me know if you’d like me to adjust anything further

@sudip-kumar-prasad
Copy link
Copy Markdown
Author

Hi, just a quick follow-up on this.
I’ve addressed the previous feedback and ensured the watcher now uses Babel’s core config via loadPartialConfigSync.

Please let me know if anything else needs adjustment. Thanks!

Comment thread packages/babel-cli/src/babel/watcher.ts
@sudip-kumar-prasad
Copy link
Copy Markdown
Author

sudip-kumar-prasad commented Mar 28, 2026

I've optimized the watcher by implementing a result cache for the ignored callback. This ensuring loadPartialConfigSync is called only once per unique path during the initial scan/watch session, significantly reducing the overhead on large projects while maintaining full parity with Babel's ignore logic.

@sudip-kumar-prasad
Copy link
Copy Markdown
Author

Thanks again for the detailed review!

I took a deeper look into the potential differences between Babel’s ignore handling and chokidar’s pattern matching.

To avoid any discrepancies, the watcher now relies on loadPartialConfigSync to determine whether a file should be ignored. This ensures we only ignore files when Babel itself would skip them (i.e., when it returns null), rather than depending on chokidar’s pattern matching.

Additionally:

  • Implemented a cache for the ignored callback to avoid repeated config resolution for the same paths
  • Ensured that even if chokidar’s pattern behavior differs, it won’t exclude files that Babel would compile

This way, we maintain correctness while still reducing unnecessary file watching.

Let me know if you’d like me to add tests specifically covering edge cases around ignore behavior.

Comment thread packages/babel-cli/src/babel/watcher.ts Outdated
Bot added 2 commits April 7, 2026 01:35
…-cli-watcher-ignore

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
Bot added 2 commits April 7, 2026 15:40
Rename .babelrc to babel-config.json and move it into in-files/ since
the test uses --config-file (project-wide config), not file-relative
.babelrc configuration. This aligns the test name and config file with
what is actually being tested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants