Skip to content

Commit

Permalink
Avoid fancy stack traces size computation
Browse files Browse the repository at this point in the history
This will end up with the actual number of frames not exactly matching the user-set Error.stackTraceLimit. However, Error.stackTraceLimit is usually set to generic numbers (10, 100, 1000, Infinity), and it does not matter if it's not _really_ X but a bit less or more.

(How long before we get a bug report "Hey, I use Error.stackTraceLimit=1562 but it's only giving me 1543 frames!"?)
  • Loading branch information
nicolo-ribaudo committed Sep 14, 2022
1 parent 091ac3d commit c1d94e5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
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(
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

0 comments on commit c1d94e5

Please sign in to comment.