Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ea73af5

Browse files
bcoesbaayel
authored andcommittedDec 27, 2023
errors: don't throw TypeError on missing export
Logic in module_job.js assumes detailed stack trace from node_errors.cc which is not populated when --enable-source-maps is set. Fixes #38790 PR-URL: #39017 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
1 parent 0d86855 commit ea73af5

7 files changed

+35
-1
lines changed
 

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const {
2424
const { ModuleWrap } = internalBinding('module_wrap');
2525

2626
const { decorateErrorStack } = require('internal/util');
27+
const {
28+
getSourceMapsEnabled,
29+
} = require('internal/source_map/source_map_cache');
2730
const assert = require('internal/assert');
2831
const resolvedPromise = PromiseResolve();
2932

@@ -122,7 +125,12 @@ class ModuleJob {
122125
}
123126
} catch (e) {
124127
decorateErrorStack(e);
125-
if (StringPrototypeIncludes(e.message,
128+
// TODO(@bcoe): Add source map support to exception that occurs as result
129+
// of missing named export. This is currently not possible because
130+
// stack trace originates in module_job, not the file itself. A hidden
131+
// symbol with filename could be set in node_errors.cc to facilitate this.
132+
if (!getSourceMapsEnabled() &&
133+
StringPrototypeIncludes(e.message,
126134
' does not provide an export named')) {
127135
const splitStack = StringPrototypeSplit(e.stack, '\n');
128136
const parentFileUrl = StringPrototypeReplace(

‎test/fixtures/source-map/esm-export-missing-module.mjs

Whitespace-only changes.

‎test/fixtures/source-map/esm-export-missing-module.mjs.map

+1
Original file line numberDiff line numberDiff line change
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import { Something } from './esm-export-missing-module.mjs';
2+
//# sourceMappingURL=esm-export-missing.mjs.map

‎test/fixtures/source-map/esm-export-missing.mjs.map

+1
Original file line numberDiff line numberDiff line change
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
import { Something } from './exm-export-missing-module.mjs';
3+
console.info(Something);

‎test/parallel/test-source-map-enable.js

+19
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,25 @@ function nextdir() {
324324
assert.ok(sourceMap);
325325
}
326326

327+
// Does not throw TypeError when exception occurs as result of missing named
328+
// export.
329+
{
330+
const coverageDirectory = nextdir();
331+
const output = spawnSync(process.execPath, [
332+
'--enable-source-maps',
333+
require.resolve('../fixtures/source-map/esm-export-missing.mjs'),
334+
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
335+
const sourceMap = getSourceMapFromCache(
336+
'esm-export-missing.mjs',
337+
coverageDirectory
338+
);
339+
// Module loader error displayed.
340+
assert.match(output.stderr.toString(),
341+
/does not provide an export named 'Something'/);
342+
// Source map should have been serialized.
343+
assert.ok(sourceMap);
344+
}
345+
327346
function getSourceMapFromCache(fixtureFile, coverageDirectory) {
328347
const jsonFiles = fs.readdirSync(coverageDirectory);
329348
for (const jsonFile of jsonFiles) {

0 commit comments

Comments
 (0)
Please sign in to comment.