Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup! module: disable package self resolution when name contains a c…
…olon
  • Loading branch information
aduh95 committed Feb 9, 2021
commit 42cddea0e3ee92978993a79d62fa16497f4a9ba2
5 changes: 4 additions & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ function trySelfParentPath(parent) {

function trySelf(parentPath, request) {
if (!parentPath) return false;
if (StringPrototypeIncludes(request, ':')) return false;

const { data: pkg, path: pkgPath } = readPackageScope(parentPath) || {};
if (!pkg || pkg.exports === undefined) return false;
Expand Down Expand Up @@ -901,6 +900,10 @@ Module._resolveFilename = function(request, parent, isMain, options) {
const parentPath = trySelfParentPath(parent);
const selfResolved = trySelf(parentPath, request);
if (selfResolved) {
if (StringPrototypeIncludes(request, ':')) {
throw new ERR_INVALID_MODULE_SPECIFIER(
request, 'is not a valid package name', parentPath);
}
const cacheKey = request + '\x00' +
(paths.length === 1 ? paths[0] : ArrayPrototypeJoin(paths, '\x00'));
Module._pathCache[cacheKey] = selfResolved;
Expand Down
19 changes: 7 additions & 12 deletions test/message/test-self-referential-package.out
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
Error: Cannot find module 'file:package'
Require stack:
- */self_ref_module_with_colon/bin.js
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file:package" is not a valid package name imported from *
at new NodeError (node:internal/errors:*)
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:*)
at Function.Module._load (node:internal/modules/cjs/loader:*)
at Module.require (node:internal/modules/cjs/loader:*)
at require (node:internal/modules/cjs/helpers:*)
at Object.<anonymous> (*/self_ref_module_with_colon/bin.js:*)
at Object.<anonymous> (*)
at Module._compile (node:internal/modules/cjs/loader:*)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:*)
at Module.load (node:internal/modules/cjs/loader:*)
at Function.Module._load (node:internal/modules/cjs/loader:*)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:*) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'*/self_ref_module_with_colon/bin.js'
]
at Function.Module._load (node:internal/modules/cjs/loader:*) {
code: 'ERR_INVALID_MODULE_SPECIFIER'
}
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/package' imported from */self_ref_module_with_colon/bin.js
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/package' imported from *
at new NodeError (node:internal/errors:*)
at finalizeResolution (node:internal/modules/esm/resolve:*)
at moduleResolve (node:internal/modules/esm/resolve:*)
Expand All @@ -29,7 +24,7 @@ Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/package' imported from */self
at importModuleDynamically (node:vm:*) {
code: 'ERR_MODULE_NOT_FOUND'
}
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file%3Apackage" is not a valid package name imported from */self_ref_module_with_colon/bin.js
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file%%3Apackage" is not a valid package name imported from *
at new NodeError (node:internal/errors:*)
at parsePackageName (node:internal/modules/esm/resolve:*)
at packageResolve (node:internal/modules/esm/resolve:*)
Expand Down