rtk icon indicating copy to clipboard operation
rtk copied to clipboard

`rtk ls -t` / `-lt` groups directories before files, breaking mtime order

Open ludobos opened this issue 2 months ago • 3 comments

Environment

  • rtk 0.49.0 (Homebrew bottle), macOS arm64 (Darwin 25.6.0)
  • Claude Code PreToolUse Bash hook; rtk rewrite "ls -lt" -> rtk ls -lt

Summary

rtk ls ignores the -t sort order. It lists directories first, then files, so the most recently modified entry is no longer the first line. Native ls -t sorts all entries by mtime regardless of type. An agent that runs ls -t | head -1 or reads the first line of ls -lt to find the newest file gets a directory, or an older file.

Metadata loss under -l is tracked in #1162. This issue is only about ordering.

Repro

$ mkdir -p repro/adir repro/zdir && cd repro
$ touch -t 202601010000 adir; touch -t 202601020000 zdir
$ touch -t 202609010000 old.txt; touch -t 202609270000 new.txt

$ ls -t
new.txt
old.txt
zdir
adir

$ rtk ls -t
zdir/
adir/
new.txt  0B
old.txt  0B

$ rtk ls -lt
755  zdir/
755  adir/
644  new.txt  0B
644  old.txt  0B

Expected

With -t (and -tr, -S), the output order matches native ls: one list sorted by the requested key, with no grouping by type. Alternatively, the rewrite declines when a sort flag is present.

Workaround in use

Our hook leaves any ls carrying -l or -t unrewritten.

ludobos avatar Sep 27 '26 10:09 ludobos

Same grouping with -S (size sort). Added to the same directory: head -c 5000 /dev/zero > big.bin.

$ ls -S
big.bin
adir
zdir
new.txt
old.txt

$ rtk ls -S
adir/
zdir/
big.bin  4.9K
new.txt  0B
old.txt  0B

big.bin should be first. -tr happens to match native order in this repro only because both directories are also the oldest entries.

ludobos avatar Sep 27 '26 10:09 ludobos

wshm · Automated triage by AI

🔍 Automated Triage

🐛 Category bug
🟡 Priority medium
🎯 Confidence 90%

Summary

rtk ls -t/-lt groups directories before files instead of a single mtime-sorted list, breaking scripts that rely on native ls -t ordering.

This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

📁 Relevant files
  • src/cmds/ls.rs

Suggested Actions

  1. Fix ls sort logic to sort all entries together by mtime/size instead of grouping dirs before files when -t/-tr/-S is pr…
  2. Add regression test using the repro's touch -t timestamps for -t, -tr, -S flag combos
  3. Cross-reference #1162 (metadata loss under -l) since this issue explicitly scopes out that overlap
  4. Consider whether the rewrite should decline (fall back to native ls) when a sort flag is passed, per the issue's altern…

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

rtk-wshm-sync-bot avatar Sep 27 '26 10:09 rtk-wshm-sync-bot

Confirmed the long-listing case on Ubuntu 26.04.1 LTS x86-64 with the RTK 1.2.1 full release. The newer file should precede the older directory. The native order was x, d. RTK put the directory first.

# In an empty directory
export LC_ALL=C
mkdir -p a/d
printf 'content\n' > a/x
chmod 755 a/d
chmod 644 a/x
touch -t 202601010000 a/d
touch -t 202602010000 a/x
ls -lt a | awk 'NR>1 {print $NF}'
rtk ls -lt a

Native output, projected to entry names:

x
d

RTK output:

755  d/
644  x  8B

SNComrade avatar Nov 26 '26 11:11 SNComrade