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

take ownership of prepareStackTrace for upstream v8 changes #23926

Merged
merged 1 commit into from
May 21, 2019

Conversation

devsnek
Copy link
Member

@devsnek devsnek commented Oct 27, 2018

These changes exist for a few reasons:

  • We have a double-decoration problem where people need to use process.binding('util') to stop node from decorating errors if they want to do their own thing.
  • V8 is making upstream changes to Error.prepareStackTrace which scope it per-context and can't land those until we fix how we handle Error.prepareStackTrace in repl
  • Error stack decoration was not very consistent, as it pinged around between two methods in C++ and JS that had to be called in the correct order

Closes #21958

/cc @BridgeAR @joyeecheung @bmeck @jdalton @mmarchini @ljharb

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@devsnek devsnek added the wip Issues and PRs that are still a work in progress. label Oct 27, 2018
@nodejs-github-bot nodejs-github-bot added the lib / src Issues and PRs related to general changes in the lib or src directory. label Oct 27, 2018
@devsnek
Copy link
Member Author

devsnek commented Oct 27, 2018

I'm also unsure of how this should be semver classified.

src/node_errors.h Outdated Show resolved Hide resolved
Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

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

1st commit looks good with nits, and style conformance.

src/node_errors.h Outdated Show resolved Hide resolved
src/node_errors.cc Outdated Show resolved Hide resolved
Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

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

4th commit looks good with nits.

lib/internal/bootstrap/node.js Outdated Show resolved Hide resolved
src/node.cc Outdated Show resolved Hide resolved
src/node.cc Outdated Show resolved Hide resolved
src/node_errors.cc Outdated Show resolved Hide resolved
src/node.cc Outdated Show resolved Hide resolved
src/node.cc Outdated Show resolved Hide resolved
src/node.cc Outdated Show resolved Hide resolved
@refack
Copy link
Contributor

refack commented Oct 27, 2018

+1 I like the idea of owning _setupPrepareStackTrace. Two comments:
Can this change be made semver-minor (modulo Error.prepareStackTrace behaviour), at least to the extent the we generate the same stack trace as the current?
IMHO first commit can be its own PR, and land sooner.

@refack refack added the errors Issues and PRs related to JavaScript errors originated in Node.js core. label Oct 27, 2018
@devsnek devsnek mentioned this pull request Oct 29, 2018
@devsnek devsnek force-pushed the refactor/errors branch 2 times, most recently from fe70f4e to f36a5e3 Compare October 29, 2018 01:25
@devsnek
Copy link
Member Author

devsnek commented Oct 29, 2018

@nodejs/assert can someone take a look at how this effects getErrMessage?

Copy link
Contributor

@refack refack left a comment

Choose a reason for hiding this comment

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

LGTM

src/node_errors.cc Outdated Show resolved Hide resolved
@jdalton
Copy link
Member

jdalton commented Oct 29, 2018

I've compiled the PR but haven't had a chance to check it against my use case. I should be able to in the next couple days though.

Update:

It looks like I can work with this change. It doesn't re-decorate decorated stacks so for me this should be OK.

@devsnek
Copy link
Member Author

devsnek commented Nov 3, 2018

@nodejs/assert another ping... can someone take a look at what changes need to be made to assertion error decoration

@refack
Copy link
Contributor

refack commented Nov 3, 2018

@Trott
Copy link
Member

Trott commented Nov 4, 2018

@BridgeAR Any chance you have some time to look at this proposed change?

lib/repl.js Outdated Show resolved Hide resolved
return `${sourceLine}
${' '.repeat(startPosition)}${'^'.repeat(endPosition - startPosition)}
${errorString}
at ${trace.join('\n at ')}`;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: We do not really use multi line template strings in /lib. It would be nice to replace this using string concat.

Copy link
Member Author

Choose a reason for hiding this comment

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

why

Copy link
Contributor

@refack refack Nov 4, 2018

Choose a reason for hiding this comment

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

why

That one of my favorite questions ;)
Sometimes the answer is "tradition", but in this case I think it's readability, as it breaks the indentation.
There also might be some weirdness in Windows where EOL is \r\n if you set git to autocrlf=true, and that changes the strings. I'm so so sorry and a little ashamed for my prefered platform, but then again macOS has .DS_Store files so...

Copy link
Member Author

Choose a reason for hiding this comment

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

take your vote i guess

    return `${resourceName}:${lineNumber}
${sourceLine}
${' '.repeat(startPosition)}${'^'.repeat(endPosition - startPosition)}

${errorString}
    at ${trace.join('\n    at ')}`;

vs

    return `${resourceName}:${lineNumber}\n${sourceLine}\n` +
      ' '.repeat(startPosition) + '^'.repeat(endPosition - startPosition) +
      `\n\n${errorString}\n    at ${trace.join('\n    at ')}`;

Copy link
Member

Choose a reason for hiding this comment

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

It's probably going to look a bit better if you break at the end of every \n..

Copy link
Member Author

Choose a reason for hiding this comment

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

@joyeecheung like this?

    return `${resourceName}:${lineNumber}\n${sourceLine}\n` +
      ' '.repeat(startPosition) + '^'.repeat(endPosition - startPosition) +
      '\n' +
      '\n' +
      errorString +
      '\n' +
      `    at ${trace.join('\n    at ')}`;

Copy link
Contributor

@refack refack Nov 4, 2018

Choose a reason for hiding this comment

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

Maybe (just to reduce the '\n' kruft):

const numberOfCarrots = endPosition - startPosition;
const traceLines = [
  `${resourceName}:${lineNumber}`,
  `${sourceLine}`,
  ' '.repeat(startPosition) + '^'.repeat(numberOfCarrots),
  '',
  errorString,
  ...(trace.map((t) => `    at ${t}`)),
];
return traceLines.join('\n');

(carrot is funnier than carat)

Also could concat the trace more elegantly:

  errorString,
];
traceLines = traceLines.concat(trace.map((t) => `    at ${t}`))
return traceLines.join('\n');

Copy link
Member Author

@devsnek devsnek Nov 4, 2018

Choose a reason for hiding this comment

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

all of these are equally unreadable and the last one is creating an array just for the purpose of joining it...

i understand the indentation hazard but that's why we have tests right?

Copy link
Contributor

Choose a reason for hiding this comment

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

all of these are equally unreadable and the last one is creating an array just for the purpose of joining it...

No strong argument from me... Trying to make the best of a complicated situation.
as for the array, it's probably going to be the last code run by the process, so..

Copy link
Member

Choose a reason for hiding this comment

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

@devsnek it has been something that we tried to stick to in the whole code base. Not everyone is aware how multi line template strings actually work as people forget that leading spaces actually count. This was an issue in multiple PRs so far. So it's not only about the readability but about correctness as well (not in this specific PR but as a general issue).

Sadly there's no eslint rule to only forbid multi line template strings. We should probably write one to enforce this.

@BridgeAR
Copy link
Member

BridgeAR commented Nov 4, 2018

I have to compile this on my own and play around with it. If I understand this correct it will from now on always attach the source to the stack (if the user did not override the function). That is great! It would be nice if the error decoration could be expanded to cover multiple line statements fully and not only print the first line of the statement. Besides that it seems like the position indicator is not always correct (I checked some error output), so that should be looked at again.

It does not directly seem to impact getErrMessage() if the user does not override the prepareStackTrace() function. I did not yet test this but I guess if the user overrides it, the getErrMessage() function would fail? That would be pretty bad and we should try to find away around it.

We definitely have to think about changing the simple assert error message again before this lands since it would otherwise print redundant information.

@devsnek please correct me if I am wrong with anything I said here.

@devsnek
Copy link
Member Author

devsnek commented Nov 4, 2018

@BridgeAR i think removing it entirely would make sense with this change.

Before After

@devsnek devsnek force-pushed the refactor/errors branch 2 times, most recently from d2764d5 to a55c34f Compare May 9, 2019 13:46
@devsnek devsnek requested a review from joyeecheung May 9, 2019 13:47
@devsnek devsnek force-pushed the refactor/errors branch 2 times, most recently from 9325c95 to 2f00fbf Compare May 12, 2019 01:36
@devsnek devsnek removed the blocked PRs that are blocked by other issues or PRs. label May 12, 2019
@devsnek devsnek added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label May 16, 2019
@nodejs-github-bot
Copy link
Collaborator

@devsnek
Copy link
Member Author

devsnek commented May 16, 2019

@joyeecheung does the current init code here make sense?

lib/internal/bootstrap/loaders.js Outdated Show resolved Hide resolved
src/api/environment.cc Show resolved Hide resolved
@devsnek devsnek force-pushed the refactor/errors branch 2 times, most recently from 9272abf to a647319 Compare May 20, 2019 16:49
@nodejs-github-bot
Copy link
Collaborator

@devsnek devsnek requested a review from joyeecheung May 20, 2019 22:14
lib/internal/bootstrap/node.js Outdated Show resolved Hide resolved
@devsnek devsnek force-pushed the refactor/errors branch 2 times, most recently from ee4d8bd to 73be33e Compare May 21, 2019 16:53
@nodejs-github-bot
Copy link
Collaborator

Refs https://crbug.com/v8/7848

PR-URL: nodejs#23926
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
@devsnek
Copy link
Member Author

devsnek commented May 21, 2019

Landed in b046bd1

@devsnek devsnek merged commit b046bd1 into nodejs:master May 21, 2019
@devsnek devsnek deleted the refactor/errors branch May 21, 2019 20:41
BridgeAR pushed a commit that referenced this pull request May 22, 2019
Refs https://crbug.com/v8/7848

PR-URL: #23926
Reviewed-By: Refael Ackermann <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
@BridgeAR BridgeAR mentioned this pull request May 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. errors Issues and PRs related to JavaScript errors originated in Node.js core. lib / src Issues and PRs related to general changes in the lib or src directory.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

intent to implement - rework of stack trace decoration
10 participants