Skip to content

Commit 6e622d6

Browse files
guybedfordruyadorno
authored andcommitted
esm: better package.json parser errors
PR-URL: #35117 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent b75822d commit 6e622d6

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

lib/internal/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,7 @@ E('ERR_INVALID_OPT_VALUE', (name, value, reason = '') => {
11191119
E('ERR_INVALID_OPT_VALUE_ENCODING',
11201120
'The value "%s" is invalid for option "encoding"', TypeError);
11211121
E('ERR_INVALID_PACKAGE_CONFIG', (path, base, message) => {
1122-
return `Invalid package config ${path}${base ? ` imported from ${base}` :
1122+
return `Invalid package config ${path}${base ? ` while importing ${base}` :
11231123
''}${message ? `. ${message}` : ''}`;
11241124
}, Error);
11251125
E('ERR_INVALID_PACKAGE_TARGET',

lib/internal/modules/esm/resolve.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function tryStatSync(path) {
8282
}
8383
}
8484

85-
function getPackageConfig(path) {
85+
function getPackageConfig(path, specifier, base) {
8686
const existing = packageJSONCache.get(path);
8787
if (existing !== undefined) {
8888
return existing;
@@ -106,7 +106,11 @@ function getPackageConfig(path) {
106106
try {
107107
packageJSON = JSONParse(source);
108108
} catch (error) {
109-
throw new ERR_INVALID_PACKAGE_CONFIG(path, null, error.message);
109+
throw new ERR_INVALID_PACKAGE_CONFIG(
110+
path,
111+
(base ? `"${specifier}" from ` : '') + fileURLToPath(base || specifier),
112+
error.message
113+
);
110114
}
111115

112116
let { imports, main, name, type } = packageJSON;
@@ -130,13 +134,14 @@ function getPackageConfig(path) {
130134
return packageConfig;
131135
}
132136

133-
function getPackageScopeConfig(resolved, base) {
137+
function getPackageScopeConfig(resolved) {
134138
let packageJSONUrl = new URL('./package.json', resolved);
135139
while (true) {
136140
const packageJSONPath = packageJSONUrl.pathname;
137141
if (StringPrototypeEndsWith(packageJSONPath, 'node_modules/package.json'))
138142
break;
139-
const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl), base);
143+
const packageConfig = getPackageConfig(fileURLToPath(packageJSONUrl),
144+
resolved);
140145
if (packageConfig.exists) return packageConfig;
141146

142147
const lastPackageJSONUrl = packageJSONUrl;
@@ -497,7 +502,7 @@ function packageImportsResolve(name, base, conditions) {
497502
throw new ERR_INVALID_MODULE_SPECIFIER(name, reason, fileURLToPath(base));
498503
}
499504
let packageJSONUrl;
500-
const packageConfig = getPackageScopeConfig(base, base);
505+
const packageConfig = getPackageScopeConfig(base);
501506
if (packageConfig.exists) {
502507
packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
503508
const imports = packageConfig.imports;
@@ -535,7 +540,7 @@ function packageImportsResolve(name, base, conditions) {
535540
}
536541

537542
function getPackageType(url) {
538-
const packageConfig = getPackageScopeConfig(url, url);
543+
const packageConfig = getPackageScopeConfig(url);
539544
return packageConfig.type;
540545
}
541546

@@ -580,7 +585,7 @@ function packageResolve(specifier, base, conditions) {
580585
StringPrototypeSlice(specifier, separatorIndex));
581586

582587
// ResolveSelf
583-
const packageConfig = getPackageScopeConfig(base, base);
588+
const packageConfig = getPackageScopeConfig(base);
584589
if (packageConfig.exists) {
585590
const packageJSONUrl = pathToFileURL(packageConfig.pjsonPath);
586591
if (packageConfig.name === packageName &&
@@ -608,7 +613,7 @@ function packageResolve(specifier, base, conditions) {
608613
}
609614

610615
// Package match.
611-
const packageConfig = getPackageConfig(packageJSONPath, base);
616+
const packageConfig = getPackageConfig(packageJSONPath, specifier, base);
612617
if (packageConfig.exports !== undefined && packageConfig.exports !== null)
613618
return packageExportsResolve(
614619
packageJSONUrl, packageSubpath, packageConfig, base, conditions

test/es-module/test-esm-invalid-pjson.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ child.on('close', mustCall((code, signal) => {
1919
strictEqual(signal, null);
2020
ok(
2121
stderr.includes(
22-
[
23-
'[ERR_INVALID_PACKAGE_CONFIG]: ',
24-
`Invalid package config ${invalidJson}. `,
25-
`Unexpected token } in JSON at position ${isWindows ? 16 : 14}`
26-
].join(''),
22+
`[ERR_INVALID_PACKAGE_CONFIG]: Invalid package config ${invalidJson} ` +
23+
`while importing "invalid-pjson" from ${entry}. ` +
24+
`Unexpected token } in JSON at position ${isWindows ? 16 : 14}`
2725
),
2826
stderr);
2927
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
syntax error

0 commit comments

Comments
 (0)