Skip to content

[Bug]: rewrite-stack-trace logic break stack trace for Jest #14927

@smcenlly

Description

@smcenlly

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

Steps for reproducing are described here (jestjs/jest#13248); confirmed by jest team as upstream (babel) issue. More detail on confirming the problem described below.

In rewrite-stack-trace.ts, the Error.stackTraceLimit is being adjusted in a singleton function. This runs just once within a process.

What's happening is as follows:

  1. Jest starts and sets Error.stackTraceLimit to 100.
  2. At some point the babel rewrite code runs and sets Error.stackTraceLimit to Error.stackTraceLimit += STACK_TRACE_LIMIT_DELTA; (const STACK_TRACE_LIMIT_DELTA = 100;).
  3. When errors are reported on first run, they are truncated to newTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA), (see here).
  4. When a process is re-used, jest re-initializes the Error.stackTraceLimit to 100.
  5. Because rewrite-stack-trace.ts operates as a singleton, it is not expecting Error.stackTraceLimit to have been reset, and then completely truncates subsequent stack traces because now Error.stackTraceLimit is 100, and STACK_TRACE_LIMIT_DELTA is 100, and the processing code newTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA) becomes newTrace.slice(0, 100 - 100).

You can confirm this behavior by changing setupPrepareStackTrace method in distributed @babel/core/lib/errors/rewrite-stack-trace.js:

...
function setupPrepareStackTrace() {
+   require('fs').appendFileSync('path_to_log_file.txt', (Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA).toString() + '\n');
    return prepareStackTrace(err, newTrace.slice(0, Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA));
...
}
...

Sample output when running the sample repo provided in the jest issue, for Error.stackTraceLimit - STACK_TRACE_LIMIT_DELTA:

100
100
100
100
0

After this point, the error stack is always completely truncated.

Configuration file name

No response

Configuration

Default Jest Babel Configuration (not applicable to the issue)

Current and expected behavior

Babel should be tolerant of other tooling setting Error.stackTraceLimit.

Environment

@babel/core@7.19.0

Possible solution

No response

Additional context

No response

Metadata

Metadata

Labels

outdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: core

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions