Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/loud-pumas-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"hardhat": patch
---

Fixed `--coverage` runs failing with `RangeError: Invalid string length` when coverage data exceeds Node's `JSON.stringify` string-length ceiling.
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import { formatTable } from "@nomicfoundation/hardhat-utils/format";
import {
ensureDir,
getAllFilesMatching,
readJsonFile,
readJsonFileAsStream,
readUtf8File,
remove,
writeJsonFile,
writeJsonFileAsStream,
writeUtf8File,
} from "@nomicfoundation/hardhat-utils/fs";

Expand Down Expand Up @@ -130,7 +130,9 @@ export class CoverageManagerImplementation implements CoverageManager {
const dataPath = await this.#getDataPath(id);
const filePath = path.join(dataPath, `${crypto.randomUUID()}.json`);
const data = Object.fromEntries(this.data);
await writeJsonFile(filePath, data);
// NOTE: We use writeJsonFileAsStream here because the coverage data for large runs
// can exceed the maximum string length when calling JSON.stringify.
await writeJsonFileAsStream(filePath, data);
log("Saved data", id, filePath);
}

Expand Down Expand Up @@ -177,7 +179,10 @@ export class CoverageManagerImplementation implements CoverageManager {
const filePaths = await getAllFilesMatching(dataPath);

for (const filePath of filePaths) {
const entries = await readJsonFile<Record<string, number>>(filePath);
// NOTE: We use readJsonFileAsStream here because the coverage data for large runs
// can exceed the maximum string length when calling JSON.parse.
const entries =
await readJsonFileAsStream<Record<string, number>>(filePath);

for (const [tag, count] of Object.entries(entries)) {
this.data.set(tag, (this.data.get(tag) ?? 0) + count);
Expand Down