summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMyles Borins <mylesborins@google.com>2020-04-17 17:13:16 -0400
committerRuben Bridgewater <ruben@bridgewater.de>2020-04-28 13:15:04 +0200
commite540d5cd9bf73a11c1ff3c9ed1ef17b5a5efa6a2 (patch)
tree66248961698ed69b01b49b683038235ac4e02636
parentbfa19c47a484147275817e5cef936fcb1234bca1 (diff)
downloadnode-new-e540d5cd9bf73a11c1ff3c9ed1ef17b5a5efa6a2.tar.gz
module: improve error for invalid package targets
For targets that are strings that do not start with `./` or `/` the error will now have additional information about what the programming error is. Closes: https://github.com/nodejs/node/issues/32034 PR-URL: https://github.com/nodejs/node/pull/32052 Fixes: https://github.com/nodejs/node/issues/32034 Reviewed-By: Geoffrey Booth <webmaster@geoffreybooth.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jan Krems <jan.krems@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Signed-off-by: Myles Borins <myles.borins@gmail.com>
-rw-r--r--lib/internal/errors.js17
-rw-r--r--test/es-module/test-esm-exports.mjs4
-rw-r--r--test/fixtures/node_modules/pkgexports/package.json1
3 files changed, 19 insertions, 3 deletions
diff --git a/lib/internal/errors.js b/lib/internal/errors.js
index 06e7219fe7..f7f027851b 100644
--- a/lib/internal/errors.js
+++ b/lib/internal/errors.js
@@ -20,6 +20,7 @@ const {
ObjectDefineProperty,
ObjectKeys,
StringPrototypeSlice,
+ StringPrototypeStartsWith,
Symbol,
SymbolFor,
WeakMap,
@@ -1100,18 +1101,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 "./"' : ''}`;
} 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 "./"' : ''}`;
}
} 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 ?
+ '; targets must start with "./"' : ''}`;
+ } 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 "./"';
} else {
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
diff --git a/test/es-module/test-esm-exports.mjs b/test/es-module/test-esm-exports.mjs
index f71e217295..2b58f28344 100644
--- a/test/es-module/test-esm-exports.mjs
+++ b/test/es-module/test-esm-exports.mjs
@@ -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'],
// Missing / invalid fallbacks
['pkgexports/nofallback1', './nofallback1'],
['pkgexports/nofallback2', './nofallback2'],
@@ -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');
+ }
}));
}
diff --git a/test/fixtures/node_modules/pkgexports/package.json b/test/fixtures/node_modules/pkgexports/package.json
index f3ec20c49b..43ccf7795b 100644
--- a/test/fixtures/node_modules/pkgexports/package.json
+++ b/test/fixtures/node_modules/pkgexports/package.json
@@ -13,6 +13,7 @@
"./invalid2": 1234,
"./invalid3": "",
"./invalid4": {},
+ "./invalid5": "invalid5.js",
"./fallbackdir/": [[], null, {}, "builtin:x/", "./fallbackfile", "./"],
"./fallbackfile": [[], null, {}, "builtin:x", "./asdf.js"],
"./nofallback1": [],