Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors: remove source map rekey logic #39025

Closed
wants to merge 5 commits into from
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
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
bcoe marked this conversation as resolved.
Show resolved Hide resolved
// 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
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
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
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