diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index fc333e937d6b..c994d1a98c74 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3502,6 +3502,9 @@ npx codemod@latest @nodejs/crypto-rsa-pss-update -Type: Runtime +Type: End-of-Life -The remapping of specifiers ending in `"/"` like `import 'pkg/x/'` is deprecated -for package `"exports"` and `"imports"` pattern resolutions. +The remapping of specifiers ending in `"/"` like `import 'pkg/x/'` is no longer +supported for package `"exports"` and `"imports"` pattern resolutions. Such +specifiers throw an [`ERR_INVALID_MODULE_SPECIFIER`][] error. ### DEP0156: `.aborted` property and `'abort'`, `'aborted'` event in `http` @@ -3758,6 +3762,9 @@ that will be removed in a future V8 release. -Type: Runtime +Type: End-of-Life Package imports and exports targets mapping into paths including a double slash -(of _"/"_ or _"\\"_) are deprecated and will fail with a resolution validation -error in a future release. This same deprecation also applies to pattern matches -starting or ending in a slash. +(of _"/"_ or _"\\"_) throw an [`ERR_INVALID_PACKAGE_TARGET`][] error. Pattern +matches starting or ending in a slash, or containing a double slash, throw an +[`ERR_INVALID_MODULE_SPECIFIER`][] error. ### DEP0167: Weak `DiffieHellmanGroup` instances (`modp1`, `modp2`, `modp5`) @@ -4868,6 +4875,8 @@ Type: Documentation-only [`CryptoKey`]: webcrypto.md#class-cryptokey [`Decipheriv`]: crypto.md#class-decipheriv [`Duplex.toWeb()`]: stream.md#streamduplextowebstreamduplex-options +[`ERR_INVALID_MODULE_SPECIFIER`]: errors.md#err_invalid_module_specifier +[`ERR_INVALID_PACKAGE_TARGET`]: errors.md#err_invalid_package_target [`Error.isError`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/isError [`KeyObject.from()`]: crypto.md#static-method-keyobjectfromkey [`REPLServer.clearBufferedCommand()`]: repl.md#replserverclearbufferedcommand diff --git a/lib/internal/modules/esm/resolve.js b/lib/internal/modules/esm/resolve.js index ac057127a717..10c6cf3178de 100644 --- a/lib/internal/modules/esm/resolve.js +++ b/lib/internal/modules/esm/resolve.js @@ -9,7 +9,6 @@ const { ObjectPrototypeHasOwnProperty, RegExpPrototypeExec, RegExpPrototypeSymbolReplace, - SafeSet, String, StringPrototypeEndsWith, StringPrototypeIncludes, @@ -57,61 +56,6 @@ const { const { hasPackageMap, packageMapResolve } = require('internal/modules/package_map'); -const emittedPackageWarnings = new SafeSet(); - -/** - * Emits a deprecation warning for the use of a deprecated trailing slash pattern mapping in the "exports" field - * module resolution of a package. - * @param {string} match - The deprecated trailing slash pattern mapping. - * @param {string} pjsonUrl - The URL of the package.json file. - * @param {string} base - The URL of the module that imported the package. - */ -function emitTrailingSlashPatternDeprecation(match, pjsonUrl, base) { - if (process.noDeprecation) { - return; - } - const pjsonPath = fileURLToPath(pjsonUrl); - if (emittedPackageWarnings.has(pjsonPath + '|' + match)) { return; } - emittedPackageWarnings.add(pjsonPath + '|' + match); - process.emitWarning( - `Use of deprecated trailing slash pattern mapping "${match}" in the ` + - `"exports" field module resolution of the package at ${pjsonPath}${ - base ? ` imported from ${fileURLToPath(base)}` : - ''}. Mapping specifiers ending in "/" is no longer supported.`, - 'DeprecationWarning', - 'DEP0155', - ); -} - -const doubleSlashRegEx = /[/\\][/\\]/; - -/** - * Emits a deprecation warning for invalid segment in module resolution. - * @param {string} target - The target module. - * @param {string} request - The requested module. - * @param {string} match - The matched module. - * @param {string} pjsonUrl - The package.json URL. - * @param {boolean} internal - Whether the module is in the "imports" or "exports" field. - * @param {string} base - The base URL. - * @param {boolean} isTarget - Whether the target is a module. - */ -function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, internal, base, isTarget) { - if (process.noDeprecation) { - return; - } - const pjsonPath = fileURLToPath(pjsonUrl); - const double = RegExpPrototypeExec(doubleSlashRegEx, isTarget ? target : request) !== null; - process.emitWarning( - `Use of deprecated ${double ? 'double slash' : - 'leading or trailing slash matching'} resolving "${target}" for module ` + - `request "${request}" ${request !== match ? `matched to "${match}" ` : '' - }in the "${internal ? 'imports' : 'exports'}" field module resolution of the package at ${ - pjsonPath}${base ? ` imported from ${fileURLToPath(base)}` : ''}.`, - 'DeprecationWarning', - 'DEP0166', - ); -} - /** * Emits a deprecation warning if the given URL is a module and * the package.json file does not define a "main" or "exports" field. @@ -323,7 +267,6 @@ function invalidPackageTarget( } const invalidSegmentRegEx = /(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))?(\\|\/|$)/i; -const deprecatedInvalidSegmentRegEx = /(^|\\|\/)((\.|%2e)(\.|%2e)?|(n|%6e|%4e)(o|%6f|%4f)(d|%64|%44)(e|%65|%45)(_|%5f)(m|%6d|%4d)(o|%6f|%4f)(d|%64|%44)(u|%75|%55)(l|%6c|%4c)(e|%65|%45)(s|%73|%53))(\\|\/|$)/i; const patternRegEx = /\*/g; /** @@ -335,7 +278,6 @@ const patternRegEx = /\*/g; * @param {string} base - The base URL to resolve the target against. * @param {RegExp} pattern - The pattern to replace in the target string. * @param {boolean} internal - Whether the target is internal to the package. - * @param {boolean} isPathMap - Whether the target is a path map. * @param {string[]} conditions - The import conditions. * @returns {URL} - The resolved URL object. * @throws {ERR_INVALID_PACKAGE_TARGET} - If the target is invalid. @@ -349,7 +291,6 @@ function resolvePackageTargetString( base, pattern, internal, - isPathMap, conditions, ) { @@ -374,19 +315,7 @@ function resolvePackageTargetString( } if (RegExpPrototypeExec(invalidSegmentRegEx, StringPrototypeSlice(target, 2)) !== null) { - if (RegExpPrototypeExec(deprecatedInvalidSegmentRegEx, StringPrototypeSlice(target, 2)) === null) { - if (!isPathMap) { - const request = pattern ? - StringPrototypeReplace(match, '*', () => subpath) : - match + subpath; - const resolvedTarget = pattern ? - RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) : - target; - emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, true); - } - } else { - throw invalidPackageTarget(match, target, packageJSONUrl, internal, base); - } + throw invalidPackageTarget(match, target, packageJSONUrl, internal, base); } const resolved = new URL(target, packageJSONUrl); @@ -401,16 +330,7 @@ function resolvePackageTargetString( if (RegExpPrototypeExec(invalidSegmentRegEx, subpath) !== null) { const request = pattern ? StringPrototypeReplace(match, '*', () => subpath) : match + subpath; - if (RegExpPrototypeExec(deprecatedInvalidSegmentRegEx, subpath) === null) { - if (!isPathMap) { - const resolvedTarget = pattern ? - RegExpPrototypeSymbolReplace(patternRegEx, target, () => subpath) : - target; - emitInvalidSegmentDeprecation(resolvedTarget, request, match, packageJSONUrl, internal, base, false); - } - } else { - throwInvalidSubpath(request, match, packageJSONUrl, internal, base); - } + throwInvalidSubpath(request, match, packageJSONUrl, internal, base); } if (pattern) { @@ -442,16 +362,15 @@ function isArrayIndex(key) { * @param {string} base - The base path to resolve. * @param {RegExp} pattern - The pattern to match. * @param {boolean} internal - Whether the package is internal. - * @param {boolean} isPathMap - Whether the package is a path map. * @param {Set} conditions - The conditions to match. * @returns {URL | null | undefined} - The resolved target, or null if not found, or undefined if not resolvable. */ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, - base, pattern, internal, isPathMap, conditions) { + base, pattern, internal, conditions) { if (typeof target === 'string') { return resolvePackageTargetString( target, subpath, packageSubpath, packageJSONUrl, base, pattern, internal, - isPathMap, conditions); + conditions); } else if (ArrayIsArray(target)) { if (target.length === 0) { return null; @@ -464,7 +383,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, try { resolveResult = resolvePackageTarget( packageJSONUrl, targetItem, subpath, packageSubpath, base, pattern, - internal, isPathMap, conditions); + internal, conditions); } catch (e) { lastException = e; if (e.code === 'ERR_INVALID_PACKAGE_TARGET') { @@ -501,7 +420,7 @@ function resolvePackageTarget(packageJSONUrl, target, subpath, packageSubpath, const conditionalTarget = target[key]; const resolveResult = resolvePackageTarget( packageJSONUrl, conditionalTarget, subpath, packageSubpath, base, - pattern, internal, isPathMap, conditions); + pattern, internal, conditions); if (resolveResult === undefined) { continue; } return resolveResult; } @@ -560,12 +479,18 @@ function packageExportsResolve( exports = { '.': exports }; } + if (StringPrototypeEndsWith(packageSubpath, '/')) { + const reason = `is not a valid subpath for the "exports" resolution of ${ + fileURLToPath(packageJSONUrl)}`; + throw new ERR_INVALID_MODULE_SPECIFIER(packageSubpath, reason, + base && fileURLToPath(base)); + } + if (ObjectPrototypeHasOwnProperty(exports, packageSubpath) && - !StringPrototypeIncludes(packageSubpath, '*') && - !StringPrototypeEndsWith(packageSubpath, '/')) { + !StringPrototypeIncludes(packageSubpath, '*')) { const target = exports[packageSubpath]; const resolveResult = resolvePackageTarget( - packageJSONUrl, target, '', packageSubpath, base, false, false, false, + packageJSONUrl, target, '', packageSubpath, base, false, false, conditions, ); @@ -585,16 +510,6 @@ function packageExportsResolve( if (patternIndex !== -1 && StringPrototypeStartsWith(packageSubpath, 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, - base); - } const patternTrailer = StringPrototypeSlice(key, patternIndex + 1); if (packageSubpath.length >= key.length && StringPrototypeEndsWith(packageSubpath, patternTrailer) && @@ -618,7 +533,6 @@ function packageExportsResolve( base, true, false, - StringPrototypeEndsWith(packageSubpath, '/'), conditions); if (resolveResult == null) { @@ -674,7 +588,7 @@ function packageImportsResolve(name, base, conditions) { if (ObjectPrototypeHasOwnProperty(imports, name) && !StringPrototypeIncludes(name, '*')) { const resolveResult = resolvePackageTarget( - packageJSONUrl, imports[name], '', name, base, false, true, false, + packageJSONUrl, imports[name], '', name, base, false, true, conditions, ); if (resolveResult != null) { @@ -708,7 +622,7 @@ function packageImportsResolve(name, base, conditions) { const resolveResult = resolvePackageTarget(packageJSONUrl, target, bestMatchSubpath, bestMatch, base, true, - true, false, conditions); + true, conditions); if (resolveResult != null) { return resolveResult; } diff --git a/test/es-module/test-esm-exports-deprecations.mjs b/test/es-module/test-esm-exports-deprecations.mjs index d4d8b8e3e379..7cc3d6e87b75 100644 --- a/test/es-module/test-esm-exports-deprecations.mjs +++ b/test/es-module/test-esm-exports-deprecations.mjs @@ -4,19 +4,6 @@ import assert from 'assert'; let curWarning = 0; const expectedWarnings = [ - 'Use of deprecated leading or trailing slash', - 'Use of deprecated double slash', - './/asdf.js', - '".//internal/test.js"', - '".//internal//test.js"', - '"./////internal/////test.js"', - '"./trailing-pattern-slash/"', - '"./subpath/dir1/dir1.js"', - '"./subpath//dir1/dir1.js"', - './/asdf.js', - '".//internal/test.js"', - '".//internal//test.js"', - '"./////internal/////test.js"', 'no_exports', 'default_index', ]; diff --git a/test/es-module/test-esm-exports.mjs b/test/es-module/test-esm-exports.mjs index bc065898c075..216576870b0e 100644 --- a/test/es-module/test-esm-exports.mjs +++ b/test/es-module/test-esm-exports.mjs @@ -41,20 +41,6 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; ['pkgexports/dir1/dir1/trailer', maybeWrapped({ default: 'main' })], ['pkgexports/dir2/dir2/trailer', maybeWrapped({ default: 'index' })], ['pkgexports/a/dir1/dir1', maybeWrapped({ default: 'main' })], - ['pkgexports/a/b/dir1/dir1', maybeWrapped({ default: 'main' })], - - // Deprecated: - // Double slashes: - ['pkgexports/a//dir1/dir1', maybeWrapped({ default: 'main' })], - // double slash target - ['pkgexports/doubleslash', maybeWrapped({ default: 'asdf' })], - // Null target with several slashes - ['pkgexports/sub//internal/test.js', maybeWrapped({ default: 'internal only' })], - ['pkgexports/sub//internal//test.js', maybeWrapped({ default: 'internal only' })], - ['pkgexports/sub/////internal/////test.js', maybeWrapped({ default: 'internal only' })], - // trailing slash - ['pkgexports/trailing-pattern-slash/', - maybeWrapped({ default: 'trailing-pattern-slash' })], ]); if (!isRequire) { @@ -112,6 +98,8 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; ['pkgexports/nofallback2', './nofallback2'], // Reaching into nested node_modules ['pkgexports/nodemodules', './nodemodules'], + // Double slash target + ['pkgexports/doubleslash', './doubleslash'], // Self resolve invalid ['pkgexports/resolve-self-invalid', './invalid2'], ]); @@ -124,6 +112,12 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; ['pkgexports/sub/no%64e_modules', './sub/no%64e_modules'], // Cannot backtrack below exposed path, even with percent encoded "." ['pkgexports/sub/%2e./asdf', './asdf'], + // Pattern matches containing empty segments (double slashes) + ['pkgexports/a/b/dir1/dir1', './a/b/dir1/dir1'], + ['pkgexports/a//dir1/dir1', './a//dir1/dir1'], + ['pkgexports/sub//internal/test.js', './sub//internal/test.js'], + ['pkgexports/sub//internal//test.js', './sub//internal//test.js'], + ['pkgexports/sub/////internal/////test.js', './sub/////internal/////test.js'], ]); for (const [specifier, subpath] of undefinedExports) { @@ -154,6 +148,13 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js'; })); } + // Subpaths ending in "/" are invalid, even when matching a pattern + loadFixture('pkgexports/trailing-pattern-slash/').catch(mustCall((err) => { + assert.strictEqual(err.code, 'ERR_INVALID_MODULE_SPECIFIER'); + assertStartsWith(err.message, 'Invalid module "./trailing-pattern-slash/"'); + assertIncludes(err.message, 'is not a valid subpath for the "exports"'); + })); + // Conditional export, even with no match, should still be used instead // of falling back to main if (isRequire) { diff --git a/test/es-module/test-esm-imports-deprecations.mjs b/test/es-module/test-esm-imports-deprecations.mjs deleted file mode 100644 index 775152f29ecc..000000000000 --- a/test/es-module/test-esm-imports-deprecations.mjs +++ /dev/null @@ -1,27 +0,0 @@ -// Flags: --pending-deprecation -import { mustCall } from '../common/index.mjs'; -import assert from 'assert'; - -let curWarning = 0; -const expectedWarnings = [ - 'Use of deprecated double slash', - 'Use of deprecated double slash', - './sub//null', - './sub/////null', - './sub//internal/test', - './sub//internal//test', - '#subpath/////internal', - '#subpath//asdf.asdf', - '#subpath/as//df.asdf', - './sub//null', - './sub/////null', - './sub//internal/test', - './sub//internal//test', - '#subpath/////internal', -]; - -process.addListener('warning', mustCall((warning) => { - assert(warning.stack.includes(expectedWarnings[curWarning++]), warning.stack); -}, expectedWarnings.length)); - -await import('./test-esm-imports.mjs'); diff --git a/test/es-module/test-esm-imports.mjs b/test/es-module/test-esm-imports.mjs index b9982e2d0a58..14c62ce6d774 100644 --- a/test/es-module/test-esm-imports.mjs +++ b/test/es-module/test-esm-imports.mjs @@ -29,10 +29,6 @@ const { requireImport, importImport } = importer; ['#external/subpath/asdf.js', maybeWrapped({ default: 'asdf' })], // Trailing pattern imports ['#subpath/asdf.asdf', maybeWrapped({ default: 'test' })], - // Leading slash - ['#subpath//asdf.asdf', maybeWrapped({ default: 'test' })], - // Double slash - ['#subpath/as//df.asdf', maybeWrapped({ default: 'test' })], ]); for (const [validSpecifier, expected] of internalImports) { @@ -63,6 +59,14 @@ const { requireImport, importImport } = importer; const invalidImportSpecifiers = new Map([ // Backtracking below the package base ['#subpath/sub/../../../belowbase', 'request is not a valid match in pattern'], + // Pattern matches containing empty segments (leading or double slashes) + ['#subpath//asdf.asdf', 'request is not a valid match in pattern'], + ['#subpath/as//df.asdf', 'request is not a valid match in pattern'], + ['#subpath//null', 'request is not a valid match in pattern'], + ['#subpath/////null', 'request is not a valid match in pattern'], + ['#subpath//internal/test', 'request is not a valid match in pattern'], + ['#subpath//internal//test', 'request is not a valid match in pattern'], + ['#subpath/////internal/////test', 'request is not a valid match in pattern'], // Percent-encoded slash errors ['#external/subpath/x%2Fy', 'must not include encoded "/" or "\\"'], ['#external/subpath/x%5Cy', 'must not include encoded "/" or "\\"'], @@ -109,11 +113,6 @@ const { requireImport, importImport } = importer; // Handle not found for the defined imports target not existing const nonDefinedImports = new Set([ '#notfound', - '#subpath//null', - '#subpath/////null', - '#subpath//internal/test', - '#subpath//internal//test', - '#subpath/////internal/////test', ]); for (const specifier of nonDefinedImports) { loadFixture(specifier).catch(mustCall((err) => {