From afb9f1b15951f85ef8fd173985e35de62321cabf Mon Sep 17 00:00:00 2001 From: tsushanth <78000697+tsushanth@users.noreply.github.com> Date: Wed, 10 Jun 2026 13:09:59 -0700 Subject: [PATCH] fix(json-reporter): surface RangeError from large merged reports MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://github.com/microsoft/playwright/issues/40983 `JSON.stringify` throws `RangeError: Invalid string length` once the merged report exceeds V8's ~512 MiB string limit. The multiplexer would catch the error as a generic "Error in reporter" warning and the merge command would exit 0, leaving the shell-redirected output file at 0 bytes — downstream `jq` then died with "Unexpected end of JSON input" with no link back to the real cause. Catch the serialization failure, write a small parseable JSON describing the failure (so the output isn't an empty file), and rethrow with an actionable message explaining the V8 limit and suggesting the blob reporter for large sharded runs. --- packages/playwright/src/reporters/json.ts | 32 ++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/playwright/src/reporters/json.ts b/packages/playwright/src/reporters/json.ts index a9e2401a1742b..2f1eef9599e13 100644 --- a/packages/playwright/src/reporters/json.ts +++ b/packages/playwright/src/reporters/json.ts @@ -239,7 +239,37 @@ class JSONReporter implements ReporterV2 { } async function outputReport(report: JSONReport, resolvedOutputFile: string | undefined) { - const reportString = JSON.stringify(report, undefined, 2); + let reportString: string; + try { + reportString = JSON.stringify(report, undefined, 2); + } catch (e: any) { + // V8 caps strings at ~2**29 - 24 chars (~512 MiB on 64-bit). A merge of + // hundreds of shards with attachments / stack traces can blow past that, + // and `JSON.stringify` throws `RangeError: Invalid string length`. The + // wrapping reporter would otherwise log it as a generic "Error in + // reporter" and leave a 0-byte file from any shell redirect — emit a + // small, parseable JSON describing the failure so downstream tooling + // (`jq`, etc.) doesn't die with "Unexpected end of JSON input", and + // rethrow with an actionable message so the warning isn't opaque. + const failurePayload = JSON.stringify({ + error: 'JSON reporter failed to serialize the merged report.', + reason: e?.message ?? String(e), + hint: 'The merged report likely exceeds V8\'s maximum string length (~512 MiB). Use the blob reporter for sharded runs or filter the dataset before re-merging.', + }, undefined, 2); + if (resolvedOutputFile) { + await fs.promises.mkdir(path.dirname(resolvedOutputFile), { recursive: true }); + await fs.promises.writeFile(resolvedOutputFile, failurePayload); + } else { + // eslint-disable-next-line no-console + console.log(failurePayload); + } + throw new Error( + `JSON reporter could not serialize the merged report: ${e?.message ?? e}. ` + + 'The report likely exceeds V8\'s maximum string length (~512 MiB on 64-bit Node). ' + + 'Use the blob reporter for sharded runs or filter the dataset before re-merging.', + { cause: e }, + ); + } if (resolvedOutputFile) { await fs.promises.mkdir(path.dirname(resolvedOutputFile), { recursive: true }); await fs.promises.writeFile(resolvedOutputFile, reportString);