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

src,lib: print source map error source on demand #43875

Closed
wants to merge 2 commits into from

Conversation

legendecas
Copy link
Member

@legendecas legendecas commented Jul 17, 2022

The source context is not prepended to the value of the stack property when the source map is not enabled. Rather than prepending the error source context to the value of the stack property unconditionally, this PR aligns the behavior and only prints the source context when the error is not handled by userland (e.g. fatal errors).

Also, this PR fixes that when source-map support is enabled, the error source context is not pointing to where the error was thrown. For a source file "test.js" like:

const err = new Error('foo');
throw err;

The source context for the thrown error should be:

test.js:2
throw err; // << where the error was thrown
^

Error: foo
    at test.js:1:13 // << where the error was created

Fixes: #43186
Fixes: #41541

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Jul 17, 2022
@legendecas legendecas added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jul 17, 2022
@legendecas legendecas marked this pull request as ready for review July 17, 2022 14:50
@legendecas legendecas requested a review from bcoe July 18, 2022 02:13
@bcoe bcoe self-assigned this Jul 18, 2022
@legendecas legendecas added the request-ci Add this label to start a Jenkins CI on a PR. label Jul 18, 2022
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jul 18, 2022
@nodejs-github-bot
Copy link
Collaborator

Copy link
Contributor

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

I really like this approach 🎉

One thing I noticed, was the addition of kSourceURLMagicComment, I think this relates to evaluated code? If this addresses an issue we didn't catch with your last PR, perhaps we should add an additional test for it?

Local<Value> argv[] = {script_resource_name,
v8::Int32::New(isolate, linenum),
v8::Int32::New(isolate, columnum)};
MaybeLocal<Value> maybe_ret = env->get_source_map_error_source()->Call(
Copy link
Contributor

Choose a reason for hiding this comment

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

I like this approach a lot 👍 good idea moving this behaviour into C++ land.

lib/internal/source_map/prepare_stack_trace.js Outdated Show resolved Hide resolved
lib/internal/source_map/source_map_cache.js Show resolved Hide resolved
lib/internal/source_map/source_map_cache.js Outdated Show resolved Hide resolved
lib/internal/source_map/source_map_cache.js Outdated Show resolved Hide resolved
The source context is not prepended to the value of the `stack` property
when the source map is not enabled. Rather than prepending the error
source context to the value of the `stack` property unconditionally,
this patch aligns the behavior and only prints the source context when
the error is not handled by userland (e.g. fatal errors).

Also, this patch fixes that when source-map support is enabled, the
error source context is not pointing to where the error was thrown.
@legendecas
Copy link
Member Author

@bcoe thank you for the suggestion! Updated with stricter nullish check. :)

Copy link
Contributor

@bcoe bcoe left a comment

Choose a reason for hiding this comment

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

LGTM.

If you still have benchmarks setup, mind testing this branch against HEAD, for the source-map benchmarks?

  1. To make sure moving responsiblities into C++ didn't hit performance in any ways.
  2. To see if we actually improved performance in some cases.

@bcoe
Copy link
Contributor

bcoe commented Jul 22, 2022

@nodejs/cpp-reviewers would be good to have someone take a look at this PR.

@bcoe
Copy link
Contributor

bcoe commented Jul 22, 2022

@legendecas I'd make the case that, even though you could squint and call this a breaking change, perhaps it's worth landing in a patch, when we weigh the performance benefits against the likeliness of breaking people:

  • If it breaks people, it's most likely to break test suites.
  • The benefit of performance improvements for people printing errors might outweigh the cost of breakages for others.

This might be a question worth bringing to @nodejs/tsc

@mcollina mcollina added the tsc-agenda Issues and PRs to discuss during the meetings of the TSC. label Jul 22, 2022
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

Have we got a measure of the performance benefit?

I would recommend we land it on Node.js v18 in a minor release but do not backport to Node v16. Having it in Node.js v19 push one year and a few month widespread adoption of this change.

@mcollina
Copy link
Member

@nodejs/tsc would you be willing to land this in Node v18 with do-not-backport flags for v16?

@legendecas
Copy link
Member Author

Updated with a benchmark on accessing the error.stack property and the improvement in my local environment is considerable:

                                                      confidence improvement accuracy (*)   (**)  (***)
es/error-stack.js n=100000 method='sourcemap'                ***     59.82 %       ±1.97% ±2.65% ±3.50%
es/error-stack.js n=100000 method='without-sourcemap'                 0.07 %       ±0.58% ±0.77% ±1.01%

tniessen added a commit to tniessen/node that referenced this pull request Jul 28, 2022
@BridgeAR
Copy link
Member

@mcollina any specific reason why it should not be backported to e.g., v16?

@mcollina
Copy link
Member

Possible breakages. Maybe we could just label it as "backing" and reevaluate in a few months.

@aduh95 aduh95 added baking-for-lts PRs that need to wait before landing in a LTS release. backport-blocked-v16.x and removed dont-land-on-v16.x labels Jul 28, 2022
legendecas pushed a commit that referenced this pull request Jul 30, 2022
Refs: #43875

PR-URL: #44019
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Feng Yu <F3n67u@outlook.com>
danielleadams pushed a commit that referenced this pull request Aug 16, 2022
The source context is not prepended to the value of the `stack` property
when the source map is not enabled. Rather than prepending the error
source context to the value of the `stack` property unconditionally,
this patch aligns the behavior and only prints the source context when
the error is not handled by userland (e.g. fatal errors).

Also, this patch fixes that when source-map support is enabled, the
error source context is not pointing to where the error was thrown.

PR-URL: #43875
Fixes: #43186
Fixes: #41541
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
danielleadams pushed a commit that referenced this pull request Aug 16, 2022
Refs: #43875

PR-URL: #44019
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Feng Yu <F3n67u@outlook.com>
danielleadams added a commit that referenced this pull request Aug 16, 2022
Notable changes:

bootstrap: implement run-time user-land snapshots via --build-snapshot
and --snapshot-blob

This patch introduces `--build-snapshot` and `--snapshot-blob` options
for creating and using user land snapshots.

To generate a snapshot using snapshot.js as an entry point and write
the snapshot blob to snapshot.blob:

```bash
echo "globalThis.foo = 'I am from the snapshot'" > snapshot.js
node --snapshot-blob snapshot.blob --build-snapshot snapshot.js
```

To restore application state from snapshot.blob, with index.js as the
entry point script for the deserialized application:

```bash
echo "console.log(globalThis.foo)" > index.js
node --snapshot-blob snapshot.blob index.js
```

Users can also use the `v8.startupSnapshot` API to specify an entry
point at snapshot building time, thus avoiding the need of an additional
entry script at deserialization time:

```bash
echo "require('v8').startupSnapshot.setDeserializeMainFunction(() => console.log('I am from the snapshot'))" > snapshot.js
node --snapshot-blob snapshot.blob --build-snapshot snapshot.js
node --snapshot-blob snapshot.blob
```

Contributed by Joyee Cheung in #38905

Other notable changes:

* crypto:
  * (SEMVER-MINOR) allow zero-length IKM in HKDF and in webcrypto PBKDF2
  (Filip Skokan) #44201
  * (SEMVER-MINOR) allow zero-length secret KeyObject (Filip Skokan) #44201
* doc:
  * add MoLow to collaborators (Moshe Atlow) #44214
  * add ErickWendel to collaborators (Erick Wendel) #44088
* http:
  * (SEMVER-MINOR) make idle http parser count configurable (theanarkh) #43974
* net:
  * (SEMVER-MINOR) add local family (theanarkh) #43975
* src:
  * (SEMVER-MINOR) add detailed embedder process initialization API (Anna
  Henningsen) #44121
  * (SEMVER-MINOR) print source map error source on demand (Chengzhong
  Wu) #43875
* tls:
  * (SEMVER-MINOR) pass a valid socket on `tlsClientError` (Daeyeon
  Jeong) #44021
* worker:
  * deprecate `--trace-atomics-wait` (Keyhan Vakil) #44093
ruyadorno pushed a commit that referenced this pull request Aug 23, 2022
The source context is not prepended to the value of the `stack` property
when the source map is not enabled. Rather than prepending the error
source context to the value of the `stack` property unconditionally,
this patch aligns the behavior and only prints the source context when
the error is not handled by userland (e.g. fatal errors).

Also, this patch fixes that when source-map support is enabled, the
error source context is not pointing to where the error was thrown.

PR-URL: #43875
Fixes: #43186
Fixes: #41541
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
ruyadorno pushed a commit that referenced this pull request Aug 23, 2022
Refs: #43875

PR-URL: #44019
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Feng Yu <F3n67u@outlook.com>
ruyadorno added a commit that referenced this pull request Aug 23, 2022
Notable changes:

* bootstrap:
  * implement run-time user-land snapshots via --build-snapshot and
  --snapshot-blob (Joyee Cheung) in #38905
* crypto:
  * (SEMVER-MINOR) allow zero-length IKM in HKDF and in webcrypto PBKDF2
  (Filip Skokan) #44201
  * (SEMVER-MINOR) allow zero-length secret KeyObject
  (Filip Skokan) #44201
* deps:
  * upgrade npm to 8.18.0 (npm team) #44263 - Adds a new npm query cmd
* doc:
  * add Erick Wendel to collaborators (Erick Wendel) #44088
  * add MoLow to collaborators (Moshe Atlow) #44214
  * deprecate --trace-atomics-wait (Keyhan Vakil) #44093
* http:
  * (SEMVER-MINOR) make idle http parser count configurable
  (theanarkh) #43974
* net:
  * (SEMVER-MINOR) add local family (theanarkh) #43975
* src:
  * (SEMVER-MINOR) print source map error source on demand
  (Chengzhong Wu) #43875
* tls:
  * (SEMVER-MINOR) pass a valid socket on tlsClientError
  (Daeyeon Jeong) #44021

PR-URL: TBD
ruyadorno added a commit that referenced this pull request Aug 23, 2022
Notable changes:

* bootstrap:
  * implement run-time user-land snapshots via --build-snapshot and
  --snapshot-blob (Joyee Cheung) in #38905
* crypto:
  * (SEMVER-MINOR) allow zero-length IKM in HKDF and in webcrypto PBKDF2
  (Filip Skokan) #44201
  * (SEMVER-MINOR) allow zero-length secret KeyObject
  (Filip Skokan) #44201
* deps:
  * upgrade npm to 8.18.0 (npm team) #44263 - Adds a new npm query cmd
* doc:
  * add Erick Wendel to collaborators (Erick Wendel) #44088
  * add MoLow to collaborators (Moshe Atlow) #44214
  * deprecate --trace-atomics-wait (Keyhan Vakil) #44093
* http:
  * (SEMVER-MINOR) make idle http parser count configurable
  (theanarkh) #43974
* net:
  * (SEMVER-MINOR) add local family (theanarkh) #43975
* src:
  * (SEMVER-MINOR) print source map error source on demand
  (Chengzhong Wu) #43875
* tls:
  * (SEMVER-MINOR) pass a valid socket on tlsClientError
  (Daeyeon Jeong) #44021

PR-URL: #44353
ruyadorno added a commit that referenced this pull request Aug 23, 2022
Notable changes:

* bootstrap:
  * implement run-time user-land snapshots via --build-snapshot and
  --snapshot-blob (Joyee Cheung) in #38905
* crypto:
  * (SEMVER-MINOR) allow zero-length IKM in HKDF and in webcrypto PBKDF2
  (Filip Skokan) #44201
  * (SEMVER-MINOR) allow zero-length secret KeyObject
  (Filip Skokan) #44201
* deps:
  * upgrade npm to 8.18.0 (npm team) #44263 - Adds a new npm query cmd
* doc:
  * add Erick Wendel to collaborators (Erick Wendel) #44088
  * add theanarkh to collaborators (theanarkh) #44131
  * add MoLow to collaborators (Moshe Atlow) #44214
  * add cola119 to collaborators (cola119) #44248
  * deprecate --trace-atomics-wait (Keyhan Vakil) #44093
* http:
  * (SEMVER-MINOR) make idle http parser count configurable
  (theanarkh) #43974
* net:
  * (SEMVER-MINOR) add local family (theanarkh) #43975
* src:
  * (SEMVER-MINOR) print source map error source on demand
  (Chengzhong Wu) #43875
* tls:
  * (SEMVER-MINOR) pass a valid socket on tlsClientError
  (Daeyeon Jeong) #44021

PR-URL: #44353
ruyadorno added a commit that referenced this pull request Aug 24, 2022
Notable changes:

* bootstrap:
  * implement run-time user-land snapshots via --build-snapshot and
  --snapshot-blob (Joyee Cheung) in #38905
* crypto:
  * (SEMVER-MINOR) allow zero-length IKM in HKDF and in webcrypto PBKDF2
  (Filip Skokan) #44201
  * (SEMVER-MINOR) allow zero-length secret KeyObject
  (Filip Skokan) #44201
* deps:
  * upgrade npm to 8.18.0 (npm team) #44263 - Adds a new npm query cmd
* doc:
  * add Erick Wendel to collaborators (Erick Wendel) #44088
  * add theanarkh to collaborators (theanarkh) #44131
  * add MoLow to collaborators (Moshe Atlow) #44214
  * add cola119 to collaborators (cola119) #44248
  * deprecate --trace-atomics-wait (Keyhan Vakil) #44093
* http:
  * (SEMVER-MINOR) make idle http parser count configurable
  (theanarkh) #43974
* net:
  * (SEMVER-MINOR) add local family (theanarkh) #43975
* src:
  * (SEMVER-MINOR) print source map error source on demand
  (Chengzhong Wu) #43875
* tls:
  * (SEMVER-MINOR) pass a valid socket on tlsClientError
  (Daeyeon Jeong) #44021

PR-URL: #44353
}
}
}
if (sourceMap && sourceMap.data) {
return new SourceMap(sourceMap.data);
}
return undefined;
return null;
Copy link

Choose a reason for hiding this comment

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

Isn't this a breaking change? AVA for example relies on this that it returns undefined here.

Copy link
Member

Choose a reason for hiding this comment

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

Can you open a separate issue? This looks like something unintended that we can fix.

Copy link

Choose a reason for hiding this comment

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

sidwebworks pushed a commit to sidwebworks/node that referenced this pull request Aug 26, 2022
Notable changes:

* bootstrap:
  * implement run-time user-land snapshots via --build-snapshot and
  --snapshot-blob (Joyee Cheung) in nodejs#38905
* crypto:
  * (SEMVER-MINOR) allow zero-length IKM in HKDF and in webcrypto PBKDF2
  (Filip Skokan) nodejs#44201
  * (SEMVER-MINOR) allow zero-length secret KeyObject
  (Filip Skokan) nodejs#44201
* deps:
  * upgrade npm to 8.18.0 (npm team) nodejs#44263 - Adds a new npm query cmd
* doc:
  * add Erick Wendel to collaborators (Erick Wendel) nodejs#44088
  * add theanarkh to collaborators (theanarkh) nodejs#44131
  * add MoLow to collaborators (Moshe Atlow) nodejs#44214
  * add cola119 to collaborators (cola119) nodejs#44248
  * deprecate --trace-atomics-wait (Keyhan Vakil) nodejs#44093
* http:
  * (SEMVER-MINOR) make idle http parser count configurable
  (theanarkh) nodejs#43974
* net:
  * (SEMVER-MINOR) add local family (theanarkh) nodejs#43975
* src:
  * (SEMVER-MINOR) print source map error source on demand
  (Chengzhong Wu) nodejs#43875
* tls:
  * (SEMVER-MINOR) pass a valid socket on tlsClientError
  (Daeyeon Jeong) nodejs#44021

PR-URL: nodejs#44353
Fyko pushed a commit to Fyko/node that referenced this pull request Sep 15, 2022
The source context is not prepended to the value of the `stack` property
when the source map is not enabled. Rather than prepending the error
source context to the value of the `stack` property unconditionally,
this patch aligns the behavior and only prints the source context when
the error is not handled by userland (e.g. fatal errors).

Also, this patch fixes that when source-map support is enabled, the
error source context is not pointing to where the error was thrown.

PR-URL: nodejs#43875
Fixes: nodejs#43186
Fixes: nodejs#41541
Reviewed-By: Ben Coe <bencoe@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Fyko pushed a commit to Fyko/node that referenced this pull request Sep 15, 2022
Refs: nodejs#43875

PR-URL: nodejs#44019
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Feng Yu <F3n67u@outlook.com>
Fyko pushed a commit to Fyko/node that referenced this pull request Sep 15, 2022
Notable changes:

* bootstrap:
  * implement run-time user-land snapshots via --build-snapshot and
  --snapshot-blob (Joyee Cheung) in nodejs#38905
* crypto:
  * (SEMVER-MINOR) allow zero-length IKM in HKDF and in webcrypto PBKDF2
  (Filip Skokan) nodejs#44201
  * (SEMVER-MINOR) allow zero-length secret KeyObject
  (Filip Skokan) nodejs#44201
* deps:
  * upgrade npm to 8.18.0 (npm team) nodejs#44263 - Adds a new npm query cmd
* doc:
  * add Erick Wendel to collaborators (Erick Wendel) nodejs#44088
  * add theanarkh to collaborators (theanarkh) nodejs#44131
  * add MoLow to collaborators (Moshe Atlow) nodejs#44214
  * add cola119 to collaborators (cola119) nodejs#44248
  * deprecate --trace-atomics-wait (Keyhan Vakil) nodejs#44093
* http:
  * (SEMVER-MINOR) make idle http parser count configurable
  (theanarkh) nodejs#43974
* net:
  * (SEMVER-MINOR) add local family (theanarkh) nodejs#43975
* src:
  * (SEMVER-MINOR) print source map error source on demand
  (Chengzhong Wu) nodejs#43875
* tls:
  * (SEMVER-MINOR) pass a valid socket on tlsClientError
  (Daeyeon Jeong) nodejs#44021

PR-URL: nodejs#44353
@richardlau richardlau removed the baking-for-lts PRs that need to wait before landing in a LTS release. label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
8 participants