From d3351a95d674a5bc6a474f0b920c8181af957c6d Mon Sep 17 00:00:00 2001 From: Martin Ruiz Date: Thu, 30 Jul 2026 14:52:13 -0700 Subject: [PATCH] fix: keep dry-run output valid json Suppress human-readable reify diffs when JSON output is requested so stdout remains parseable. Fixes #8565 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 48e629f8-eb5a-467c-970b-feb313628c00 --- lib/utils/reify-output.js | 3 ++- test/lib/utils/reify-output.js | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/utils/reify-output.js b/lib/utils/reify-output.js index e50d4ac72d967..73d496bfb94d1 100644 --- a/lib/utils/reify-output.js +++ b/lib/utils/reify-output.js @@ -44,7 +44,8 @@ const reifyOutput = (npm, arb, extras = {}) => { } if (diff) { - const showDiff = npm.config.get('dry-run') || npm.config.get('long') + const showDiff = !npm.flatOptions.json && + (npm.config.get('dry-run') || npm.config.get('long')) const chalk = npm.chalk depth({ diff --git a/test/lib/utils/reify-output.js b/test/lib/utils/reify-output.js index 0678e9cabeb28..864edc85a441b 100644 --- a/test/lib/utils/reify-output.js +++ b/test/lib/utils/reify-output.js @@ -440,6 +440,53 @@ t.test('prints dedupe difference on dry-run', async t => { t.matchSnapshot(out, 'diff table') }) +t.test('prints only json for dry-run and long', async t => { + for (const flag of ['dry-run', 'long']) { + await t.test(flag, async t => { + const out = await mockReify(t, { + actualTree: { + inventory: { + has: () => true, + }, + children: [], + }, + diff: { + children: [ + { + action: 'ADD', + ideal: { + path: 'test/foo', + name: 'foo', + package: { version: '1.0.0' }, + }, + }, + ], + }, + }, { + [flag]: true, + json: true, + }) + + t.strictSame(JSON.parse(out), { + add: [ + { + name: 'foo', + version: '1.0.0', + path: 'test/foo', + }, + ], + added: 1, + audited: 0, + change: [], + changed: 0, + funding: 0, + remove: [], + removed: 0, + }) + }) + } +}) + t.test('prints dedupe difference on long', async t => { const mock = { actualTree: {