Skip to content

vfs: enforce trailing separators on mounted paths - #67972

Open
dcavalcante wants to merge 1 commit into
nodejs:mainfrom
dcavalcante:vfs-trailing-separator
Open

dcavalcante wants to merge 1 commit 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 92.85714% with 24 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.56%. Comparing base (020cb3c) to head (7846a32).
⚠️ Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/vfs/file_system.js 92.81% 23 Missing ⚠️
lib/internal/vfs/setup.js 92.30% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67972      +/-   ##
==========================================
- Coverage   90.58%   90.56%   -0.03%     
==========================================
  Files         802      802              
  Lines      286735   287123     +388     
  Branches    55396    55490      +94     
==========================================
+ Hits       259741   260034     +293     
- Misses      17091    17176      +85     
- Partials     9903     9913      +10     
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 98.50% <92.81%> (-1.15%) ⬇️

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

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