Fix stdout buffering to enable streaming output#499
Open
prateek wants to merge 2 commits intocpisciotta:mainfrom
Open
Fix stdout buffering to enable streaming output#499prateek wants to merge 2 commits intocpisciotta:mainfrom
prateek wants to merge 2 commits intocpisciotta:mainfrom
Conversation
Disable stdout buffering with setvbuf so xcbeautify streams output immediately through pipes, and add a CLI test that verifies line-by-line flushing behavior.
e11cdc4 to
f536e13
Compare
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #481 by ensuring xcbeautify streams output immediately when its stdout is piped, rather than being delayed by C stdio full-buffering.
Changes:
- Disable stdout buffering at CLI startup via
setvbuf(stdout, nil, _IONBF, 0). - Add a FIFO-based streaming regression test (
tools/test-streaming) and run it from the existing CLI test harness. - Add environment/setup notes to
AGENTS.md(Cursor Cloud-specific).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
Sources/xcbeautify/Xcbeautify.swift |
Disables stdout buffering at process start (and adjusts imports for Swift 6 concurrency). |
tools/test-streaming |
Adds an integration test to validate per-line flushing through pipes using FIFOs + timeouts. |
tools/cli-tests |
Integrates the streaming test into the CLI test suite. |
AGENTS.md |
Adds Cursor Cloud environment/tooling notes and CLI testing guidance. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| var junitReportFilename = "junit.xml" | ||
|
|
||
| func run() throws { | ||
| setvbuf(stdout, nil, _IONBF, 0) |
Comment on lines
+25
to
+33
| # Start xcbeautify: reads from IN_FIFO, writes to OUT_FIFO. | ||
| "$BIN" --disable-logging --preserve-unbeautified < "$IN_FIFO" > "$OUT_FIFO" & | ||
| XCB_PID=$! | ||
|
|
||
| # Open the write side of IN_FIFO (keeps it open across iterations). | ||
| exec 3>"$IN_FIFO" | ||
|
|
||
| # Open the read side of OUT_FIFO. | ||
| exec 4<"$OUT_FIFO" |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Signed-off-by: Prateek Rungta <prateek@users.noreply.github.com>
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.
Fixes #481 (
Request: streaming output).Problem
When
xcbeautifyis piped to another process (the typical usage:xcodebuild ... | xcbeautify | ...), the C runtime defaults to full buffering for stdout. Since the CLI usedprint()for all output, lines accumulated in a ~4 KB buffer before being flushed to the downstream consumer. This meant CI agents, log watchers, or other tools couldn't see results as they were produced; they had to wait for the buffer to fill or for the process to exit.Solution
Add
setvbuf(stdout, nil, _IONBF, 0)at process start to disable C stdio buffering on stdout. This is a single-line fix that makes all existingprint()calls flush immediately, with no other code changes needed.Uses
@preconcurrency import Foundationto satisfy Swift 6 strict concurrency for the globalstdoutsymbol.Why
setvbufover the alternativessetvbuf(stdout, nil, _IONBF, 0)(chosen)print(), single I/O layer, no error handling neededwrite(STDOUT_FILENO, ...)fflush(stdout)after eachprintChanges
Sources/xcbeautify/Xcbeautify.swift: addsetvbuf(stdout, nil, _IONBF, 0)at the top ofrun()and use@preconcurrency import Foundationtools/test-streaming: add a CLI streaming test using FIFO-to-FIFO reads withread -tto verify each line flushes immediatelytools/cli-tests: integrate the streaming test into the existing CLI suiteTesting
swift testswift build --configuration release -Xswiftc -warnings-as-errors./tools/lint./tools/cli-tests