Skip to content

fix(ls): preserve multiple directory headers - #2865

Open
roian6 wants to merge 4 commits into
rtk-ai:developfrom
roian6:fix/ls-multiple-directory-headers
Open

roian6 wants to merge 4 commits into
rtk-ai:developfrom
roian6:fix/ls-multiple-directory-headers

Conversation

@roian6

@roian6 roian6 commented Jul 7, 2026

Copy link
Copy Markdown

Summary

  • Preserve per-directory headers while compacting rtk ls output for multiple directory operands.
  • Keep empty directory operands attributed as dir: plus (empty) instead of collapsing all output into one unlabeled empty result.
  • Add regression coverage for populated and empty multi-directory listings.

Fixes #2857.

Testing

  • rustup run 1.91.0 cargo fmt --all -- --check
  • rustup run 1.91.0 cargo clippy --all-targets
  • rustup run 1.91.0 cargo test
  • Manual smoke: rtk ls dir_a dir_b
  • Manual smoke: rtk ls empty_a empty_b
  • Manual smoke: rtk ls root.txt dir_a

Refresh the existing fix without rewriting its branch history.
Preserve directory headers, empty sections and hidden-entry attribution
while retaining short names, long metadata and native-output fallback.
Recognize date-shaped directory labels before parsing file rows.

Validated with fmt, clippy, all 5329 tests (8 ignored), and eight CLI cases.
@roian6

roian6 commented Nov 15, 2026

Copy link
Copy Markdown
Author

@KuSh, thanks for directing #2868 to this PR. The CI run for current head 1a90d22 is marked action_required and has no jobs, so it has not tested this head.

Could a maintainer approve or restart CI for this head and review the multi-directory header fix for #2857? I can address any concrete CI or review findings.

@KuSh KuSh 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.

Round 1 — CHANGES REQUESTED

Claim: with several directory operands, rtk ls keeps each directory's path: header and shows an empty operand as path: plus (empty), instead of one flat list.
Scope: accept if narrowed to "several directory operands without -R" (round 1, frozen). The header test also catches the sections of -R, so this PR currently regroups recursive listings too. That is #714's problem, handled by #1251 in another format. With -R/--recursive given, keep develop's output and leave it to #1251, with a test pinning that.
Ran: develop @42961225 vs this PR merged on it, real GNU ls 9.x under LC_ALL=C, every row through rtk rewrite first. 18 rows: the PR's 3 repros + ls -l a b, -lR, -la with an empty dir, a dir named total samples, over-cap (120 + 3 entries), a missing operand (exit 2), ACL/fifo/symlink rows ending in :, a file named (empty), plus a sweep of 40 flag spellings × 2 operand sets (80 runs) over a dir holding same:, fifo:, sub:/, ln: -> plain. Invariants: non-default targets, over-cap + accounting, under the hook, every decision. Savings 57% (ls -l a b), 85% (over-cap) vs the 20% floor (CONTRIBUTING.md).
Pins: 20 decisions, 20 mutated, 4 survived the suite: the row regex's type letters and its .+@ suffix (item 1), the header of a group cut entirely by the cap (item 3), and the regex's link-count field, which no real header reaches
Matrix: executed this round; pin check: executed this round
CI @1a90d22d: all green (15/15). Local gate on the merge with develop @42961225: fmt, clippy, 5921 tests pass.

Blocks merge (3, frozen at round 1)

  1. src/cmds/system/ls.rs:528: a row ending in : is taken for a directory header when something comes before the permission field. 20 of the 80 sweep runs: -s/--size, -i/--inode, --dired and every combination containing them, including -lash. These flags put block counts or inode numbers in front of the permissions (--dired indents the row), so LS_ROW_RE misses, and a file, fifo or directory whose name ends in : opens a new section. Single directory, rtk ls -lash colon:
    develop: plain 0B / same: 0B
    this PR: plain 0B / (blank) / 0 -rw-rw-r-- 1 nlecam nlecam 0 Nov 28 02:01 same: / (empty)
    With two operands, rtk ls -lash sweep b prints sweep: then (empty) for a directory holding four entries, and lists ln: and plain under a section named after the fifo: row. That is the misattribution #2857 is about, and here develop gave the right answer. A header is only ever the first line of the output or the line after a blank line; a test on that position, rather than on the row's shape, holds for all these flags. The rows a fix must pass:
    • rtk ls -lash d, -li d and -ls d b, where d holds same:, fifo: (a fifo) and sub: (a directory): every entry is listed under d, and nothing prints (empty).
    • These shapes stay entries, each pinned by a test on captured output, because the mutations dropping them from LS_ROW_RE pass the suite today: the ACL row -rw-rw-r--+ 1 … acl:, the macOS row -rw-r--r--@ 1 … x:, the fifo row prw-rw-r-- 1 … fifo:, and the symlink row lrwxrwxrwx 1 … link -> tgt:.
  2. src/cmds/system/ls.rs:662: (empty) is printed for a directory that holds entries. A group prints (empty) whenever none of its rows was displayed, including rows the parser dropped:
    • a file name ending in . is classified by is_dotdir (which checks how the line ends) as ./..: rtk ls -l dotend b, with dotend/notes., prints dotend: / (empty). Develop leaves notes. out without saying the directory is empty.
    • a row the date parser rejects (mtime in year 10000, set with touch -d 10000-01-01 on tmpfs): rtk ls -l a b prints b: / (empty) / ... (1 filtered), where develop only said (3 filtered).
      A fix must make both rows print the entry, or at least no (empty) for that group. is_dotdir comparing the name after the date with . and .. closes the first. Printing (empty) only for a group with no rows besides real ./.. closes the second.
  3. src/cmds/system/ls.rs:653: unpinned behaviour: a group whose entries all fall past the cap prints no header. Evidence: mutation if all_lines.is_empty() || displayed < CAP_INVENTORY { → if true { passes the whole ls suite and tests/ls_never_worse_test.rs. Test must: on ls -l big colon (120 files in big, 2 in colon), check that the output has no colon: line and that the 2 entries are in the hidden list attributed to colon:. The mutation must turn it red.

Optional (will not hold merge)

  • The recall hint counts lines, not entries. rtk ls -l big a says ... (72 more, 1 filtered) and then [+146 hidden: …], because each hidden entry is stored as header\nentry. Writing a header only when it changes halves the stored file and brings the count closer to the line above it.
  • Empty groups that come after the cap still print, between the last visible entry and ... (70 more) (rtk ls -l big empty1 empty2), so the cut entries read as if they followed empty2:.
  • The compact_ls doc comment says filtered holds "the display name of each entry". Those entries now carry the same header\n prefix as truncated.

Follow-ups

  • Recursive listings (-R), cut by the narrowing: #714, handled by #1251.

Checked and correct (no need to re-verify)

  • Headers and grouping in long mode for 2 and 3 operands, a file operand before directory operands, -la on an empty dir ((empty)), a directory named total samples, a directory named a b c d Jan 1 12:00 folder.
  • Short mode: the compact view with headers is printed only when it is smaller (rtk ls big a: 514 bytes against 1105); in every other row the plain listing is printed unchanged, as on develop.
  • Over-cap accounting: 50 shown + 72 more + 1 filtered = the 123 entries ls prints; the recall output lists all 73, each attributed to its directory.
  • A missing operand (exit 2) still passes the child's output and exit code through.
  • Names containing a newline: GNU ls prints them as ? to rtk's pipe, so no fake header comes from them.
  • Merged with #4103: fmt, clippy and 5943 tests pass.

Your questions

  • "Could a maintainer approve or restart CI for this head and review the multi-directory header fix for #2857?" → CI approved and green on 1a90d22; this is the review.

Next

Narrow to non-recursive listings (with -R, the output equals develop's, pinned by a test), fix items 1 and 2 so that their rows pass, and add the tests from items 1 and 3.
Threads: 3 open (waiting on the author), 0 resolved this round.

Comment thread src/cmds/system/ls.rs
LazyLock::new(|| Regex::new(r"^[bcdlps?-][rwxstST-]{9}[.+@]?\s+\d+\s").unwrap());

fn is_listing_header(line: &str) -> bool {
line.ends_with(':') && !LS_ROW_RE.is_match(line)

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.

With -s, -i or --dired (all passed through to the child), something comes before the permission field, so this check misses and a row ending in : opens a fake section. rtk ls -lash colon turns same: into the header 0 -rw-rw-r-- … same: followed by (empty). The rows a fix must pass are in item 1 of the review.

Comment thread src/cmds/system/ls.rs
entries.push('\n');
}
if all_lines.is_empty() {
entries.push_str("(empty)\n");

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.

This fires for groups whose rows were dropped, not only for empty ones. A file named notes. (taken for ./.. by is_dotdir) or a row the date regex rejects leaves the group with entries and still prints (empty). See item 2.

Comment thread src/cmds/system/ls.rs
if all_lines.is_empty() && group.header.is_none() && groups.len() > 1 {
continue;
}
if all_lines.is_empty() || displayed < CAP_INVENTORY {

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.

Mutating this to if true { passes the suite. Item 3 gives the test that has to fail on it.

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.

rtk ls with multiple directory arguments omits per-directory headers

2 participants