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

Avoid fancy stack traces size computation #14930

Merged
merged 2 commits into from Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions packages/babel-core/src/errors/rewrite-stack-trace.ts
Expand Up @@ -46,15 +46,6 @@ const ErrorToString = Function.call.bind(Error.prototype.toString);

const SUPPORTED = !!Error.captureStackTrace;

// We add some extra frames to Error.stackTraceLimit, so that we can respect
// the original Error.stackTraceLimit even after removing all our internal
// frames.
// STACK_TRACE_LIMIT_DELTA should be bigger than the expected number of internal
// frames, but not too big because capturing the stack trace is slow (this is
// why Error.stackTraceLimit does not default to Infinity!).
// Increase it if needed.
const STACK_TRACE_LIMIT_DELTA = 100;

const START_HIDNG = "startHiding - secret - don't use this - v1";
const STOP_HIDNG = "stopHiding - secret - don't use this - v1";

Expand Down Expand Up @@ -131,7 +122,17 @@ function setupPrepareStackTrace() {

const { prepareStackTrace = defaultPrepareStackTrace } = Error;

Error.stackTraceLimit += STACK_TRACE_LIMIT_DELTA;
// We add some extra frames to Error.stackTraceLimit, so that we can
// always show some useful frames even after deleting ours.
// STACK_TRACE_LIMIT_DELTA should be around the maximum expected number
// of internal frames, and not too big because capturing the stack trace
// is slow (this is why Error.stackTraceLimit does not default to Infinity!).
// Increase it if needed.
const MIN_STACK_TRACE_LIMIT = 50;
Error.stackTraceLimit = Math.max(
Copy link
Member

Choose a reason for hiding this comment

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

  if (
    Error.stackTraceLimit < MIN_STACK_TRACE_LIMIT &&
    Error.stackTraceLimit > 10
  ) {
    Error.stackTraceLimit = MIN_STACK_TRACE_LIMIT;
  }

Would this be better?

Copy link
Member Author

Choose a reason for hiding this comment

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

I only skipped the 0 case. If Error.stackTraceLimit is a low number, we would otherwise often make it 0 by deleting our own frames (and 0 is worse then "a few more than expected", when you still want something).

Error.stackTraceLimit,
MIN_STACK_TRACE_LIMIT,
);

Error.prepareStackTrace = function stackTraceRewriter(err, trace) {
let newTrace = [];
Expand Down Expand Up @@ -160,10 +161,7 @@ function setupPrepareStackTrace() {
}
}

return prepareStackTrace(
err,
newTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA),
);
return prepareStackTrace(err, newTrace);
};
}

Expand Down
7 changes: 0 additions & 7 deletions packages/babel-core/test/errors-stacks.js
Expand Up @@ -76,13 +76,6 @@ const fixture = name =>
);

describe("@babel/core errors", function () {
beforeAll(() => {
Error.stackTraceLimit += 100;
});
afterAll(() => {
Error.stackTraceLimit -= 100;
});

it("error inside config function", function () {
expectError(() => {
babel.parseSync("foo;", {
Expand Down