Use fputs for stderr writes in Logger - #416
Conversation
FileHandle.standardError.write is unbuffered while print's stdout is block-buffered when piped, so under `2>&1` (any CI log) warnings and errors jumped ahead of "Report successfully created" instead of interleaving where they actually occurred. It also raises an uncatchable Objective-C exception when the descriptor is closed (e.g. `2>&-`), crashing where the tool previously failed silently. Switch to fputs(message + "\n", stderr), flushing stdout first to keep interleaving accurate. Fixes #390 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Warning Review limit reached
Next review available in: 18 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Comment |
What was wrong
3.0 moved
Logger.errorandLogger.warningto stderr viaFileHandle.standardError.write. That introduced two problems:printwrites to a block-buffered stdout when output is piped;FileHandle.writeis unbuffered. Under2>&1(i.e. any CI log), warnings/errors jumped ahead of the "Report successfully created" line instead of interleaving where they actually occurred.FileHandle.writeraises an uncatchable Objective-C exception, soxchtmlreport … 2>&-crashed where it previously failed silently.Fixes #390.
What changed
Only
Sources/XCTestHTMLReportCore/Classes/Helpers/Logger.swift:printToStandardErrornow flushes stdout and usesfputs(message + "\n", stderr)instead ofFileHandle.standardError.write.errorandwarningstill go to stderr;success,step, andsubstepare untouched and remain on stdout (verified by the CLI test suite, which parsesLogger.successoutput from stdout).private static func printToStandardError(_ message: String) { - FileHandle.standardError.write(Data((message + "\n").utf8)) + // `FileHandle.write` is unbuffered while `print`'s stdout is block-buffered + // when piped, so under `2>&1` these lines would jump ahead of stdout output + // instead of interleaving where they occurred. It also raises an uncatchable + // Objective-C exception when the descriptor is closed (e.g. `2>&-`). `fputs` + // avoids both problems. Flushing stdout first keeps interleaving accurate. + fflush(stdout) + fputs(message + "\n", stderr) }Verification
All commands run from a clean checkout of this branch, fixtures generated via
./prepareTestResults.sh.1.
swift build— clean2.
swift test— 22 tests, 1 skipped, 0 failures(
CoreTests.testRetryFunctionalityJunitis skipped pending #378, as expected.)3. Ordering fix, using a degraded bundle (
mkdir -p /tmp/Broken.xcresult) piped with> out.log 2>&1:Before this fix (stashed the change, rebuilt), the success line was pushed to the very end, after all the warnings that were chronologically emitted later:
After this fix, "Report successfully created" appears in its correct chronological position — before the later degradation warnings that are emitted after the report is written:
4. Crash fix, running with stderr closed (
xchtmlreport -o /tmp/broken-out /tmp/Broken.xcresult 2>&-):Before this fix:
After this fix:
No crash; exits cleanly with code 3 (faults were collected, as expected for a broken bundle).
5.
Logger.successstill on stdout (xchtmlreport -o /tmp/broken-out /tmp/Broken.xcresult 2>/dev/null):Concerns
None. The change is scoped to the single private helper method in
Logger.swift.🤖 Generated with Claude Code