Skip to content

fix(coverage): stream coverage data json to disk#8292

Closed
gultekinmakif wants to merge 4 commits into
NomicFoundation:mainfrom
gultekinmakif:fix/json-stringify-error-coverage
Closed

fix(coverage): stream coverage data json to disk#8292
gultekinmakif wants to merge 4 commits into
NomicFoundation:mainfrom
gultekinmakif:fix/json-stringify-error-coverage

Conversation

@gultekinmakif

@gultekinmakif gultekinmakif commented May 13, 2026

Copy link
Copy Markdown
Contributor
  • Because this PR includes a bug fix, relevant tests have been included.

Summary

This PR addresses a bug:

Large --coverage runs fail with RangeError: Invalid string length when the serialized coverage map exceeds Node's JSON.stringify / JSON.parse string-length ceiling.

Fix

Both saveData and loadData now use writeJsonFileAsStream / readJsonFileAsStream instead of their non-streaming counterparts. The rationale comment mirrors build-system.ts:1035.

Tests

Introducing any regression tests would not make any sense, so I tested the behaviour change locally.

I ran a small Node script that builds a Record<string, number> of 5,000,000 coverage-tag-shaped entries (~520 MB serialized, same order of magnitude as the failure on the linked issue).

poc

The script confirms that this PR is able to stream large files, while the main branch fails.

Changeset

patch on hardhat.

Notes

For large --coverage runs, the serialized coverage data exceeds Node's JSON.stringify string-length ceiling and saveData throws RangeError: Invalid string length.
Switching to writeJsonFileAsStream avoids the intermediate string.

Fixes NomicFoundation#8071.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@changeset-bot

changeset-bot Bot commented May 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b9cbaab

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
hardhat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@gultekinmakif gultekinmakif changed the title Fix/json stringify error coverage fix(coverage): stream coverage data json to disk May 13, 2026
@alcuadrado

Copy link
Copy Markdown
Member

Can you provide a reproduction of the bug with the latest version first? We already did some things related to this recently, so the bug may not be present.

@alcuadrado

Copy link
Copy Markdown
Member

I ran a small Node script that builds a Record<string, number> of 5,000,000 coverage-tag-shaped entries (~520 MB serialized, same order of magnitude as the failure on the linked issue).

This would be equivalent to having 5M solidity statements in a single project

@gultekinmakif

Copy link
Copy Markdown
Contributor Author

@alcuadrado I've spun up a small script to replicate the issue across 3.1.6, 3.1.12, and latest versions: hh-8071-poc.

POC concludes that #8213 already addressed both issues that resurface.
I think #8071 can also be marked as fixed.
Closing this PR.

@alcuadrado

Copy link
Copy Markdown
Member

Thanks a lot for the follow up investigation, @gultekinmakif! Appreciate it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coverage fails to write huge json when tests completed

3 participants