Skip to content

vfs: enforce trailing separators on mounted paths - #67972

Open
dcavalcante wants to merge 2 commits into
nodejs:mainfrom
dcavalcante:vfs-trailing-separator
Open

dcavalcante wants to merge 2 commits into
nodejs:mainfrom
dcavalcante:vfs-trailing-separator

Conversation

@dcavalcante

Copy link
Copy Markdown
Contributor

A path with a trailing separator must name a directory. The VFS dropped the separator while resolving mounted paths, so operations acted on the file instead of failing as they do on a real file system:

fs.readFileSync(`${mnt}/file/`);       // real fs: ENOTDIR  vfs: returned the contents
fs.writeFileSync(`${mnt}/file/`, 'x'); // real fs: EISDIR   vfs: overwrote the file
fs.unlinkSync(`${mnt}/file/`);         // real fs: ENOTDIR  vfs: deleted the file
fs.lstatSync(`${mnt}/link-to-dir/`);   // real fs: stats the directory  vfs: stats the link

The separator is now enforced in one place, VirtualFileSystem#toProviderPath, as each operation enforces it on POSIX:

  • lookups follow links and require a directory;
  • lstat(), readlink() and the other l*() functions act on the directory a final link names;
  • unlink(), rmdir() and the source of rename() require the entry itself to be a directory;
  • creating a file or a link at such a path fails.

On Linux, I compared the VFS with real fs for 27 operations, each on a file, a directory, a missing path, and links to a file, a directory and nothing, using both the sync and promise APIs. Before this change, 71 results differed with a trailing separator; now none do. Paths without a trailing separator behave exactly as before.

Notes:

  • rm('link-to-dir/', { recursive: true }) throws ENOTDIR and removes nothing. Real fs currently empties the link's target directory first (fs: rmSync() deletes a symlinked directory's contents then throws ENOTDIR when the path has a trailing slash #67186), and the VFS deliberately doesn't follow that.
  • realpath() still ignores the separator. The JS and native implementations differ there, and they share one VFS hook.
  • As for a real path, fs.statSync() with throwIfNoEntry: false now returns undefined on ENOTDIR as well as ENOENT.
  • Some differences that don't involve the separator are left alone, for example syscall names in some errors and the provider path in some error messages.

Refs: #67186

Assisted-by: Claude Opus 5.5

cc @mcollina

A path with a trailing separator must name a directory, but resolving a
mounted path drops the separator, so VFS operations acted on the file:
fs.readFileSync('<mount>/file/') returned its contents,
fs.writeFileSync('<mount>/file/') overwrote it, and fs.unlinkSync() and
fs.rmSync() deleted it, where a real file system fails with ENOTDIR or
EISDIR.

Enforce the separator where paths are resolved, the way each operation
enforces it on a POSIX file system: lookups follow links and require a
directory; lstat(), readlink() and the l*() functions act on the
directory a final link names; unlink(), rmdir() and the source of
rename() require the entry itself to be a directory; creating a file or
a link there fails. rm() refuses a link to a directory rather than
following it into its target.

Paths given to an unmounted file system now lose a trailing separator
as mounted ones already did, and the callback functions of a
VirtualFileSystem report errors from resolving the path through the
callback instead of throwing. As for a real path, fs.statSync() with
throwIfNoEntry: false returns undefined on ENOTDIR as well as ENOENT.

Signed-off-by: Daniel Cavalcante <dcavalcante@gmail.com>
Assisted-by: Claude Opus 5.5
@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. vfs Issues and PRs related to the virtual filesystem subsystem. labels Dec 2, 2026
@codecov

codecov Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.80952% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.57%. Comparing base (020cb3c) to head (9c78362).
⚠️ Report is 22 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/vfs/file_system.js 99.06% 3 Missing ⚠️
lib/internal/vfs/setup.js 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67972      +/-   ##
==========================================
- Coverage   90.58%   90.57%   -0.02%     
==========================================
  Files         802      802              
  Lines      286735   287201     +466     
  Branches    55396    55504     +108     
==========================================
+ Hits       259741   260129     +388     
- Misses      17091    17161      +70     
- Partials     9903     9911       +8     
Files with missing lines Coverage Δ
lib/fs.js 97.11% <100.00%> (+<0.01%) ⬆️
lib/internal/vfs/setup.js 88.32% <92.30%> (+0.11%) ⬆️
lib/internal/vfs/file_system.js 99.70% <99.06%> (+0.05%) ⬆️

... and 43 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: Daniel Cavalcante <dcavalcante@gmail.com>
Assisted-by: Claude Opus 5.5
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 file-system APIs and the fs module. needs-ci PRs that need a full CI run. vfs Issues and PRs related to the virtual filesystem subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants