Skip to content

fix(core): stdout-only filters silently swallow a tool's stderr - #3772

Merged
KuSh merged 2 commits into
rtk-ai:developfrom
KuSh:fix/stdout-only-filters-swallow-stderr
Sep 12, 2026
Merged

KuSh merged 2 commits into
rtk-ai:developfrom
KuSh:fix/stdout-only-filters-swallow-stderr

Conversation

@KuSh

@KuSh KuSh commented Aug 30, 2026

Copy link
Copy Markdown
Collaborator

Every RunOptions::stdout_only() filter drops the tool's stderr. When a tool reports on stderr and leaves stdout empty, rtk prints nothing at all — on either stream.

src/cmds/README.md documents the constructor as "stdout-only to filter, stderr passthrough, no tee", but the passthrough half was never implemented: run_captured_filter only emits stderr on the skip_filter_on_failure path, which most callers do not enable.

Impact

Around 30 call sites: ruff, pytest, rspec, rubocop, prettier, phpunit, phpstan, gh, glab, go test, golangci-lint, tree, wc, psql.

Partially addresses #2878, which documents the prettier case in detail. That issue's first defect — stderr discarded before the filter ever sees it — is fixed here, so the [warn] lines now reach the user. Its second defect (filter_prettier_output excluding [warn] lines) is prettier-specific and is not touched by this PR, so #2878 should stay open.

How it broke

Two commits compound:

  • 078ad1d moved golangci_cmd onto runner::run_filtered and deleted a block whose own comment read // Always forward stderr (config errors, missing linters, etc.).
  • 861a46d added guard::never_worse. Under stdout_only the baseline is stdout, so with stdout empty any filter output counts as "worse than nothing" and collapses to the empty string.

Together: silence on both streams.

The fix

Forward the captured stderr, and count it. The guard keeps comparing against stdout, which is the correct baseline: stderr is forwarded verbatim on both sides, so it cancels, and never-worse reduces to filtered <= stdout. Widening the baseline to the combined output would let rtk emit more than the command it replaces.

Tracking now measures against the full raw output and counts what was actually emitted — comparing stdout to stdout while printing stderr booked a passed-through stream as if the filter had removed it, overstating savings in rtk gain.

Tests

Six integration tests over a fake tool on PATH, pinning both halves of the contract: the tool's stderr must reach the user, and rtk must not invent a stdout message in its place. The prettier case covers the second — it writes its report to stderr and nothing to stdout even on a successful run, so a filter's empty-input placeholder would otherwise be printed over a run that worked.

cargo test --all: 2927 passing. fmt clean, 0 clippy warnings.

@KuSh
KuSh force-pushed the fix/stdout-only-filters-swallow-stderr branch from 25429d0 to 7b89028 Compare August 30, 2026 13:06
kylehgc added a commit to kylehgc/rtk that referenced this pull request Sep 3, 2026
The six tests adopted from upstream rtk-ai#3772 spawn rtk without RTK_DB_PATH, so
every run booked fake-tool savings into the developer's history.db. Point
them at a per-test file in the tempdir, as tests/stderr_passthrough_test.rs
already does. Also names rtk-ai#2878 as the upstream issue it is.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
kylehgc added a commit to kylehgc/rtk that referenced this pull request Sep 3, 2026
fix(core): forward stderr from stdout-only filters on every exit (adopt upstream rtk-ai#3772)
@aeppling aeppling self-assigned this Sep 11, 2026
@aeppling

Copy link
Copy Markdown
Contributor

Hey @KuSh

Looks sound, stderr should be forwarded. For most tools it's a few lines of pure signal, so passing it through uncompressed is fine for now

One case to flag: prettier writes its whole report to stderr, so rtk prettier --check . on a large repo goes from 1 token to ~26k. Not a blocker for this PR

KuSh and others added 2 commits September 12, 2026 22:59
`RunOptions::stdout_only()` is documented as "stdout-only to filter, stderr
passthrough", but the passthrough half was never implemented: stderr was captured
and dropped on every path except `skip_filter_on_failure`. A tool that reports on
stderr and leaves stdout empty therefore produced nothing at all.

golangci-lint is the visible case -- a config or build error goes to stderr, the
filter is handed an empty stdout, and the never-worse guard replaces its
"JSON parse failed" fallback with the empty string, so both streams end up
silent. The same shape applies to every other stdout-only filter: ruff, pytest,
rspec, rubocop, gh, glab, prettier, tree, wc.

Savings are also measured against running the command directly, so the stderr
that is forwarded has to be counted as emitted. Comparing stdout to stdout while
printing stderr as well booked a passed-through stream as if it had been
filtered away.

The guard keeps comparing against stdout. stderr is forwarded verbatim on both
sides, so it cancels: never-worse reduces to `filtered <= stdout`, and widening
the baseline to the combined output would let rtk emit more than the command it
replaces.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Six cases over a fake tool on PATH, covering both halves of the contract: the
tool's own stderr has to reach the user, and rtk must not invent a stdout
message in its place.

The prettier case is the one that is easy to get wrong. It writes its report to
stderr and nothing to stdout even on a successful run, so a filter's empty-input
placeholder ("Error: prettier produced no output") would be printed over a run
that worked. An empty stdout from a command that printed nothing on stdout is
the correct answer.

Also pinned: stderr is not replayed onto stdout on top of being forwarded, a
genuinely silent command stays silent, forwarding is not golangci-specific, and
exit codes propagate -- including golangci-lint's exit 1, which means issues
were found and is reported without failing the build.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@KuSh
KuSh force-pushed the fix/stdout-only-filters-swallow-stderr branch from 7b89028 to baf3a10 Compare September 12, 2026 21:02
@KuSh

KuSh commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator Author

One thing worth flagging, though not a blocker: filter_prettier_output only runs on stdout, and prettier's [warn] <file> report lives entirely on stderr. Once this lands, rtk prettier --check . will forward that report raw and unfiltered — on a large repo with many unformatted files that's roughly 1 token today going to ~26k. This is the same gap #2878 already tracks (extending the filter to handle stderr's [warn] lines); this PR correctly leaves that half for #2878, just calling out that the failure mode changes from silent to verbose in the interim.

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.

2 participants