Skip to content

feat(ls): support -h human-readable sizes and -t/-S/-r sorting - #228

Merged
ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:feat/ls-human-sort-flags
Nov 12, 2026
Merged

ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:feat/ls-human-sort-flags

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Human-readable listings printed help instead of files, and sorting by time or size was rejected. Those are commands agents use to inspect file sizes and pick the newest or largest file.

This changes ls and its tests to add human-readable sizes and time, size and reverse sorting for both directory contents and file operands, including files expanded from a glob. Files are listed before directories, and adjacent file operands no longer get blank lines between them. Direct-bind symlink metadata (#254) and glob expansion on direct binds remain separate issues.

I compared 28 cases with GNU ls covering file operands, mixed directories, recursive sorting, complete long-listing rows and globs. The new operand regressions failed before the fix and pass now, and the full Rust suite, lint, doctests and documentation checks pass.

Related Issues

Part of #207

Documentation PR

N/A

Type of Change

New feature

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler
ferdingler force-pushed the feat/ls-human-sort-flags branch from 4809f50 to 9a41af5 Compare November 12, 2026 22:12
@ferdingler
ferdingler marked this pull request as ready for review November 12, 2026 22:12
@ferdingler
ferdingler requested a review from a team as a code owner November 12, 2026 22:12
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent 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.

Changes requested: one bug. The -h formatting is byte-identical to GNU, and sorting inside a directory matches GNU. But ls -t *.log | head -1, the usual way to get the newest file, now silently returns the alphabetically first file.

  • 🔴 ls.rs:294: -t/-S don't reorder file operands, so any glob gets name order back. Before this PR, ls -t failed with invalid option. Now it gives a wrong answer that looks right. The PR calls this a follow-up, but I think it needs to land here: sort the operands with the same comparator, or reject -t/-S when there are several operands. Details inline.

Everything else is non-blocking (below).

✅ What I verified
  • Head 9a41af5. cargo test --test shell_integration: 1629 passed. The 41 ls_ tests and human_size_matches_gnu pass.
  • human_size against GNU coreutils 9.1 LC_ALL=C ls -lh on sparse files: 7,488 sizes (every K/M/G/T boundary ±3, tenths boundaries, random sizes up to 16T) with 0 mismatches. A second independent run of 6,494 sizes plus numfmt --to=iec --round=up also had 0 mismatches. A fuzz run up to u64::MAX (16E) agreed with the reference. No panic path: the largest exponent is 6, and SUFFIXES has 8 entries.
  • Sorting on a direct bind against GNU: 38 of 48 cases pass. The passes cover -t -tr -S -Sr -tS -St -r -at -aS -Rt -RS -lt -lS -ltr -lhS -lhtr --reverse, ties in name order, ties reversing under -r, the last of -t/-S winning, and sub-second mtimes. Of the 10 failures, 7 are operands (the 🔴 above). The other 3 are things that already happen on main: ./.. under -la, #254, and globs not expanding on direct binds.
  • Scope fits #207 (P0: ls -lh printed help; P1: ls -t -S -h). -r is small and reasonable.
  • Evidence uploaded: repro-ls-t-operands.txt, human-size-gnu-diff.log, sort-gnu-diff.log.
Appendix: non-blocking (5)
  • ⚪ ls.rs:196 and :225: with -lt/-lS, each entry is lstated twice (sort key, then printing), and hidden entries are lstated even when skipped. Keeping the FileStat from the sort key would avoid that. On a direct bind it's twice the host syscalls.
  • ⚪ ls.rs:202: _ => b.1.cmp(&a.1) also matches SortBy::Name, which can't happen inside this branch. Writing SortBy::Size makes the intent explicit.
  • ⚪ ls.rs:4-15: the new help rows (-h, --human-readable …) use a different column width from the old ones (-l …), so the descriptions don't line up.
  • ⚪ tests/shell_integration.rs:3614: -lt only checks .last(). Checking the whole output would prove that complete rows move together. Nothing covers -R or -a combined with a sort.
  • Already on main, not caused here:
    • A blank line is printed between file operands (ls n2 n1 → n2\n\nn1). Fixing it alongside operand sorting would make sense.
    • lstat follows symlinks on direct binds, so -t/-S sort symlinks by their target. Already filed as #254.
    • ls /bound/dir/*.log doesn't expand the glob on a direct bind. I didn't investigate the cause and haven't filed it.

Comment thread src/commands/ls.rs Resolved
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent 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.

Approve. The 🔴 is fixed in b57f989: ls -t *.log | head -1 now returns the newest file, and file operands use the same comparator as directory entries.

  • 🔴 operand sort: fixed. I attacked the fix too: reverse ties, files before directories under -r, a mixed ls -t d c.log a.log (→ c.log a.log, blank line, d:), missing operands, and symlink-to-dir operands. All match GNU.
  • Appendix nits: fixed (single lstat, hidden entries filtered before it, explicit SortBy arms, full-row tests). One nit is left: the help text is still one column off, because the -h/-r descriptions start at column 25 and the rest at column 24. Adding one space to each of the six short rows fixes it.
✅ What I verified
  • Head b57f989. cargo test --test shell_integration: 1634 passed. human_size_matches_gnu passes. cargo fmt --check is clean, and cargo clippy --workspace --all-targets reports no warnings in ls.rs.
  • Compared with GNU coreutils 9.1 on a direct bind, using explicit operands, mixed files and directories, -t/-tr/-S/-Sr/-r, -ta/-Sa/-Rt/-RSr/-ltr/-lhS, and a nonexistent operand. With symlinks removed, every case orders the same as GNU. Where -l lines differ, it's only the nlink/owner columns we don't print, which was already the case on main. The remaining order differences are symlinks being sorted by their target (#254).
  • I re-ran the in-memory VFS repro from my earlier review. -t, -tr and head -1 are now correct.
  • Evidence uploaded: followup-operand-sort-gnu-diff.log.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

Thanks for checking the operand cases. You are right, I missed the remaining one-column difference in the help text. I am leaving that cosmetic nit for a follow-up so this approved revision can finish its CI run without restarting the matrix.

@ferdingler
ferdingler merged commit 0483c6b into strands-agents:main Nov 12, 2026
31 of 32 checks passed
@ferdingler
ferdingler deleted the feat/ls-human-sort-flags branch November 12, 2026 23:50
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