Skip to content

fix(core): emit one trailing newline, not one on top of the output's own - #4154

Merged
TaKO8Ki merged 2 commits into
rtk-ai:developfrom
KuSh:fix/emit-guarded-trailing-newline
Oct 9, 2026
Merged

TaKO8Ki merged 2 commits into
rtk-ai:developfrom
KuSh:fix/emit-guarded-trailing-newline

Conversation

@KuSh

@KuSh KuSh commented Sep 20, 2026 •

Copy link
Copy Markdown
Collaborator

closes #4471

Stacked on #4471, whose commit comes first here: the stub tests below need its isolation, since a SHELLOPTS exported in a contributor's shell would otherwise reach the stubs.

The bug

emit_guarded in src/core/runner.rs ended with println!("{}", shown), which appends a newline unconditionally. When shown already ends in one — notably every time never_worse falls back to raw, since a command's own output is newline-terminated — rtk emits one byte more than the command it stands in front of.

The guard cannot see it. never_worse compares token estimates of raw against the candidate before anything is printed, and the extra byte is added by the printer afterwards; a one-byte difference rarely moves a bytes/4 estimate anyway.

Measured with a stub go on PATH and a filter summary longer than the command's output, so the guard falls back:

stub stdout native bytes rtk before rtk after
ok\n 3 4 3
ok ex\n 7 8 7
PASS\n 5 6 5

Not tee-specific and not go-specific: it is every path through this shared printer whose body already ends with a newline, the plain (no tee) branch of run_captured_filter included.

The change

  • Supply the terminator only where it is missing. Most filters build their body without one and rely on the printer; those still get exactly one.
  • An empty body prints nothing at all, instead of a blank line.
  • A recovery hint goes on the line after the filtered text, reusing that text's terminator rather than adding a blank line before it. The join is one function, runner::with_hint, which find's guard baseline and vitest's hinted report use too.
  • Weigh both forms terminated before the guard chooses. The printer's newline is paid by whichever side wins, so charging it to one side only decided close calls on a byte that is not the difference between them.
  • emit_guarded returns the bytes it wrote, so timer.track(..., &shown) books what reached the user.
  • The plain print branch of run_captured_filter carried the same println!; it now shares the printer and still waives the guard for the clean outputs it is given (ruff's injected [] still prints Ruff: No issues found). The no_trailing_newline opt-out is untouched — those filters own their line endings.

One byte the fix does not remove: a command whose own output is unterminated still gets the single newline a printed line needs.

Call sites checked

Every caller of emit_guarded / print_with_hint, plus the branches inside run_captured_filter: main.rs (TOML filter path, ×2), git_cmd.rs (git stash show), aws_cmd.rs (×3), curl_cmd.rs, vitest_cmd.rs, lint_cmd.rs, playwright_cmd.rs, gt_cmd.rs, uv_cmd.rs, find_cmd.rs (×2). All of them track the bytes returned except uv_cmd.rs, which discarded them and tracked its filtered text; it now tracks what it printed. forwarded_stderr takes shown only through trim().is_empty(), unaffected.

Not in this PR

Restating never_worse's guarantee in bytes rather than token estimates. The unit was not what let this through — the byte was added after the guard ran — and the guard is called directly from roughly sixty sites outside this printer, where switching units would change which outputs get filtered. That is a compression change, not a byte-accounting fix.

Tests

src/core/runner.rs unit-tests the printed form: a terminated fallback costs exactly the command's bytes, an unterminated body gains its one newline, a hint is terminated once and joins without a blank line, an empty body with a hint prints the hint alone, with_hint in each case, both sides of the guard weighed terminated, ruff's injected clean output shows its summary terminated, and an empty body stays empty. vitest_cmd.rs pins its hinted report's join.

tests/stderr_only_failure_test.rs runs the binary end to end behind stubs, through common::rtk_command(), which with #4471 starts it from an empty environment with its data in a scratch directory: a go vet guard fallback emits the command's bytes exactly, a golangci report ends with exactly one newline, ruff check on clean output prints exactly Ruff: No issues found\n, and a ruff format fallback on the plain branch emits the command's bytes exactly.

cargo fmt --all -- --check, cargo clippy --all-targets -- -D warnings and cargo test --all are clean; CI is green.

🤖 Generated with Claude Code

KuSh and others added 2 commits October 7, 2026 05:30
isolate_rtk, renamed from redirect_rtk_data next to isolate_git, clears the
environment and keeps only what KEPT names: PATH, what the loader needs to start
a tool installed outside the system directories, TMPDIR, the locale and the
time zone with the data they load, and what rustup needs to find cargo, plus the
system variables WINDOWS_KEPT names on Windows, where the profile variables are
pinned into the scratch directory alongside HOME.

Removing variables by name let everything else through. A test's fake tools are
shell scripts, so SHELLOPTS=xtrace exported in a contributor's shell made
silent_command_stays_silent fail on any host where /bin/sh is bash; BASHOPTS,
BASH_ENV, POSIXLY_CORRECT, CDPATH and exported functions change them the same
way.

A native tool or git a test compares rtk's output with gets the same
environment, through common::native_command and isolate_git, so a setting only
the contributor's shell exports, such as RIPGREP_CONFIG_PATH or COLUMNS,
reaches neither side. Tests drop the
removals and pins the empty environment makes redundant, and the hook protocol
test isolates the bash hook that runs rtk the same way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`emit_guarded` terminated every body with `println!`, so a body that already
ended in a newline left rtk one byte longer than the command it stands in front
of. That is every run where `never_worse` falls back to the command's own
output, which is newline-terminated in the ordinary case. The guard cannot
catch it: it compares token estimates before the printer runs, and one byte
rarely moves a `bytes/4` estimate.

Supply the terminator only where it is missing, and print nothing for an empty
body: a filter with nothing to report should not cost a blank line. A recovery
hint goes on the line after the filtered text, reusing that text's terminator
rather than adding a blank line before the hint; `find` builds its guard
baseline and `vitest` its hinted report with the same join. The guard now
weighs both forms terminated, so the byte the printer adds no longer decides a
close call against whichever side happened to be charged it.

`emit_guarded` returns the bytes it wrote, so what its callers track is what
the user received; the terminator used to go uncounted. `uv run` discarded
those bytes and tracked its filtered text; it now tracks what it printed.

The plain print branch of `run_captured_filter` carried the same `println!` and
now shares `emit_guarded`'s printer, still waiving the guard for the clean
outputs it is given (ruff's injected `[]`). The `no_trailing_newline` opt-out is
untouched: its filters own their line endings.

Measured with a stub `go` on PATH and a summary longer than the command's
output, so the guard falls back:

    stub stdout   native   rtk before   rtk after
    ok\n               3            4           3
    ok  ex\n           7            8           7
    PASS\n             5            6           5

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@TaKO8Ki
TaKO8Ki merged commit be8d546 into rtk-ai:develop Oct 9, 2026
13 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 9, 2026
@KuSh
KuSh deleted the fix/emit-guarded-trailing-newline branch October 9, 2026 17:53
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