Skip to content

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

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

fs: prevent spurious recursive watch events on prefix siblings#63095
marcopiraccini wants to merge 2 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

Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
@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 89.66%. Comparing base (b2f6aa3) to head (7b92f84).
⚠️ Report is 143 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #63095      +/-   ##
==========================================
+ Coverage   89.65%   89.66%   +0.01%     
==========================================
  Files         712      712              
  Lines      220512   220515       +3     
  Branches    42289    42282       -7     
==========================================
+ Hits       197690   197718      +28     
+ Misses      14663    14652      -11     
+ Partials     8159     8145      -14     
Files with missing lines Coverage Δ
lib/internal/fs/recursive_watch.js 87.11% <100.00%> (+1.84%) ⬆️

... and 22 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.

Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
@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

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.

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