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

module: fix null restrictions in imports and exports patterns #44328

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 20 additions & 11 deletions lib/internal/modules/esm/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,27 +573,31 @@ function packageExportsResolve(
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const patternIndex = StringPrototypeIndexOf(key, '*');
const normalizedSubpath = patternIndex !== -1 && exports[key] === null &&
StringPrototypeIncludes(packageSubpath, '//') ?
RegExpPrototypeSymbolReplace(/\/+/g, packageSubpath, '/') :
packageSubpath;
if (patternIndex !== -1 &&
StringPrototypeStartsWith(packageSubpath,
StringPrototypeStartsWith(normalizedSubpath,
StringPrototypeSlice(key, 0, patternIndex))) {
// When this reaches EOL, this can throw at the top of the whole function:
//
// if (StringPrototypeEndsWith(packageSubpath, '/'))
// throwInvalidSubpath(packageSubpath)
//
// To match "imports" and the spec.
if (StringPrototypeEndsWith(packageSubpath, '/'))
emitTrailingSlashPatternDeprecation(packageSubpath, packageJSONUrl,
if (StringPrototypeEndsWith(normalizedSubpath, '/'))
emitTrailingSlashPatternDeprecation(normalizedSubpath, packageJSONUrl,
base);
const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
if (packageSubpath.length >= key.length &&
StringPrototypeEndsWith(packageSubpath, patternTrailer) &&
if (normalizedSubpath.length >= key.length &&
StringPrototypeEndsWith(normalizedSubpath, patternTrailer) &&
patternKeyCompare(bestMatch, key) === 1 &&
StringPrototypeLastIndexOf(key, '*') === patternIndex) {
bestMatch = key;
bestMatchSubpath = StringPrototypeSlice(
packageSubpath, patternIndex,
packageSubpath.length - patternTrailer.length);
normalizedSubpath, patternIndex,
normalizedSubpath.length - patternTrailer.length);
}
}
}
Expand Down Expand Up @@ -666,18 +670,22 @@ function packageImportsResolve(name, base, conditions) {
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const patternIndex = StringPrototypeIndexOf(key, '*');
const normalizedSubpath = patternIndex !== -1 && imports[key] === null &&
StringPrototypeIncludes(name, '//') ?
RegExpPrototypeSymbolReplace(/\/+/g, name, '/') :
name;
if (patternIndex !== -1 &&
StringPrototypeStartsWith(name,
StringPrototypeStartsWith(normalizedSubpath,
StringPrototypeSlice(key, 0,
patternIndex))) {
const patternTrailer = StringPrototypeSlice(key, patternIndex + 1);
if (name.length >= key.length &&
StringPrototypeEndsWith(name, patternTrailer) &&
if (normalizedSubpath.length >= key.length &&
StringPrototypeEndsWith(normalizedSubpath, patternTrailer) &&
patternKeyCompare(bestMatch, key) === 1 &&
StringPrototypeLastIndexOf(key, '*') === patternIndex) {
bestMatch = key;
bestMatchSubpath = StringPrototypeSlice(
name, patternIndex, name.length - patternTrailer.length);
normalizedSubpath, patternIndex, normalizedSubpath.length - patternTrailer.length);
}
}
}
Expand Down Expand Up @@ -1124,6 +1132,7 @@ module.exports = {
const {
defaultGetFormatWithoutErrors,
} = require('internal/modules/esm/get_format');
const console = require('console');

if (policy) {
const $defaultResolve = defaultResolve;
Expand Down
7 changes: 7 additions & 0 deletions test/es-module/test-esm-exports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/invalid1', './invalid1'],
['pkgexports/invalid4', './invalid4'],
// Null mapping
['pkgexports/sub/internal/test', './sub/internal/test'],
['pkgexports/sub//internal/test', './sub//internal/test'],
['pkgexports/sub/internal//test', './sub/internal//test'],
['pkgexports/sub//internal//test', './sub//internal//test'],
['pkgexports/sub/////internal/////test', './sub/////internal/////test'],
['pkgexports/null', './null'],
['pkgexports//null', './/null'],
['pkgexports/////null', './////null'],
['pkgexports/null/subpath', './null/subpath'],
// Empty fallback
['pkgexports/nofallback1', './nofallback1'],
Expand Down
22 changes: 18 additions & 4 deletions test/es-module/test-esm-imports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,17 @@ const { requireImport, importImport } = importer;
'#missing',
// Explicit null import
'#null',
'#subpath/null',
// No condition match import
'#nullcondition',
// Null subpath shadowing
'#subpath/nullshadow/x',
// Null pattern
'#subpath/internal/test',
'#subpath//internal/test',
'#subpath/internal//test',
'#subpath//internal//test',
'#subpath/////internal/////test',
]);

for (const specifier of undefinedImports) {
Expand All @@ -94,10 +101,17 @@ const { requireImport, importImport } = importer;
}

// Handle not found for the defined imports target not existing
loadFixture('#notfound').catch(mustCall((err) => {
strictEqual(err.code,
isRequire ? 'MODULE_NOT_FOUND' : 'ERR_MODULE_NOT_FOUND');
}));
const nonDefinedImports = new Set([
'#notfound',
'#subpath//null',
'#subpath/////null',
]);
for (const specifier of nonDefinedImports) {
loadFixture(specifier).catch(mustCall((err) => {
strictEqual(err.code,
isRequire ? 'MODULE_NOT_FOUND' : 'ERR_MODULE_NOT_FOUND');
}));
}
});

// CJS resolver must still support #package packages in node_modules
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/es-modules/pkgimports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"require": "./requirebranch.js"
},
"#subpath/*": "./sub/*",
"#subpath/internal/*": null,
"#subpath/null": null,
"#subpath/*.asdf": "./test.js",
"#external": "pkgexports/valid-cjs",
"#external/subpath/*": "pkgexports/sub/*",
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/node_modules/pkgexports/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.