Skip to content

Commit 673bed4

Browse files
committed
vfs: fix package lookup on Windows
1 parent 46afd4e commit 673bed4

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

lib/internal/modules/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
ReflectApply,
1313
SafeMap,
1414
SafeSet,
15+
Symbol,
1516
StringPrototypeCharCodeAt,
1617
StringPrototypeIncludes,
1718
StringPrototypeSlice,
@@ -93,6 +94,10 @@ const nativeLoaderMethods = {
9394
getResolutionRoot: () => undefined,
9495
};
9596
const loaderMethodKeys = ObjectKeys(nativeLoaderMethods);
97+
// Overrides use this sentinel when they handled a request but their method's
98+
// meaningful result is `undefined`. A plain `undefined` means fall through to
99+
// the native implementation.
100+
const kLoaderOverrideNoResult = Symbol('kLoaderOverrideNoResult');
96101
const loaderOverrides = new SafeMap();
97102
let hasLoaderOverrides = false;
98103

@@ -109,6 +114,7 @@ function wrapLoaderMethod(key, originalFn) {
109114
const override = MapPrototypeGet(loaderOverrides, key);
110115
if (override !== undefined) {
111116
const result = ReflectApply(override, undefined, args);
117+
if (result === kLoaderOverrideNoResult) return undefined;
112118
if (result !== undefined) { return result; }
113119
}
114120
}
@@ -688,6 +694,7 @@ module.exports = {
688694
initializeCjsConditions,
689695
legacyMainResolveExtensions,
690696
legacyMainResolveExtensionsIndexes,
697+
kLoaderOverrideNoResult,
691698
loaderMethods,
692699
loadBuiltinModuleForEmbedder,
693700
loadBuiltinModule,

lib/internal/vfs/setup.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ function installModuleLoaderOverrides() {
784784
const {
785785
legacyMainResolveExtensions,
786786
legacyMainResolveExtensionsIndexes,
787+
kLoaderOverrideNoResult,
787788
setLoaderOverrides,
788789
} = require('internal/modules/helpers');
789790
const { kResolvedByMainIndexNode } = legacyMainResolveExtensionsIndexes;
@@ -854,12 +855,12 @@ function installModuleLoaderOverrides() {
854855
const r = findVFS(jsonPath);
855856
if (r === null) return undefined;
856857
const { vfs } = r;
857-
if (vfsStat(vfs, jsonPath) !== 0) return undefined;
858+
if (vfsStat(vfs, jsonPath) !== 0) return kLoaderOverrideNoResult;
858859
let content;
859860
try {
860861
content = vfs.readFileSync(jsonPath);
861862
} catch {
862-
return undefined;
863+
return kLoaderOverrideNoResult;
863864
}
864865
// Both CJS and ESM raise ERR_INVALID_PACKAGE_CONFIG since
865866
// nodejs/node#48606.
@@ -870,7 +871,7 @@ function installModuleLoaderOverrides() {
870871
const r = findVFS(checkPath);
871872
if (r === null) return undefined;
872873
const found = findVFSPackageJSON(r.vfs, checkPath, r.normalized);
873-
return found.tuple;
874+
return found.tuple ?? kLoaderOverrideNoResult;
874875
},
875876
getPackageScopeConfig(resolved) {
876877
let filePath;
@@ -908,7 +909,7 @@ function installModuleLoaderOverrides() {
908909
const type = found.tuple[2];
909910
if (type === 'module' || type === 'commonjs') return type;
910911
}
911-
return undefined;
912+
return kLoaderOverrideNoResult;
912913
},
913914
});
914915
}

test/parallel/test-vfs-invalid-package-json.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ const vfs = require('node:vfs');
6565
myVfs.mkdirSync('/outer2/inner', { recursive: true });
6666
myVfs.writeFileSync('/outer2/package.json', '{ bad }');
6767
myVfs.writeFileSync('/outer2/inner/mod.js', 'export const x = 1;');
68-
const mountPoint = myVfs.mount();
68+
myVfs.mount();
6969

70-
await assert.rejects(() => import(`${mountPoint}/outer2/inner/mod.js`),
71-
{ code: 'ERR_INVALID_PACKAGE_CONFIG' });
70+
await assert.rejects(
71+
() => import(`${myVfs.mountPointURL}/outer2/inner/mod.js`),
72+
{ code: 'ERR_INVALID_PACKAGE_CONFIG' },
73+
);
7274

7375
myVfs.unmount();
7476
})().then(common.mustCall());

0 commit comments

Comments
 (0)