Skip to content

errors: remove source map rekey logic - #39025

Closed
bcoe wants to merge 5 commits into
nodejs:masterfrom
bcoe:38945
Closed

errors: remove source map rekey logic#39025
bcoe wants to merge 5 commits into
nodejs:masterfrom
bcoe:38945

Conversation

@bcoe

@bcoebcoe commented Jun 13, 2021

Copy link
Copy Markdown
Contributor

The rekeying logic no longer seems to be necessary to ensure that source maps
are written to disk before the application exits.

Fixes#38945


I believe improvements could be made to both this class of error and to the error described in #39017, such that we try to still annotate the stack trace with the original call site. But, in both cases, I think it's good to land the obvious fix initially.

What we ultimately lose with source-maps enabled is the syntactic sugar of:

/Users/bencoe/oss/node-1/test/fixtures/source-map/throw-string.js:1
functionHello(){throw"goodbye"}Hello();
^

Which I don't think is standard stack trace output any ways.

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
Fixesnodejs#38945
@bcoe
bcoe requested review from aduh95 and targosJune 13, 2021 16:26
@github-actionsgithub-actionsBot added the needs-ci PRs that need a full CI run. label Jun 13, 2021
// 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
ContributorAuthor

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.

@aduh95aduh95 added the source maps Issues and PRs related to source map support. label Jun 13, 2021
Comment threadlib/internal/source_map/source_map_cache.js Outdated

@addaleaxaddaleax left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM with @aduh95's comment (handling null)

Benjamin E. Coeand others added 2 commits June 13, 2021 19:14
@bcoebcoe changed the title errors: don't rekey on primitive typeerrors: remove source map rekey logicJun 13, 2021
@bcoe

bcoe commented Jun 13, 2021

Copy link
Copy Markdown
ContributorAuthor

@addaleax I realized while addressing @aduh95's feedback that the test suite seems to work currently without the rekeying logic. I tested against the c8 test suite too, and it passes after removing rekey.

This logic originally existed for the edge case of an exception being thrown while a module was being loaded, which would mean that it was already garbage collected before source maps were serialized to disk, or applied to stack traces. I'm a little surprised tests now pass without the logic, but my temptation is to remove the code if it's not needed ... perhaps the GC of WeakMap values is doesn't happen before the process exits?

@bcoe
bcoe requested review from Trott, addaleax and aduh95June 13, 2021 23:49
@targostargos added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 14, 2021
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 14, 2021
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@aduh95

aduh95 commented Jun 14, 2021

Copy link
Copy Markdown
Contributor

@bcoe Do you think this is backportable to v14.x?

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@bcoe

bcoe commented Jun 15, 2021

Copy link
Copy Markdown
ContributorAuthor

Do you think this is backportable to v14.x?

@aduh95 I don't see any reason why not.

bcoe pushed a commit that referenced this pull request Jun 22, 2021
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
PR-URL: #39025
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@bcoe

bcoe commented Jun 22, 2021

Copy link
Copy Markdown
ContributorAuthor

Landed in c5cc3d4

@bcoebcoe closed this Jun 22, 2021
danielleadams pushed a commit that referenced this pull request Jun 22, 2021
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
PR-URL: #39025
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@danielleadamsdanielleadams mentioned this pull request Jun 22, 2021
richardlau pushed a commit that referenced this pull request Jul 19, 2021
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
PR-URL: #39025
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
richardlau pushed a commit that referenced this pull request Jul 20, 2021
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
PR-URL: #39025
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@richardlaurichardlau mentioned this pull request Jul 20, 2021
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ciPRs that need a full CI run.source mapsIssues and PRs related to source map support.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--enable-source-maps throws internal TypeError when throwing non-object value

6 participants

@bcoe@nodejs-github-bot@aduh95@Trott@addaleax@targos