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: improve error for invalid package targets #32052

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/internal/errors.js
Expand Up @@ -20,6 +20,7 @@ const {
ObjectDefineProperty,
ObjectKeys,
StringPrototypeSlice,
StringPrototypeStartsWith,
Symbol,
SymbolFor,
WeakMap,
Expand Down Expand Up @@ -1104,18 +1105,28 @@ E('ERR_INVALID_PACKAGE_CONFIG', (path, message, hasMessage = true) => {
}, Error);
E('ERR_INVALID_PACKAGE_TARGET',
(pkgPath, key, subpath, target, base = undefined) => {
const relError = typeof target === 'string' &&
target.length && !StringPrototypeStartsWith(target, './');
if (key === null) {
if (subpath !== '') {
return `Invalid "exports" target ${JSONStringify(target)} defined ` +
`for '${subpath}' in the package config ${pkgPath} imported from ` +
base;
`${base}.${relError ? ' - targets must start with "./"' : ''}`;
MylesBorins marked this conversation as resolved.
Show resolved Hide resolved
} else {
return `Invalid "exports" main target ${target} defined in the ` +
`package config ${pkgPath} imported from ${base}.`;
`package config ${pkgPath} imported from ${base}${relError ?
' - targets must start with "./"' : ''}`;
MylesBorins marked this conversation as resolved.
Show resolved Hide resolved
}
} else if (key === '.') {
return `Invalid "exports" main target ${JSONStringify(target)} defined ` +
`in the package config ${pkgPath}${sep}package.json`;
`in the package config ${pkgPath}${sep}package.json${relError ?
guybedford marked this conversation as resolved.
Show resolved Hide resolved
' - targets must start with "./"' : ''}`;
MylesBorins marked this conversation as resolved.
Show resolved Hide resolved
} else if (typeof target === 'string' && target !== '' &&
!StringPrototypeStartsWith(target, './')) {
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
`package config ${pkgPath}${sep}package.json. ` +
'- targets must start with "./"';
MylesBorins marked this conversation as resolved.
Show resolved Hide resolved
} else {
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
Expand Down
4 changes: 4 additions & 0 deletions test/es-module/test-esm-exports.mjs
Expand Up @@ -78,6 +78,7 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
['pkgexports/null', './null'],
['pkgexports/invalid2', './invalid2'],
['pkgexports/invalid3', './invalid3'],
['pkgexports/invalid5', 'invalid5'],
jkrems marked this conversation as resolved.
Show resolved Hide resolved
// Missing / invalid fallbacks
['pkgexports/nofallback1', './nofallback1'],
['pkgexports/nofallback2', './nofallback2'],
Expand Down Expand Up @@ -106,6 +107,9 @@ import fromInside from '../fixtures/node_modules/pkgexports/lib/hole.js';
strictEqual(err.code, 'ERR_INVALID_PACKAGE_TARGET');
assertStartsWith(err.message, 'Invalid "exports"');
assertIncludes(err.message, subpath);
if (!subpath.startsWith('./')) {
assertIncludes(err.message, 'targets must start with');
}
}));
}

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.