Skip to content

Commit ff63ef4

Browse files
committed
esm: preserve syntax error locations in dynamic imports
1 parent f71d644 commit ff63ef4

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎lib/internal/modules/esm/loader.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
const { getOptionValue } = require('internal/options');
3434
const { isURL, pathToFileURL } = require('internal/url');
3535
const {
36+
decorateErrorStack,
3637
getDeprecationWarningEmitter,
3738
kEmptyObject,
3839
} = require('internal/util');
@@ -438,7 +439,13 @@ class ModuleLoader {
438439
throw new ERR_UNKNOWN_MODULE_FORMAT(translatorKey, url);
439440
}
440441

441-
const result = FunctionPrototypeCall(translator, this, url, translateContext, parentURL);
442+
let result;
443+
try {
444+
result = FunctionPrototypeCall(translator, this, url, translateContext, parentURL);
445+
} catch (error) {
446+
decorateErrorStack(error);
447+
throw error;
448+
}
442449
assert(result instanceof ModuleWrap, `The ${format} module returned is not a ModuleWrap`);
443450
if (format === 'commonjs' || format === 'commonjs-sync' || format === 'require-commonjs') {
444451
result.isCommonJS = true;

‎test/es-module/test-esm-syntax-error.mjs‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,16 @@ describe('ESM: importing a module with syntax error(s)', { concurrency: !process
1313
assert.match(stderr, /SyntaxError:/);
1414
assert.notStrictEqual(code, 0);
1515
});
16+
17+
it('should include the source location when imported dynamically', async () => {
18+
const moduleURL = fixtures.fileURL('es-module-loaders', 'syntax-error.mjs');
19+
const { code, stderr } = await spawnPromisified(execPath, [
20+
'-e',
21+
`import(${JSON.stringify(moduleURL.href)}).catch(console.error)`,
22+
]);
23+
24+
assert.strictEqual(code, 0);
25+
assert.match(stderr, /syntax-error\.mjs:2/);
26+
assert.match(stderr, /await async \(\) => 0;\n\^+/);
27+
});
1628
});

0 commit comments

Comments
 (0)