fs: restore fs patchability in ESM loader · nodejs/node@f4ea495 · GitHub
Skip to content

Commit f4ea495

Browse files
joyeecheungaduh95
authored andcommitted
fs: restore fs patchability in ESM loader
Temporarily restore fs patchability in ESM loader as a workaround for helping downstream projects that depend on this undocumented hidden contract transition into using hook proper APIs. This patch intentionally avoids adding a test and instead adds warning comments to hopefully steer new code away from depending on it. PR-URL: #62835 Refs: #62012 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 01a9552 commit f4ea495

3 files changed

Lines changed: 18 additions & 6 deletions

File tree

lib/internal/modules/esm/load.js

Lines changed: 6 additions & 2 deletions

lib/internal/modules/esm/resolve.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const {
2525
const assert = require('internal/assert');
2626
const internalFS = require('internal/fs/utils');
2727
const { BuiltinModule } = require('internal/bootstrap/realm');
28-
const { realpathSync } = require('fs');
28+
const fs = require('fs');
2929
const { getOptionValue } = require('internal/options');
3030
// Do not eagerly grab .manifest, it may be in TDZ
3131
const { sep, posix: { relative: relativePosixPath }, resolve } = require('path');
@@ -273,7 +273,11 @@ function finalizeResolution(resolved, base, preserveSymlinks) {
273273
}
274274

275275
if (!preserveSymlinks) {
276-
const real = realpathSync(path, {
276+
// If you are reading this code to figure out how to patch Node.js module loading
277+
// behavior - DO NOT depend on the patchability in new code: Node.js
278+
// internals may stop going through the JavaScript fs module entirely.
279+
// Prefer module.registerHooks() or other more formal fs hooks released in the future.
280+
const real = fs.realpathSync(path, {
277281
[internalFS.realpathCacheKey]: realpathCache,
278282
});
279283
const { search, hash } = resolved;

lib/internal/modules/esm/translators.js

Lines changed: 6 additions & 2 deletions

0 commit comments

Comments
 (0)