Repository navigation
fix(ext/node): implement Module._stat - #34157
Merged
Merged
Conversation
Adds the internal-but-public `Module._stat(path)` accessor on the `node:module` Module constructor. Per Node's `lib/internal/modules/cjs/loader.js`: - Returns 0 if path is a file - Returns 1 if path is a directory - Returns a negative value if the path does not exist or stat fails The default implementation is backed by op_require_stat. User code can overwrite the property; the setter emits an `ExperimentalWarning` and swaps in the new function so the loader's internal stat() picks it up (used by patch-package, bundlers, and other tooling). Fixes `parallel/test-module-stat.js` node compat test. Closes denoland/orchid#114 Co-Authored-By: Divy Srivastava <me@littledivy.com>
lunadogbot
previously requested changes
May 17, 2026
lunadogbot
left a comment
Contributor
There was a problem hiding this comment.
const result = internalModuleStat(filename);is the load-bearing behavior change, but the enabledparallel/test-module-stat.jsonly calls the defaultModule._statgetter. It does not prove that assigningModule._stat = fnaffects Deno's internal CJS resolution path, or that the setter warning path works. Please add a focused test that patchesModule._statand observesrequire()resolution changing, for example by returning< 0for an existing temp module and assertingrequire(file)fails withMODULE_NOT_FOUND.
CI is not green yet, but this test gap is independent of the current CI state.
Contributor
|
Build infra flake ( |
Adds a regression test in tests/unit_node/module_test.ts that: 1. Asserts the default Module._stat returns 0 for files, 1 for directories, and a negative value for missing paths. 2. Patches Module._stat to return -ENOENT for a real on-disk file and asserts require() fails with MODULE_NOT_FOUND, then restores the original stat and asserts the same require() succeeds. This proves the setter actually swaps the function used by Deno's internal CJS resolution path, not just the public accessor. Co-Authored-By: Divy Srivastava <me@littledivy.com>
littledivy
approved these changes
May 17, 2026
littledivy
added a commit
to crowlKats/deno
that referenced
this pull request
Jun 10, 2026
## Summary
Implements the internal-but-public `Module._stat(path)` accessor on the
`node:module` `Module` constructor, matching Node's
`lib/internal/modules/cjs/loader.js`:
- Returns `0` if `path` is a file
- Returns `1` if `path` is a directory
- Returns a negative value if the path does not exist or stat fails
- Synchronous
The default implementation is backed by the existing `op_require_stat`
op
(which Deno's internal `stat()` helper already used). Assigning
`Module._stat = fn` emits an `ExperimentalWarning` (matching Node) and
swaps the underlying function so the module loader's internal stat
lookups
also pick it up, which is what `patch-package`, certain bundlers, and
the
Node test suite rely on.
## Test plan
- [x] `cargo test --test node_compat -- test-module-stat` passes
- [ ] Manual: `Module._stat(<file>) === 0`, `Module._stat(<dir>) === 1`,
`Module._stat(<nonexistent>) < 0`
- [ ] CI
`parallel/test-vfs.js` still fails after this change but at a later
point:
it expects the CJS loader to use the (user-patched) `fs.realpathSync`
and
`fs.readFileSync`. Deno's loader currently calls `op_require_real_path`
/
`op_require_read_file` directly, which bypass user monkey-patching.
Making
the loader patchable is a larger architectural change worth handling in
a
follow-up.
Closes denoland/orchid#114
---------
Co-authored-by: divybot <divybot@users.noreply.github.com>
Co-authored-by: Divy Srivastava <me@littledivy.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the internal-but-public
Module._stat(path)accessor on thenode:moduleModuleconstructor, matching Node'slib/internal/modules/cjs/loader.js:0ifpathis a file1ifpathis a directoryThe default implementation is backed by the existing
op_require_statop(which Deno's internal
stat()helper already used). AssigningModule._stat = fnemits anExperimentalWarning(matching Node) andswaps the underlying function so the module loader's internal stat lookups
also pick it up, which is what
patch-package, certain bundlers, and theNode test suite rely on.
Test plan
cargo test --test node_compat -- test-module-statpassesModule._stat(<file>) === 0,Module._stat(<dir>) === 1,Module._stat(<nonexistent>) < 0parallel/test-vfs.jsstill fails after this change but at a later point:it expects the CJS loader to use the (user-patched)
fs.realpathSyncandfs.readFileSync. Deno's loader currently callsop_require_real_path/op_require_read_filedirectly, which bypass user monkey-patching. Makingthe loader patchable is a larger architectural change worth handling in a
follow-up.
Closes denoland/orchid#114