Skip to content

fix(core): parse stdout in json() by default and allow source selection - #1511

Open
shubhransh-gupta wants to merge 2 commits into
google:mainfrom
shubhransh-gupta:fix/json-parse-stdout
Open

fix(core): parse stdout in json() by default and allow source selection#1511
shubhransh-gupta wants to merge 2 commits into
google:mainfrom
shubhransh-gupta:fix/json-parse-stdout

Conversation

@shubhransh-gupta

Copy link
Copy Markdown

Fixes #1505

Problem

ProcessOutput.json() previously parsed this.stdall. When CLI commands write structured JSON to stdout while logging diagnostic/progress messages to stderr (or when tools output logs to stderr), stdall contains interleaved text from both streams, causing SyntaxError: Unexpected token ... in JSON at position ....

Solution

  • By default, ProcessOutput.json() now inspects this.stdout. If this.stdout.trim() is non-empty, it parses this.stdout.
  • For backward compatibility (such as synthetic ProcessOutput objects initialized with empty stdout), it falls back to this.stdall.
  • Added an optional source?: 'stdout' | 'stderr' | 'stdall' | { source?: 'stdout' | 'stderr' | 'stdall' } parameter to both ProcessPromise.json() and ProcessOutput.json(), allowing users to explicitly select which output stream to parse if desired.
  • Added comprehensive unit tests in test/core.test.js and updated documentation in docs/process-promise.md.
import {$} from 'zx'

// When CLI prints logs to stderr and JSON to stdout:
const data = await $`node -e "console.error('downloading...'); console.log(JSON.stringify({ status: 'ok' }))"`.json()
console.log(data.status) // => 'ok'

// Explicit stream selection:
const stderrData = await $`...`.json('stderr')
  • Setup Set the latest Node.js LTS version.
  • Build: I’ve run npm build before committing and verified the bundle updates correctly.
  • Tests: I’ve run test and confirmed all tests succeed. Added tests to cover my changes if needed.
  • Docs: I’ve added or updated relevant documentation as needed.
  • Sign Commits follow conventional commits spec.
  • CoC: My changes follow the project’s coding guidelines and Code of Conduct.
  • Review: This PR represents original work and is not solely generated by AI tools.

@google-cla

google-cla Bot commented Sep 4, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ProcessOutput.json() parses combined stdout+stderr, breaking on CLIs that log progress to stderr

1 participant