Skip to content

fix(ls): hold a bare listing to what ls prints, not to the long listing rtk asks for - #4833

Merged
aeppling merged 3 commits into
developfrom
fix/ls-bare-listing-never-worse
Oct 30, 2026
Merged

aeppling merged 3 commits into
developfrom
fix/ls-bare-listing-never-worse

Conversation

@aeppling

@aeppling aeppling commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • rtk ls without -l printed a size after every file (kubectl 57.9M), which plain ls never prints: bare rtk ls cost more than ls itself (341 → 531 bytes in this repo's root).
  • The never-worse guard could not catch it: the runner compares against the child's stdout, and the child is the ls -l rtk injects (2067 bytes), not the ls the user typed (341 bytes).
  • Every listing, with or without -l, is now held through the shared never_worse to the output of ls run with the user's own arguments. A listing without -l prints names only. The same baseline replaces the two places the filter printed the long listing verbatim: an unparsable listing (-T/-D dates, -@/-e attribute lines) and a failed run (ls src missing printed 742 bytes of long listing for the 108 bytes ls printed).
  • When ls rejects the command line outright (non-zero exit, nothing on stdout, e.g. GNU --all on macOS) while rtk's own listing succeeds, there is no listing to hold to: rtk lists as master does, instead of printing nothing with exit 0.

Cost: one extra ls process per call. rtk ls -la 8.9 → 12.3 ms mean (+2.4 ms CPU) on macOS with hyperfine; bare rtk ls +≈2.6 ms CPU.

Before / after (bytes, rtk repo root, macOS)

command ls master this PR
ls 341 531 341
ls -a 466 634 466
ls src 103 137 103
ls -t 341 531 341
ls -F 354 494 354
ls src missing (exit 1) 108 742 108
ls src -l (BSD reads -l as a file) 108 197 108
ls --all src (BSD rejects --all, exit 1) 0 137 111
ls -l 2067 691 691
ls -la 2764 844 844
ls -lR 153194 758 758

Large directories still compact (cap + noise dirs + recall hint) whenever that is cheaper than ls.

Test plan

  • cargo fmt --all && cargo clippy --all-targets && cargo test --all --no-fail-fast: 56 test binaries, 5266 passed, 0 failed; proxy fmt clean
  • Unit tests on render: falls back to native output when / markers or the hint cost more, keeps the compact form when it saves, exhaustive dir/file/noise combinations never exceed native, unparsable dates and failed listings held to native, long listings held to native and compact when cheaper
  • tests/ls_never_worse_test.rs: native ls vs rtk ls in temp directories, byte-identical where compacting costs more, never larger across -a -A -1 -F -p -d -R -l -la -lh -ltr, multi-operand, empty dir, failed runs keep exit code, stderr and native stdout, unreadable directory prints what ls printed, flags after an operand held to what ls listed, options ls rejects never become an empty success
  • help_passthrough_fuzz_test: the ls case of a_tools_own_dash_h_is_still_filtered now uses -l -h (a two-file bare listing is correctly passed through as native output now; the -h token is still exercised)
  • Differential fuzz on macOS BSD ls: 76,945 cases (every BSD short flag, -D with a value, --color forms and every GNU long option, all flag pairs in separate/clustered/reversed form, 40k seeded random 2–6 flag clusters with operand mixes, flags moved after operands) over six fixtures (empty, home-like, repo with noise dirs, >50 entries, odd names incl. leading dash, trailing dots, ->, tab/newline, symlinks, FIFO, setuid, unreadable dir, empty multi-operand). Each case ran native ls, master and this PR, with every recall hint expanded before judging information loss.
oracle fixed by this PR unchanged from master regressions
output tokens > ls 30,306 554 0
output bytes > ls 30,212 2,797 0
entry name lost 3,896 2,128 0
word lost (no -l) 6,849 5,772 0
exit code ≠ ls 0 3,350 0
exit 0 with no output where ls failed 0 151 0
crash 0 0 0

All 554 cases still larger than ls are command lines BSD ls rejects (unrecognized option for a GNU long option: exit 1, nothing on stdout), which rtk lists as master does. Wherever ls printed a listing, rtk ls was never larger. The other remaining cases are unchanged from master and outside this fix: exit codes for flags after an operand and GNU options on BSD, -i/-s columns dropped when the compact form wins, names ending in . (#4305), symlink-to-directory operands (#3649), and empty directories listed with a flag after the operand.

Not run: GNU ls on Linux (Docker unavailable locally); CI covers the integration tests there.

Related: #561, #4026, #4051, #3075, #4041.

…ng rtk asks for

rtk ls asks its child for a long listing to learn each entry's type and size,
and printed that size after every file even when the user never passed -l.
Without -l, ls prints bare names, so the size column was pure addition: in
rtk's own repository root `ls` prints 341 bytes and `rtk ls` printed 531. The
never-worse guard could not catch it. The runner compares the filtered output
with the child's stdout, and that stdout is the long listing rtk asked for
(2067 bytes), not the listing the user did.

A listing without -l now prints names only, and is held to the output of `ls`
run with the user's own arguments: the compact form is shown only when the
shared never_worse finds it cheaper than that output. The same baseline
governs the two places the filter used to print the long listing verbatim: a
listing it cannot parse (-T and -D dates, the attribute lines of -@ and -e),
and a failed run, where `ls src missing` printed 742 bytes of long listing for
the 108 bytes ls printed. A long listing keeps its existing filter and baseline.

The baseline costs one more `ls` process for a listing without -l.
@rtk-wshm-sync-bot rtk-wshm-sync-bot Bot added bug Something isn't working filter-quality Filter produces incorrect/truncated signal ls labels Oct 28, 2026
@rtk-wshm-sync-bot

Copy link
Copy Markdown

wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes rtk's ls filter so a bare listing (no -l) prints names only and is held via never_worse to the output of plain ls with the user's args, rather than to the long listing rtk internally requests. This also fixes the never-worse guard for unparsable listings and failed runs, which previously fell back to the verbose long-listing output instead of native ls output.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Analyzed automatically by wshm · This is an automated analysis, not a human review.

@CLAassistant

CLAassistant commented Oct 28, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

…ejected command into an empty success

A listing with -l is now held, like a bare one, to the output of ls run
with the user's own arguments. When ls rejects the command line outright
(non-zero exit, nothing on stdout) while rtk's listing succeeds, there is
no listing to hold to, so rtk lists as before instead of printing nothing
with exit 0.
@pszymkowiak

Copy link
Copy Markdown
Collaborator

@aeppling thanks for the thorough write-up and the differential fuzzing. The direction is right: a bare rtk ls should never cost more than ls. I rebuilt ed8cb5d4 and the suite passes; a few points before merge:

  1. Locale in the baseline. run() forces LC_ALL=C on the rtk listing, but native_listing() runs ls without it. The baseline therefore uses the user's locale, and that output is returned verbatim on the fallback paths. The byte count is the same either way, so the decision is unaffected, but the two sides are not the same command. Setting LC_ALL=C on native_listing too would make them consistent. Minor.

  2. help_passthrough_fuzz_test input changed to -l -h. The test is named a_tools_own_dash_h_is_still_filtered, and it no longer exercises -h on a bare listing. On a two-file directory the output is identical to ls -h, so this is about what the test guarantees rather than a visible regression. Could you keep the original case on a directory where compaction wins, and add a separate test for the passthrough behaviour?

  3. Cost of the extra ls on -l. The latest commit runs native_listing for long listings too. The +2.6 ms in the description was measured for bare listings. Could you measure the -l overhead on a release build?

Once these are settled I am happy to re-review.

…hrough on its own

a_tools_own_dash_h_is_still_filtered runs `ls -h` again, on a directory of
60 files where the compact form wins, so it still proves rtk filters a
listing that carries -h. The two-file directory, where rtk now prints
exactly what `ls -h` prints, gets its own test.
@aeppling

Copy link
Copy Markdown
Contributor Author

@pszymkowiak thanks for the review.

Not accurate: locale (1). The byte count does change with the locale: ls -b with café.txt prints 43 B in en_US.UTF-8 and 49 B in C (caf\303\251.txt). With LC_ALL=C on the baseline, rtk ls -b would print those 49 B, more than the user's ls, so I'm keeping their locale.

Fixed: help_passthrough_fuzz_test (2). In db26b75, bare ls -h is back, on a 60-file directory where the compact form wins, and the two-file case has its own test.

Measured: -l cost (3). Release builds, master → this PR: ls -l 9.6 → 12.8 ms, ls -la 8.9 → 11.6 ms, ls -lR 45.5 → 81.2 ms. It's one more ls, which the never-worse guard needs, so I consider it justified.

@pszymkowiak pszymkowiak left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aeppling you're right on the locale, I checked: ls -b with café.txt is 16 B in UTF-8 vs 22 B in C, and rtk ls -b returns the 16. Keep it as is. The -h test split looks good, and the ls unit tests, ls_never_worse_test and help_passthrough_fuzz_test all pass on db26b75.

On the -l cost, I measured on release builds: ls -lR src 9 → 12.4 ms, ls -la 6.6 → 8.4 ms. Not blocking, but spawning the baseline ls before the filtered one and waiting on it afterwards would run both in parallel and bring it back to the slower of the two.

Approving. The only thing left before merge is the CLA: the commits use [email protected], which isn't linked to your GitHub account.

@aeppling
aeppling merged commit f4d289a into develop Oct 30, 2026
20 of 21 checks passed
@rtk-release-bot rtk-release-bot Bot mentioned this pull request Oct 30, 2026
@aeppling
aeppling deleted the fix/ls-bare-listing-never-worse branch November 18, 2026 17:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working filter-quality Filter produces incorrect/truncated signal ls

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants