Skip to content

find: existing file as search root returns empty output with exit 0; hidden/gitignored matches silently dropped #2995

Open

Description

@augustoPS

Summary

rtk find produces false negatives with exit 0 in three related ways:

  1. An existing file passed as the search root returns empty output — rtk find <file> -type f finds nothing where native find prints the file.
  2. A bare file argument (rtk find <file>, RTK syntax) is parsed as a glob pattern, so it can never match either.
  3. Pattern searches silently drop hidden and gitignored matches, with no indication that pruning happened.

All three exit 0 and (as of 0.43.0) print nothing at all on zero matches. For an LLM agent consuming rtk output as ground truth, "no output, exit 0" reads as "the file does not exist". This bit us in production agent use: an agent ran rtk find <path-to-existing-file>.md -type f, got empty output, concluded the file was missing, and kicked off a large, expensive wrong-turn recovery. Same silent-data-loss class as #2944.

Environment

  • rtk 0.43.0 (Homebrew bottle), macOS arm64 (Darwin 27)
  • Also reproduced on 0.39.0 — there the empty-result cases at least printed 0 for '*' / 0 for '<pattern>'; 0.43.0 prints nothing, which is harder to notice.

Minimal reproduction

mkdir -p /tmp/rtk-find-repro/sub && cd /tmp/rtk-find-repro
echo hello > journal.md && echo world > sub/note.md
git init -q . && echo "sub/" > .gitignore
mkdir -p .hidden && echo x > .hidden/secret.md

# 1) Explicit file path as search root
rtk find ./journal.md -type f    # → (no output), exit 0   ❌
find ./journal.md -type f        # → ./journal.md          ✅

# 2) Bare file argument (RTK syntax)
rtk find ./journal.md            # → (no output), exit 0   ❌

# 3) Hidden + gitignored matches silently dropped
rtk find . -iname '*.md'         # → journal.md  (1 file)  ❌
find . -iname '*.md'             # → 3 files               ✅

Root cause (as of 5d32d07, src/cmds/system/find_cmd.rs)

1. Root-entry drop in run(). Display paths are computed by stripping the search root:

let display_path = entry_path
    .strip_prefix(path)
    .unwrap_or(entry_path)
    .to_string_lossy()
    .to_string();

if !display_path.is_empty() {
    files.push(display_path);
}

When the root is the file (native-syntax parse puts args[0] into parsed.path), the walker yields the root entry, strip_prefix returns "", and the !display_path.is_empty() guard discards the only match. Sibling symptom of the same line: rtk find <dir> -type d omits the root directory itself, which native find prints.

2. Bare file argument parsed as pattern. Without native flags, parse_rtk_find_args takes args[0] as the glob pattern and searches .. A path like ./journal.md is then glob-matched against basenames and can never match. On 0.39.0 the output 0 for './journal.md' at least exposed the misparse; 0.43.0 prints nothing.

3. Hardcoded ignore-pruning. run() builds the walker with:

builder
    .hidden(!search_hidden)   // skip hidden unless pattern starts with '.'
    .git_ignore(true)
    .git_global(true)
    .git_exclude(true);

Deliberate as a default (per the #1101 comment), but under native-flag syntax — which the help text advertises as "accepts native find flags like -name, -type" — the consumer asked for find semantics and silently gets ripgrep semantics. Nothing in the output signals that matches were pruned.

4. Empty result prints nothing. The files.is_empty() branch tracks stats and returns without printing. Silence is indistinguishable from "no files exist", and is a regression from 0.39.0's explicit 0 for '<pattern>'.

Suggested fixes

In descending order of impact:

  1. Handle a file root. If parsed.path is an existing non-directory, short-circuit: apply the type/pattern checks to it directly and print it (or fix display_path for root entries to fall back to the file name / given path instead of "").
  2. Print an explicit zero-result line (0 matches for '<pattern>' in <path>) instead of nothing — restores and improves the pre-0.43 behavior; costs a handful of tokens and prevents "empty = nonexistent" misreads.
  3. Make pruning visible or scoped: when hidden/gitignore filtering drops entries under native-flag syntax, either emit a one-line notice (note: hidden/gitignored entries skipped — use rtk proxy find for the full list) or disable gitignore handling when native find flags are present.
  4. Treat an existing path in args[0] as a path, not a pattern, in parse_rtk_find_args (or reject it with a clear error).

Workaround

For file-existence checks in agent instructions: use test -f <path> or rtk proxy find …. Pattern searches that must honor hidden/gitignored files: rtk proxy find.

Activity

  1. added
    bug
    help wanted
    on Jul 14, 2026
  2. pszymkowiak commented on Jul 14, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟠 Priority high
    🎯 Confidence 92%

    Summary

    rtk find silently drops matches (file-as-root, bare-file-as-pattern misparse, hardcoded gitignore/hidden pruning) and prints nothing on zero results, making 'no output' indistinguishable from 'file does not exist' — fix in find_cmd.rs per included root-cause analysis with line refs.

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

    📁 Relevant files
    • src/cmds/system/find_cmd.rs

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

  3. ildunari commented on Jul 19, 2026

    @ildunari

    Independently hit this class in production agent use today (agent swept a .git directory for conflict copies, got clean output, concluded there were none — there were six). While narrowing it I found what looks like a fourth silent-false-negative path not covered by this issue or #2996: bracket character classes in -name match nothing.

    It is independent of the hidden/gitignore pruning in case 3 — this reproduces in a pristine directory with no git repo, no .gitignore, no hidden files, and only plain visible regular files:

    mkdir -p /tmp/rtk-bracket && cd /tmp/rtk-bracket
    touch file1.txt file2.txt file3.txt report9.log
    
    rtk find . -name '*[0-9].txt'    # → (no output), exit 0   ❌   native: 3
    rtk find . -name '*[123].txt'    # → (no output), exit 0   ❌   native: 3
    rtk find . -name 'file[12].txt'  # → (no output), exit 0   ❌   native: 2
    rtk find . -name '*[0-9]*'       # → (no output), exit 0   ❌   native: 4
    
    # other glob metacharacters are fine:
    rtk find . -name '*.txt'         # → 3  ✅
    rtk find . -name 'file?.txt'     # → 3  ✅
    rtk find . -name 'file1.txt'     # → 1  ✅

    So * and ? are handled but [...] is not — any bracket expression yields zero matches, exit 0, empty stderr. Same "no output reads as does not exist" failure mode described above. I have not built #2996 to check whether it happens to fix this too, but nothing in that PR's description touches pattern parsing, so I suspect it is a separate fix.

    Two smaller notes that may be useful:

    -type f omits dotfiles, which I think is case 3 seen from another angle — worth including in the test matrix as a non-pattern path:

    touch .hidden visible.txt
    rtk find . -type f    # → visible.txt only        ❌
    find . -type f        # → .hidden, visible.txt    ✅

    A reproduction trap worth flagging for anyone verifying this, because it cost me three "passing" tests before I caught it: piping changes the result. rtk rewrite exits 1 for piped invocations, so the hook leaves them alone and native find runs:

    find . -name '*[0-9].txt' | grep -c .   # → 3  (native — rewrite declined, bug hidden)
    find . -name '*[0-9].txt'               # → 0  (rewritten to `rtk find` — bug fires)

    Any test that pipes to wc -l/grep -c to count results silently exercises the wrong code path and reports the bug as fixed. Worth an explicit note in the test plan.

    Environment: rtk 0.43.0 (Homebrew bottle), macOS arm64 (Darwin 25 and Darwin 27, reproduced on both).

  4. dispather commented on Aug 7, 2026

    @dispather

    Still present on 0.44.2 (macOS arm64, Homebrew) — item 3 reproduces unchanged. Two things to add that I don't think are in the thread yet, both arguing for your suggested fix #3.

    1. The pruning is inconsistent between rtk's own commands, not just against native find

    Same tree, same .gitignore, one ignored directory:

    mkdir -p repo/graphify-out repo/keepme
    (cd repo && git init -q && printf 'graphify-out/\n' > .gitignore)
    echo x > repo/graphify-out/data.txt
    command shows the ignored path?
    rtk ls repo yes — and rtk ls repo/graphify-out lists data.txt 12B
    rtk tree repo yes
    rtk du -sh repo/graphify-out yes
    rtk rg --files no — but native rg does the same, so this one is a faithful proxy
    rtk find repo -name graphify-out -type d no

    At file level too:

    find     . -name data.txt   # => ./graphify-out/data.txt   ./keepme/data.txt
    rtk find . -name data.txt   # => keepme/data.txt

    So within a single session rtk ls will show you a directory that rtk find reports does not exist. The existing argument in this issue is "native-flag syntax promises find semantics and delivers ripgrep semantics" — this is a second, independent one: whatever the intended default, the tool disagrees with itself, and a caller has no way to know which answer to trust. rtk rg is the useful contrast — it filters, but only because rg does, so it stays honest as a proxy.

    2. The obvious cross-check fails the same way

    rtk find omits the trailing newline on its last record, so a one-record result counts as zero:

    rtk find repo -name keepme -type d            # => keepme        (1 record, present)
    rtk find repo -name keepme -type d | wc -l    # => 0

    This matters here specifically because counting is what a careful caller does to detect this bug. "Don't trust the listing, pipe it through wc -l" is the natural defensive habit, and it returns 0 for a result that exists — confirming the same false negative by a second route. Terminating the final record with \n is independent of the ignore-pruning decision and would fix this on its own. (#3303 reports the same shape for git status --porcelain, so this may be one underlying behaviour rather than two.)

    Confirmations

    • Your stated workaround holds on 0.44.2: rtk proxy find returns the ignored entry where rtk find returns nothing.
    • Two hypotheses I ruled out before finding .gitignore, in case they save someone else time: a result-count cap (a flat tree of 8 matching directories returned all 8) and symlink handling (controls with and without a symlink both returned everything).

    Bounding the exposure

    For anyone who wants to know what this hid from them rather than just fixing it forward — the affected set is enumerable outside the session:

    git -C "$REPO" ls-files --others --ignored --exclude-standard --directory

    That's an upper bound on paths rtk find would have reported absent. On two of our repos it was 2,535 and 14 entries. The bias is what makes this sting: .gitignore lists caches and build outputs, which is exactly what people use find to check the existence of — so "is this cache still there?" is the question most likely to be answered wrongly, and the wrong answer is always "no". In our case an agent nearly rebuilt an intact 26 MB artifact on that basis.

  5. kadirmobil-sudo commented on Sep 8, 2026

    @kadirmobil-sudo

    A sharper case of point 3 (silently dropped gitignored matches): the pruning also drops paths that are tracked in git.

    Git does not ignore a file that is already in the index, so git check-ignore reports it as visible while rtk find still omits it. Anyone verifying "is this ignored?" the obvious way gets the opposite answer from what rtk does.

    mkdir -p repro/data/kept repro/data/legacy && cd repro
    git init -q . && echo x > data/legacy/file.txt && echo y > data/kept/file.txt
    git add -A && git commit -qm init
    printf 'data/legacy/\n' > .gitignore && git add .gitignore && git commit -qm ignore
    
    $ git ls-files data/legacy
    data/legacy/file.txt                     # tracked
    
    $ git check-ignore -v data/legacy; echo $?
    1                                        # git does NOT ignore it
    
    $ find data -maxdepth 1 -type d          # real find
    data
    data/legacy
    data/kept
    
    $ rtk find data -maxdepth 1 -type d; echo $?
    1F 1D:
    
    ./ kept
    0                                        # legacy is gone, exit 0, no notice

    This is how it surfaced in practice: auditing a vault directory, rtk find output -maxdepth 1 -type d returned 15 of 28 directories. Twelve of the thirteen missing ones matched .gitignore patterns; four of those twelve were tracked files that git itself does not ignore. There was no truncation notice and the exit status was 0, so the shorter list looked like the complete answer.

    Two smaller things visible in the same output:

    • The header reads 1F 1D: for a -type d query, where every result is a directory. The F/D split appears to be miscounted for directory-only searches.
    • -maxdepth 1 is honored, so this is not the pattern-search path specifically — the pruning applies to plain directory listings too.

    Environment: rtk 0.40.0, macOS 26.5.2 arm64 (Darwin 25.5.0). Not verified against 0.48.0.

  6. BeeKnown commented on Sep 26, 2026

    @BeeKnown

    Following up here from #4244, which was closed as a duplicate of this issue.

    One detail from #4244 that isn't covered above: when rtk find prunes a match inside a gitignored directory, the recall log doesn't help you recover it. The output ends with a (N filtered) line pointing at ~/Library/Application Support/rtk/tee/<timestamp>_find-hidden.log, but that log lists only the ignored directory name (e.g. data/), not the file that matched. So even a user who notices the filtered count and opens the log has no way to see that a real match was dropped, or which one. The only way to get it back is to rerun with rtk proxy find.

    Minimal repro (rtk 0.50.0, macOS, Homebrew bottle):

    mkdir -p /tmp/rtk-ft && cd /tmp/rtk-ft && git init -q
    printf 'data/\n' > .gitignore
    mkdir -p data/subdir src
    touch data/subdir/target.py src/target.py data/untracked.txt
    git add .gitignore src/target.py && git add -f data/subdir/target.py
    
    rtk find . -iname 'target.py'        # -> src/target.py only, plus "(1 filtered)"
    rtk proxy find . -iname 'target.py'  # -> both files

    The tee log for the first command contains data/ and nothing else.

    For the force-tracked part of case 3 specifically, #4253 is an open fix: after the walk it adds tracked files that match the pattern from git ls-files --cached --ignored --exclude-standard. It doesn't change the tee log, which it lists as out of scope, so the log problem above would still need its own change. A log entry naming the pruned paths that matched, rather than just the ignored directory, would be enough. I've retargeted #4253 to reference this issue.

  7. KuSh commented on Nov 25, 2026

    @KuSh
    Collaborator

    One more input that reaches the same misreading: a glob as the first starting point. From PowerShell or cmd, which pass globs through unexpanded, rtk find s* -name '*.rs' (in a tree with sub/x.rs) is read as the pattern form, becomes find -name 's*' -name '*.rs', and prints nothing with exit 0. Native find from bash prints sub/x.rs. Measured on Windows 11 with develop e87da19 and with #4544's head; rtk find ./s* -name '*.rs' works on #4544 because looks_like_path keeps it out of the pattern form.

  8. KuSh commented on Nov 28, 2026

    @KuSh
    Collaborator

    Status on v1.2.1: a file given as the search root (rtk find ./journal.md -type f and rtk find ./journal.md) and bracket classes like -name '*[0-9].txt' now return the same matches as find. What remains is the pruning of hidden and gitignored paths (rtk find . -iname '*.md' shows (2 filtered) where find lists the files, the recall entry names only the directories, and a git-tracked file under an ignored directory is still dropped), plus the glob-as-first-starting-point form from the previous comment. Narrowing this issue to those.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:clibughelp wantedpriority:high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions