Skip to content

Commit 11f1ad9

Browse files
targosnodejs-github-bot
authored andcommitted
module: only try to enrich CJS syntax errors
It is guaranteed that V8 throws a syntax error when `import` or `export` is used outside of ESM. Fixes: #35687 PR-URL: #35691 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 5b2c263 commit 11f1ad9

10 files changed

+69
-0
lines changed

β€Žlib/internal/modules/esm/translators.js

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const {
66
Boolean,
77
JSONParse,
8+
ObjectGetPrototypeOf,
89
ObjectPrototypeHasOwnProperty,
910
ObjectKeys,
1011
PromisePrototypeCatch,
@@ -15,6 +16,7 @@ const {
1516
StringPrototypeReplace,
1617
StringPrototypeSplit,
1718
StringPrototypeStartsWith,
19+
SyntaxErrorPrototype,
1820
} = primordials;
1921

2022
let _TYPES = null;
@@ -147,6 +149,9 @@ translators.set('module', async function moduleStrategy(url) {
147149
});
148150

149151
function enrichCJSError(err) {
152+
if (err == null || ObjectGetPrototypeOf(err) !== SyntaxErrorPrototype) {
153+
return;
154+
}
150155
const stack = StringPrototypeSplit(err.stack, '\n');
151156
/*
152157
* The regular expression below targets the most common import statement

β€Žtest/es-module/test-esm-cjs-load-error-note.mjs

+56
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const Import2 = fixtures.path('/es-modules/es-note-promiserej-import-2.cjs');
1010
const Import3 = fixtures.path('/es-modules/es-note-unexpected-import-3.cjs');
1111
const Import4 = fixtures.path('/es-modules/es-note-unexpected-import-4.cjs');
1212
const Import5 = fixtures.path('/es-modules/es-note-unexpected-import-5.cjs');
13+
const Error1 = fixtures.path('/es-modules/es-note-error-1.mjs');
14+
const Error2 = fixtures.path('/es-modules/es-note-error-2.mjs');
15+
const Error3 = fixtures.path('/es-modules/es-note-error-3.mjs');
16+
const Error4 = fixtures.path('/es-modules/es-note-error-4.mjs');
1317

1418
// Expect note to be included in the error output
1519
const expectedNote = 'To load an ES module, ' +
@@ -105,3 +109,55 @@ pImport5.on('close', mustCall((code) => {
105109
assert.ok(!pImport5Stderr.includes(expectedNote),
106110
`${expectedNote} must not be included in ${pImport5Stderr}`);
107111
}));
112+
113+
const pError1 = spawn(process.execPath, [Error1]);
114+
let pError1Stderr = '';
115+
pError1.stderr.setEncoding('utf8');
116+
pError1.stderr.on('data', (data) => {
117+
pError1Stderr += data;
118+
});
119+
pError1.on('close', mustCall((code) => {
120+
assert.strictEqual(code, expectedCode);
121+
assert.ok(pError1Stderr.includes('Error: some error'));
122+
assert.ok(!pError1Stderr.includes(expectedNote),
123+
`${expectedNote} must not be included in ${pError1Stderr}`);
124+
}));
125+
126+
const pError2 = spawn(process.execPath, [Error2]);
127+
let pError2Stderr = '';
128+
pError2.stderr.setEncoding('utf8');
129+
pError2.stderr.on('data', (data) => {
130+
pError2Stderr += data;
131+
});
132+
pError2.on('close', mustCall((code) => {
133+
assert.strictEqual(code, expectedCode);
134+
assert.ok(pError2Stderr.includes('string'));
135+
assert.ok(!pError2Stderr.includes(expectedNote),
136+
`${expectedNote} must not be included in ${pError2Stderr}`);
137+
}));
138+
139+
const pError3 = spawn(process.execPath, [Error3]);
140+
let pError3Stderr = '';
141+
pError3.stderr.setEncoding('utf8');
142+
pError3.stderr.on('data', (data) => {
143+
pError3Stderr += data;
144+
});
145+
pError3.on('close', mustCall((code) => {
146+
assert.strictEqual(code, expectedCode);
147+
assert.ok(pError3Stderr.includes('null'));
148+
assert.ok(!pError3Stderr.includes(expectedNote),
149+
`${expectedNote} must not be included in ${pError3Stderr}`);
150+
}));
151+
152+
const pError4 = spawn(process.execPath, [Error4]);
153+
let pError4Stderr = '';
154+
pError4.stderr.setEncoding('utf8');
155+
pError4.stderr.on('data', (data) => {
156+
pError4Stderr += data;
157+
});
158+
pError4.on('close', mustCall((code) => {
159+
assert.strictEqual(code, expectedCode);
160+
assert.ok(pError4Stderr.includes('undefined'));
161+
assert.ok(!pError4Stderr.includes(expectedNote),
162+
`${expectedNote} must not be included in ${pError4Stderr}`);
163+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('some error');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './es-note-error-1.cjs';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw 'string';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './es-note-error-2.cjs';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './es-note-error-3.cjs';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './es-note-error-4.cjs';

0 commit comments

Comments
Β (0)