Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loader: require.resolve should throw for unknown builtin modules #43336

Merged
merged 3 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -780,19 +780,19 @@ Module._load = function(request, parent, isMain) {
}
}

const filename = Module._resolveFilename(request, parent, isMain);
if (StringPrototypeStartsWith(filename, 'node:')) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loading a builtin module prefixed with "node:" expects "ERR_UNKNOWN_BUILTIN_MODULE" to be thrown.
If the "resolve" function is called here, "MODULE_NOT_FOUND" will be thrown.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERR_UNKNOWN_BUILTIN_MODULE certainly makes more sense here than MODULE_NOT_FOUND, I agree (although I think both are fine).

if (StringPrototypeStartsWith(request, 'node:')) {
// Slice 'node:' prefix
const id = StringPrototypeSlice(filename, 5);
const id = StringPrototypeSlice(request, 5);

const module = loadNativeModule(id, request);
if (!module?.canBeRequiredByUsers) {
throw new ERR_UNKNOWN_BUILTIN_MODULE(filename);
throw new ERR_UNKNOWN_BUILTIN_MODULE(request);
}

return module.exports;
}

const filename = Module._resolveFilename(request, parent, isMain);
const cachedModule = Module._cache[filename];
if (cachedModule !== undefined) {
updateChildren(parent, cachedModule, true);
Expand Down Expand Up @@ -854,9 +854,15 @@ Module._load = function(request, parent, isMain) {
};

Module._resolveFilename = function(request, parent, isMain, options) {
if (StringPrototypeStartsWith(request, 'node:') ||
(NativeModule.canBeRequiredByUsers(request) &&
NativeModule.canBeRequiredWithoutScheme(request))) {
if (
(
StringPrototypeStartsWith(request, 'node:') &&
NativeModule.canBeRequiredByUsers(StringPrototypeSlice(request, 5))
) || (
NativeModule.canBeRequiredByUsers(request) &&
NativeModule.canBeRequiredWithoutScheme(request)
)
) {
return request;
}

Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-require-resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ require(fixtures.path('resolve-paths', 'default', 'verify-paths.js'));
assert.strictEqual(resolvedPaths.includes('/node_modules'), false);
});
}

{
assert.strictEqual(require.resolve('node:test'), 'node:test');
assert.strictEqual(require.resolve('node:fs'), 'node:fs');

assert.throws(
() => require.resolve('node:unknown'),
{ code: 'MODULE_NOT_FOUND' },
);
}