swc: range end index out of range for slice of length...

Describe the bug

This is an intermittent bug with @swc/jest and/or @swc/core.

We are running our test suite with turbo repo, utilizing @swc/jest, and intermittently we get a failure like this:

failed to handle: range end index 7551504 out of range for slice of length 6467552
@ssi/document-landing:test: 
@ssi/document-landing:test:       at Compiler.transformSync (../../node_modules/@swc/core/index.js:241:29)
@ssi/document-landing:test:       at transformSync (../../node_modules/@swc/core/index.js:348:21)
@ssi/document-landing:test:       at Object.process (../../node_modules/@swc/jest/index.js:73:45)

Retrying the ci/cd job and it will succeed just fine.

This does not happen very often, but ~1/50 runs. That error message appears to be from rust, which is pointing me more towards it being an @swc issue, than a jest specific issue.

Input code

No response

Config

{
    "jsc": {
        "parser": {
            "syntax": "typescript",
            "tsx": true
        }
    },
    "module": {
        "type": "commonjs"
    }
}

Playground link

No response

Expected behavior

Test suites should not intermittently fail with no changes

Actual behavior

Fails intermittently

Version

1.2.128

Additional context

“@swc/cli”: “^0.1.57”, “@swc/core”: “^1.2.128”, “@swc/jest”: “^0.2.15”,

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 4
  • Comments: 37 (10 by maintainers)

Most upvoted comments

It’s not a something we can look into

I can share my findings while working on this issue last year maybe it could help:

Updating Node reduced the leaks but didn’t solve the problem completely https://github.com/jestjs/jest/issues/11956 https://issues.chromium.org/issues/42202158

Updating Jest still didn’t solve the problem entirely but reduced how often it happened.

I’m not sure if it’s related to SWC, but it definitely exacerbates the issue in Jest, I was hoping --workerIdleMemoryLimit would solve the issue but it didn’t have any effect.

As @dgreif mentioned, there’s only one option, which is runInBand, because Jest instances with memory leaks are not able to shut down and keep running forever until SWC complains.

Our solution:

Run tests in-band (sequentially) within each Jest instance. Keep parallelization at a higher level using NX, running multiple Jest instances concurrently. This approach isolates each test instance, preventing memory leaks from spreading, while still leveraging parallel execution for improved overall performance.

--runInBand is still the only solution we have found which consistently avoids this error. We use nx for build parallelization and caching in our monorepo, which allows us to get most of the benefits provided by multiple Jest workers, but we do have a few big packages which still run slowly with --runInBand and delay the overall test run from finishing. I wish I had more insight for y’all, but that’s all I’ve got 😅

@samar-1601 no, was one of the reasons that caused memory leaks, as well in our test cases there were so many tests that I had to adjust to fix the leaks, some of which had a relation with react-testing-library packages. like @testing-library/jest-dom and @testing-library/user-event

I hope that answers your question

I feel you, been in your shoes, and that’s correct with the memory leak issues you mentioned, I had to upgrade both, since then all good, and currently planning to move away from jest to vitest as well.

Has anyone got any resolution for this issue? @ezpuzz @dgreif @HashemKhalifa

@kdy1 Do we have any updates/workaround on this. I have a very large monorepo which fails the CI pipes intermittently, giving the following error: image

These are the current versions we are using

"@swc/core": "^1.3.46",
"@swc/jest": "^0.2.24",

I can relate to that. I tried using the GitHub Actions runner and everything went smoothly. However, when using our self-hosted runner in parallel mode, we encountered some issues. To mitigate the problem, I tried running it with the --runInBand option, and while the issue still occurred, it became less frequent.

Can you try RUST_BACKTRACE=1?