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 all commits
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
27 changes: 13 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,18 @@ 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.
// However, we only do it if the user did not explicitly set it to 0.
const MIN_STACK_TRACE_LIMIT = 50;
Error.stackTraceLimit &&= Math.max(
Error.stackTraceLimit,
MIN_STACK_TRACE_LIMIT,
);

Error.prepareStackTrace = function stackTraceRewriter(err, trace) {
let newTrace = [];
Expand Down Expand Up @@ -160,10 +162,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