Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
errors: don't rekey on primitive type
If an error is thrown before a module is loaded, we attempt to cache
source map against error object, rather than module object. We
can't do this if the error is a primitive type

Fixes #38945
  • Loading branch information
bcoe
bcoe committed Jun 13, 2021
commit 773ea75254a1d792a7adc59fb7945b380cb95d0f
6 changes: 5 additions & 1 deletion lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ function sourcesToAbsolute(baseURL, data) {
// Move source map from garbage collected module to alternate key.
function rekeySourceMap(cjsModuleInstance, newInstance) {
const sourceMap = cjsSourceMapCache.get(cjsModuleInstance);
if (sourceMap) {
// If an exception occurs before a module finishes loading it will removed
Comment thread
bcoe marked this conversation as resolved.
Outdated
// from the module cache and our WeakMap. To allow a source map to still be
// applied to the stack trace when this happens, we rekey on the thrown error.
// This is only possible if the error is an object and not a primitive type.
if (sourceMap && typeof newInstance === 'object') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could newInstance be null, and if so, wouldn't that cause an error? Can you add a test case please?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aduh95 I did some testing it no longer seems like that rekey logic is needed to ensure that source maps are written to disk prior to exit. Perhaps this is because of refactors that have taken place related to source map serialization? Or perhaps a FinalizationRegistry fires on the next tick, such that the source map will be available while the process shuts down?

Either way, no tests fail if we simply remove the rekey logic.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I still think it'd be interesting to have a test case with a throw null for completeness sake.

cjsSourceMapCache.set(newInstance, sourceMap);
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/source-map/throw-string-original.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* comments dropped by uglify.
*/
function Hello() {
throw 'goodbye';
}

Hello();
2 changes: 2 additions & 0 deletions test/fixtures/source-map/throw-string.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/parallel/test-source-map-enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,23 @@ function nextdir() {
assert.ok(sourceMap);
}

// Does not attempt to rekey source map on primitive type.
{
const coverageDirectory = nextdir();
const output = spawnSync(process.execPath, [
'--enable-source-maps',
require.resolve('../fixtures/source-map/throw-string.js'),
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
const sourceMap = getSourceMapFromCache(
'throw-string.js',
coverageDirectory
);
// Original stack trace.
assert.match(output.stderr.toString(), /goodbye/);
// Source map should have been serialized.
assert.ok(sourceMap);
}

function getSourceMapFromCache(fixtureFile, coverageDirectory) {
const jsonFiles = fs.readdirSync(coverageDirectory);
for (const jsonFile of jsonFiles) {
Expand Down