Skip to content

fs: prevent spurious recursive watch events on prefix siblings#63095

Open
marcopiraccini wants to merge 3 commits into
nodejs:mainfrom
marcopiraccini:rename-watch-fix
Open

fs: prevent spurious recursive watch events on prefix siblings#63095
marcopiraccini wants to merge 3 commits into
nodejs:mainfrom
marcopiraccini:rename-watch-fix

Conversation

@marcopiraccini
Copy link
Copy Markdown
Contributor

@marcopiraccini marcopiraccini commented May 3, 2026

Fix a bug in the JS-based recursive fs.watch (used on Linux) where deleting an entry caused spurious rename events to be emitted for unrelated sibling entries whose names share a prefix with the deleted entry.

FSWatcher#unwatchFiles(file) in lib/internal/fs/recursive_watch.js used StringPrototypeStartsWith(filename, file) to find the watched entries to remove. This match is a raw string prefix, so deleting foo also unwatched foo_, foo_bar, foo_bar/file.txt, and so on. The next time #watchFolder walked the parent directory, those still-on-disk siblings were missing from #files and were treated as newly created, firing extra rename events and re-attaching watchers redundantly.

Fix

Match by exact path, or by deletedPath + path.sep prefix, so only the deleted entry and its actual descendants are unwatched.

Fixes: #58868

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels May 3, 2026
@marcopiraccini marcopiraccini marked this pull request as ready for review May 3, 2026 08:25
@codecov
Copy link
Copy Markdown

codecov Bot commented May 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.33%. Comparing base (837910d) to head (430d3a4).
⚠️ Report is 86 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main   #63095    +/-   ##
========================================
  Coverage   90.33%   90.33%            
========================================
  Files         730      730            
  Lines      234205   234365   +160     
  Branches    43927    43927            
========================================
+ Hits       211559   211714   +155     
+ Misses      14370    14368     -2     
- Partials     8276     8283     +7     
Files with missing lines Coverage Δ
lib/internal/fs/recursive_watch.js 87.11% <100.00%> (+1.84%) ⬆️

... and 44 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label May 19, 2026
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label May 19, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

marcopiraccini added a commit to marcopiraccini/node that referenced this pull request May 23, 2026
@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label May 24, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label May 24, 2026
@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina added the request-ci Add this label to start a Jenkins CI on a PR. label May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. request-ci Add this label to start a Jenkins CI on a PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fs.watch recursive on Linux emits spurious rename events for sibling files/directories prefixed by related paths on deletion

4 participants