Repository navigation
fix(cli): flush stdout before exiting - #93
Merged
Merged
Conversation
process.exit() drops whatever stdout has not written yet. Writes to a pipe are asynchronous on macOS, so a report longer than the pipe buffer piped into a reader that was not ready yet (wp-tooling list --json | jq, iapi-check --output json | jq) arrived cut off at exactly 65,536 bytes, as invalid JSON. The bin now sets the exit code, writes an empty chunk and exits from its callback, after everything queued before it has gone out. A closed pipe (EPIPE) still exits straight away. A test pipes list --json (about 70 KiB) into a reader that starts a second late and checks it receives every byte; it fails without the fix on macOS. Linux writes to pipes synchronously, so there it passes either way.
There was a problem hiding this comment.
🟡 Changes recommended
The regression-test comment needs correction because asynchronous stdout writes can also truncate piped output on Linux, not just macOS.
1 open issue
What changed in this PR
This PR prevents truncated piped output from the wp-tooling CLI, supporting commands that produce large JSON reports.
Changes:
- Waits for queued stdout writes before exiting, preserving command exit codes and handling closed pipes.
- Adds regression tests for delayed pipe readers and usage-error exit codes.
- Documents the fix in the changelog.
| File | Description |
|---|---|
node-packages/wp-tooling/tests/cli/bin.test.js |
Tests complete piped output and usage-error exit status. |
node-packages/wp-tooling/CHANGELOG.md |
Records the stdout truncation fix. |
node-packages/wp-tooling/bin/wp-tooling.js |
Defers exit until stdout finishes writing or reports an error. |
Review effort: Balanced
Adi-ty
requested changes
Nov 9, 2026
Move the exit into src/cli/exit.js and wait for both streams. stderr was cut at 64 KiB like stdout, and an EPIPE on it (2>&1 | head) turned a successful run into exit code 1. Pipes are asynchronous on Linux too, so correct the test and changelog notes that said macOS only.
# Conflicts: # node-packages/wp-tooling/CHANGELOG.md
Adi-ty
approved these changes
Nov 9, 2026
github-actions Bot
pushed a commit
that referenced
this pull request
Nov 9, 2026
* fix(cli): flush stdout before exiting process.exit() drops whatever stdout has not written yet. Writes to a pipe are asynchronous on macOS, so a report longer than the pipe buffer piped into a reader that was not ready yet (wp-tooling list --json | jq, iapi-check --output json | jq) arrived cut off at exactly 65,536 bytes, as invalid JSON. The bin now sets the exit code, writes an empty chunk and exits from its callback, after everything queued before it has gone out. A closed pipe (EPIPE) still exits straight away. A test pipes list --json (about 70 KiB) into a reader that starts a second late and checks it receives every byte; it fails without the fix on macOS. Linux writes to pipes synchronously, so there it passes either way. * fix(cli): flush stderr as well as stdout before exiting Move the exit into src/cli/exit.js and wait for both streams. stderr was cut at 64 KiB like stdout, and an EPIPE on it (2>&1 | head) turned a successful run into exit code 1. Pipes are asynchronous on Linux too, so correct the test and changelog notes that said macOS only.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

What this PR does
Stops
wp-toolingfrom cutting its output short when it is piped.bin/wp-tooling.jscalledprocess.exit()as soon as a command resolved, which drops whatever stdout has not written yet. Writes to a pipe are asynchronous on macOS, sowp-tooling list --json | jq, oriapi-check --output json | jq(#91), received exactly 65,536 bytes of invalid JSON whenever the reader was not ready in time.Closes
N/A
Changes
bin/wp-tooling.jssetsprocess.exitCode, writes an empty chunk and exits from its callback, once everything queued before it has gone out. A closed pipe (EPIPE, for example| head) still exits straight away. Exit codes are unchanged.tests/cli/bin.test.jspipeslist --json(about 70 KiB) into a reader that starts a second late and checks it receives every byte, and checks a usage error still exits 2.### Fixed.How I verified
The new test fails on
main(Expected: 70949, Received: 65536) and passes here.Reviewer notes