Skip to content

fix: patch all async fs methods (callback + promises) for VFS interception#10

Open
robertsLando wants to merge 3 commits intoplatformatic:mainfrom
robertsLando:fix/fs-promises-support
Open

fix: patch all async fs methods (callback + promises) for VFS interception#10
robertsLando wants to merge 3 commits intoplatformatic:mainfrom
robertsLando:fix/fs-promises-support

Conversation

@robertsLando
Copy link
Copy Markdown

@robertsLando robertsLando commented Apr 7, 2026

Summary

The VFS module hooks only patched synchronous fs methods (readFileSync, statSync, existsSync, etc.), leaving all async operations unpatched. This caused ENOENT errors when application code used callback-based fs.* or fs.promises.* / require('fs/promises').* to access files inside the VFS.

Fixes #8

Patched methods

Sync (new):

  • fs.accessSync
  • fs.readlinkSync

Callback (new):

  • fs.access
  • fs.stat
  • fs.lstat
  • fs.readFile
  • fs.readdir
  • fs.readlink
  • fs.realpath
  • fs.createReadStream

Promises (new) — patched directly on fs.promises so require('fs/promises') also picks up the changes (same object reference):

  • fs.promises.access
  • fs.promises.readFile
  • fs.promises.stat
  • fs.promises.lstat
  • fs.promises.readdir
  • fs.promises.readlink
  • fs.promises.realpath

Coverage matrix

Method Sync Callback Promises
stat ✅ (existing) ✅ new ✅ new
lstat ✅ (existing) ✅ new ✅ new
readFile ✅ (existing) ✅ new ✅ new
readdir ✅ (existing) ✅ new ✅ new
readlink ✅ new ✅ new ✅ new
realpath ✅ (existing) ✅ new ✅ new
access ✅ new ✅ new ✅ new
existsSync ✅ (existing) N/A (deprecated) N/A
createReadStream N/A ✅ new (stream) N/A

Context

Discovered while testing yao-pkg/pkg SEA mode with a real-world application that uses:

  • fs.stat(path, callback) via send module (used by express.static)
  • await fs.promises.access(path, F_OK) for file existence checks
  • await fs.promises.readFile(path) for loading HTML email templates
  • fs.readdir / fs.realpath callbacks via glob and graceful-fs

All sync equivalents worked fine since they were already patched.

Test plan

  • npm run lint passes
  • npm test — 176/176 tests pass
  • Callback patches deliver errors via process.nextTick (non-throwing)
  • createReadStream returns a proper Readable for VFS files
  • fs.realpath.native preserved as passthrough

🤖 Generated with Claude Code

@robertsLando robertsLando force-pushed the fix/fs-promises-support branch from b70fb8d to 1f78f62 Compare April 7, 2026 15:30
The VFS module hooks only patched synchronous fs methods, leaving async
operations (fs.promises.*, fs.access) unpatched. This caused ENOENT
errors when application code used:
- `await fs.promises.access(path)` (e.g., file existence checks)
- `await fs.promises.readFile(path)` (e.g., loading templates)
- `await fs.promises.stat(path)`, `lstat`, `readdir`, `readlink`, `realpath`
- `fs.accessSync(path)` or `fs.access(path, callback)`

Since `require('fs/promises')` returns the same object as `fs.promises`,
patching directly on `fs.promises` covers both import patterns.

Fixes: platformatic#8

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@robertsLando robertsLando force-pushed the fix/fs-promises-support branch from 1f78f62 to 8eff79e Compare April 7, 2026 15:34
robertsLando added a commit to robertsLando/vfs that referenced this pull request Apr 8, 2026
Temporary fork publish while upstream PRs platformatic#9 and platformatic#10 are pending on platformatic/vfs.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
robertsLando and others added 2 commits April 8, 2026 16:50
…ream patches

The VFS module hooks only patched sync and promise-based fs methods,
leaving callback versions unpatched. This caused ENOENT errors when
libraries like express.static (via send) use fs.stat(path, callback).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Libraries like glob, graceful-fs, and enhanced-resolve use the callback
forms of these methods. Without patches they bypass the VFS entirely.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@robertsLando robertsLando changed the title fix: add fs.promises and fs.access/accessSync patches to module hooks fix: patch all async fs methods (callback + promises) for VFS interception Apr 8, 2026
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.

Support patches core fs/promises functions

1 participant