Skip to content

fix(ls): preserve hierarchy for -R + passthrough incompatible flags (#714) - #1251

Open
pszymkowiak wants to merge 3 commits into
developfrom
fix/ls-recursive-hierarchy-v2
Open

pszymkowiak wants to merge 3 commits into
developfrom
fix/ls-recursive-hierarchy-v2

Conversation

@pszymkowiak

Copy link
Copy Markdown
Collaborator

Supersedes #799 — that PR was based on the pre-refacto src/ls.rs and could not be rebased cleanly after the move to src/cmds/system/ls.rs with the new runner::run_filtered architecture. Ported the fix to the current codebase.

Closes #714

Changes

  1. Recursive hierarchy (rtk ls -R flattens recursive output and loses hierarchy context #714)

    • New compact_ls_recursive() parses ls -laR sections and emits:
      • Subsection headers: /path/to/dir:
      • Children indented 2 spaces under their header
    • LLM agents can now tell which files live in which directory. Before, everything was collapsed into a flat list.
  2. Incompatible flags passthrough

    • Flags -1, -d, -F, -C, -m, -x, -i, -p change ls output format enough that our ls -la parser breaks.
    • Now pass those flags through to raw ls so users still get the expected behavior.

Before/After

BEFORE (flat):               AFTER (hierarchy):
empty-dir/                   empty-dir/
group-a/                     group-a/
group-b/                     group-b/
nested/                      nested/
readme.md  7B                readme.md  7B
details.md  8B
overview.md  11B             /path/group-a:
examples.md  9B                details.md  8B
overview.md  11B               overview.md  11B
notes.md  6B
                             /path/group-b:
                               examples.md  9B
                               overview.md  11B

                             /path/nested:
                               notes.md  6B

Tests

  • cargo test — 1386 passed, 0 failed, 0 regressions
  • 2 new tests for compact_ls_recursive (hierarchy + empty subdir edge case)
  • Manual: rtk ls -R, rtk ls -1, rtk ls all work as expected
  • cargo fmt --all + cargo clippy --all-targets clean

…#714)

- Recursive ls output now shows directory headers + indented children so the
  LLM can tell which files live in which directory. Previously every entry
  was collapsed into a flat list that lost all path information.
- Incompatible flags (-1, -d, -F, -C, -m, -x, -i, -p) change ls output format
  enough that our `ls -la` parser breaks. Pass those through to raw ls so
  users still get the expected behavior.

Supersedes #799 — ported to the new `runner::run_filtered` architecture.

Signed-off-by: Patrick szymkowiak <[email protected]>
@pszymkowiak pszymkowiak added bug Something isn't working effort-medium 1-2 jours, quelques fichiers filter-quality Filter produces incorrect/truncated signal labels Apr 12, 2026
@pszymkowiak

Copy link
Copy Markdown
Collaborator Author

[w] wshm · Automated triage by AI

📊 Automated PR Analysis

🐛 Type bug-fix
🟡 Risk medium

Summary

Fixes recursive ls output to preserve directory hierarchy by introducing compact_ls_recursive() that emits subsection headers with indented children, and adds a passthrough path for flags incompatible with the ls -la parser. This ensures LLM agents can distinguish which files belong to which directory instead of seeing a flat list.

Review Checklist

  • Tests present
  • Breaking change
  • Docs updated

Linked issues: #714


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

@pszymkowiak
pszymkowiak requested a review from aeppling April 12, 2026 21:02

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

Otherwise LGTM!

Comment thread src/cmds/system/ls.rs Resolved
Comment thread src/cmds/system/ls.rs Resolved
Merge develop into the branch. ls.rs now follows develop's plan/render
pipeline: -R is detected from the parsed arguments, and each directory section
is compacted under its own header so a listing still says which directory each
entry belongs to. The never-worse guard against native ls still applies.
Only the separator colon is removed from a section header, so a directory named
'dir:' keeps its name. A long-format line ending in ':' (a file operand such as
'file:') is an entry, not a header. Adds tests for the flag detection and the
indented long listing.

@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 2 of 3 — CHANGES REQUESTED

Claim: rtk ls -R keeps the directory each entry belongs to (#714), section by section, instead of flattening the tree.
Scope: accept (frozen from this round on; the flag passthrough in the description is no longer in the diff). Maintainer PRs: checked against #5099 (KuSh): long_name is gone there, so whichever of the two lands second changes the recursive line to a Flag constant (two lines; merged with that change, both test sets pass, 5644/0). Checked against #4103 (KuSh): ls.rs hunks are disjoint.
Ran: real GNU ls (coreutils, LC_ALL=C child, one fr_FR run), develop 8bb6096 vs this head merged onto it, 35 rows: the #714 tree, -R/-lR/-laR/--recursive/--rec/-lRd/-R1/-g -R/--format=long/--, file + dir operands, no operand (.:), empty dir, unreadable subdir (exit 1), missing operand (exit 2), names with spaces and colons (dir:, a:b, file:), noise dirs, 60/55/2- and 75-entry sections, rtk's own src/; invariants: under the hook (every row via rtk rewrite, recall hint followed), over-cap + accounting, split streams, non-default targets, every decision; savings -lR src 59%, -laR src/cmds 66%, -lR src/cmds/system 57% vs 20% floor (CONTRIBUTING.md); plain -R prints native byte for byte (0%, correct: the indented form is larger, so never_worse keeps native).
Pins: 13 decisions, 16 mutations, 7 survived: 3 are below (item 2), 1 is item 1's total conjunct, 1 is the -R gate (Optional 3), 2 change nothing on any input ls can print (the empty-headerless-section continue, and dropping ends_with(':'))
Matrix: never run before; executed this round. Pin check: never run before; executed this round.
CI @c1eef2a3: all green (22/22); merged onto develop 8bb6096: fmt, clippy, 5571 tests pass.

Blocks merge (2)

  1. src/cmds/system/ls.rs:453, :434 — section markers matched by text a real name can spell (2 sites, both must change).
    • (empty): a file named (empty) in a names-only section is dropped by the line filter, with no notice. rtk ls -R h6b with h6b/d holding (empty) + 80 long names: 49 shown + ... (31 more) = 80 of 81. develop lists it. The marker is compact_ls's own output; it can only be told apart from a file of that name by what compact_ls parsed (a file named (empty) is never noise, and it is never the only line left by truncation).
    • total : a total N line never ends in :, so !line.starts_with("total ") only ever rejects real headers. rtk ls -lR other 'total stuff' drops both total stuff: and total stuff/in: with no notice, and a, in/, q print flush-left right after other:'s entries.
      Rows a fix must satisfy: ls -R h6b → d shows 50 + (31 more) = 81, (empty) among them; ls -lR other 'total stuff' → total stuff: and total stuff/in: headers with a/in/ and q indented under them; a section that is really empty or noise-only still prints its header alone.
  2. src/cmds/system/ls.rs:444, :445, :338 — unpinned behaviour (per-section accounting, and the -R wiring in run). The ls unit tests and tests/ls_never_worse_test.rs all pass under each of:
    • truncated.extend(cut) → truncated.extend(cut.into_iter().take(0)): the ... (N more) notice and its recall vanish. Test must: sections of 60, 55 and 2 entries → 15 truncated, shown + truncated = 117.
    • filtered.extend(hid) → filtered.extend(hid.into_iter().take(0)): a node_modules dropped in a subsection is no longer counted. Test must: node_modules in a non-first section → filtered == ["node_modules/"].
    • in run, render(…, recursive, …) → render(…, false, …): develop's flat listing is back. ls_never_worse_test's -R case only checks size. Test must run the binary: rtk ls -lR on a temp tree with sub/notes.md → the line after ./sub: is notes.md indented.
      Each mutation must turn its test red.

Optional — will not hold merge

  1. A cut section's notice and recall lose the directory (ls.rs:442). rtk ls -lR big2 (aaa 60, bbb 55, ccc 2): aaa and bbb show 50 each with no marker, ... (15 more) prints after ccc (which was not cut), and rtk recall returns a51…a60 b51…b55 with no directory. develop's flat listing loses the directory too, so this is not a regression, but it is the hierarchy this PR is about, for any directory over 50 entries. A per-section ... (N more) and dir/name lines in the stored part would close it. I'll file it if you'd rather keep it out of this PR.
  2. Noise dirs are counted as filtered but their sections print in full. rtk ls -lR odd: node_modules/ and target/ are in ... (2 filtered) in the odd: section, then odd/node_modules:, odd/node_modules/pkg:, odd/target/debug: are all listed. Same offer.
  3. The -R gate: ls a b prints the same dir: sections and stays on the flat path (#2857, which #2865 targets). No test decides between the two; calling compact_recursive for every listing closes #2857 as well. Your call; either way a test should pin it.
  4. Blank lines between sections (thread from round 1): 25 B of 7064 on ls -lR src (0.35%), and they match ls. Fine as is; resolving the thread.

Checked and correct — no need to re-verify

  • #714's tree: -lR hierarchy correct, 273 B vs 657 B native; -R = native.
  • Headers with colons (odd/dir::, odd/a:b:), names with spaces, a file operand named file: (stays an entry), file operands before the first header (unindented), .: with no operand.
  • --recursive, --rec, -laR, -g -R, --format=long, -- all reach the recursive path; -lRd and -R1 behave like develop.
  • Failure paths unchanged: unreadable subdir (exit 1) and missing operand (exit 2) print what ls printed, stderr forwarded.
  • Accounting on a single cut section: 3 + 2 + 50 shown + 25 = 80.
  • fr_FR shell: same output (the child runs under LC_ALL=C).
  • A name with an embedded blank line cannot fake a section (the child prints ?); with --show-control-chars native ls is equally ambiguous.
  • Round-1 stderr thread: that code is gone; the runner forwards non-empty stderr on every exit (src/core/runner.rs:354). Resolving it.

Next

Item 1 (both sites) and item 2's three tests; please also update the title and description to the current claim (the flag passthrough and the April before/after are no longer in the diff). Round 3 then lands it by fixup or approval.
Rounds: 2/3. Threads: 2 open (item 1, item 2), 2 resolved this round (round-1 threads).

Comment thread src/cmds/system/ls.rs
}
out.push_str(path);
out.push_str(":\n");
for line in entries.lines().filter(|line| *line != "(empty)") {

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.

Blocks merge, item 1: this filter drops a real file named (empty) (rtk ls -R h6b: 80 of 81 entries, no notice; develop lists it). The same class is at line 434: !line.starts_with("total ") can only reject a real header, since a total N line never ends in : (rtk ls -lR other 'total stuff' loses both of its headers). Rows a fix must satisfy are in the review body.

Comment thread src/cmds/system/ls.rs

let (entries, count, cut, hid) = compact_ls(&body.join("\n"), show_all, show_long);
parsed_count += count;
truncated.extend(cut);

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.

Blocks merge, item 2: no test fails when this line becomes truncated.extend(cut.into_iter().take(0)). The same holds for filtered.extend(hid) at line 445 and for render(…, false, …) in place of recursive at line 338. The tests that would kill each mutation are listed in the review body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working effort-medium 1-2 jours, quelques fichiers filter-quality Filter produces incorrect/truncated signal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants