Repository navigation
fix(core): emit one trailing newline, not one on top of the output's own - #4154
Merged
Merged
Conversation
KuSh
force-pushed
the
fix/emit-guarded-trailing-newline
branch
3 times, most recently
from
September 28, 2026 02:12
c0dac58 to
9f4473e
Compare
KuSh
force-pushed
the
fix/emit-guarded-trailing-newline
branch
from
October 7, 2026 00:04
9f4473e to
6f12f00
Compare
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]>
KuSh
force-pushed
the
fix/emit-guarded-trailing-newline
branch
from
October 7, 2026 03:32
6f12f00 to
5daca37
Compare
TaKO8Ki
approved these changes
Oct 9, 2026
Merged
2 tasks
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.
closes #4471
Stacked on #4471, whose commit comes first here: the stub tests below need its isolation, since a
SHELLOPTSexported in a contributor's shell would otherwise reach the stubs.The bug
emit_guardedinsrc/core/runner.rsended withprintln!("{}", shown), which appends a newline unconditionally. Whenshownalready ends in one — notably every timenever_worsefalls back toraw, 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_worsecompares token estimates ofrawagainst the candidate before anything is printed, and the extra byte is added by the printer afterwards; a one-byte difference rarely moves abytes/4estimate anyway.Measured with a stub
goon PATH and a filter summary longer than the command's output, so the guard falls back:ok\nok ex\nPASS\nNot 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_filterincluded.The change
runner::with_hint, whichfind's guard baseline andvitest's hinted report use too.emit_guardedreturns the bytes it wrote, sotimer.track(..., &shown)books what reached the user.run_captured_filtercarried the sameprintln!; it now shares the printer and still waives the guard for the clean outputs it is given (ruff's injected[]still printsRuff: No issues found). Theno_trailing_newlineopt-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 insiderun_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 exceptuv_cmd.rs, which discarded them and tracked its filtered text; it now tracks what it printed.forwarded_stderrtakesshownonly throughtrim().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.rsunit-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_hintin 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.rspins its hinted report's join.tests/stderr_only_failure_test.rsruns the binary end to end behind stubs, throughcommon::rtk_command(), which with #4471 starts it from an empty environment with its data in a scratch directory: ago vetguard fallback emits the command's bytes exactly, a golangci report ends with exactly one newline,ruff checkon clean output prints exactlyRuff: No issues found\n, and aruff formatfallback on the plain branch emits the command's bytes exactly.cargo fmt --all -- --check,cargo clippy --all-targets -- -D warningsandcargo test --allare clean; CI is green.🤖 Generated with Claude Code