Skip to content

grep - grep -r --exclude-dir='.*' with no path excludes the implicit . and searches nothing #165

Open

Description

@sylvestre

Known: #153 (open PR) — #153

When -r is given without a file operand, grep searches the current directory. GNU never
applies --exclude-dir to that implicit ., so the common idiom
grep -r --exclude-dir='.*' PATTERN skips hidden directories such as .git and searches
everything else. uutils grep matches the implicit . itself against the pattern, skips the
whole tree, and always exits 1 with no output. --exclude-dir=. behaves the same way.

(An explicit operand is different: GNU does apply --exclude-dir to it, e.g.
grep -r --exclude-dir=. hit . and grep -r --exclude-dir=dir hit dir search nothing in
both implementations; that part already matches.)

Related: the scanner report also covers --include/--exclude on command-line files, which
is handled by the open PR #153 (see also merged
#146). This issue is only about the implicit ..

Reproduction

$ mkdir -p d/.hidden d/sub && echo hit > d/.hidden/h && echo hit > d/sub/s && echo hit > d/x.php && cd d

Rust (incorrect)

$ ./target/release/grep -r --exclude-dir='.*' hit | sort
# Output: (none)
# Exit code: 1
$ ./target/release/grep -r --exclude-dir=. hit | sort
# Output: (none)
# Exit code: 1

GNU (correct)

$ LC_ALL=C /usr/bin/grep -r --exclude-dir='.*' hit | sort
sub/s:hit
x.php:hit
# Exit code: 0
$ LC_ALL=C /usr/bin/grep -r --exclude-dir=. hit | sort
.hidden/h:hit
sub/s:hit
x.php:hit
# Exit code: 0

Root cause

process_implicit_cwd calls process_dir(lb, Path::new("."), true), and process_dir
checks every directory entry, including the walk root, against --exclude-dir
(src/searcher.rs:172-175):

if file_type.is_dir() {
    if self.config.exclude_dir_globs.skips(entry.file_name()) {
        walker.skip_current_dir();
    }
    continue;
}

For the root entry entry.file_name() is ., which matches .* (and .), so the whole walk
is skipped. GNU only exempts the implicit operand (command_line && omit_dot_slash in
skipped_file).

Test

#[test]
fn exclude_dir_does_not_apply_to_implicit_cwd() {
    let (scene, _) = ucmd();
    scene.fixtures.mkdir_all("flat/.hidden");
    scene.fixtures.write("flat/.hidden/h.txt", "grep me\n");
    scene.fixtures.write("flat/only.txt", "grep me\n");

    let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
    c.current_dir(scene.fixtures.plus("flat"))
        .args(&["-r", "--exclude-dir=.*", "grep"])
        .succeeds()
        .stdout_only("only.txt:grep me\n");

    // An explicit "." operand is still subject to --exclude-dir.
    let mut c = scene.cmd(env!("CARGO_BIN_EXE_grep"));
    c.current_dir(scene.fixtures.plus("flat"))
        .args(&["-r", "--exclude-dir=.", "grep", "."])
        .fails_with_code(1)
        .no_output();
}

Reported by Anthropic's OSS Scanner (ANT-2026-6JM9AYXR).

Activity

  1. added
    bugSomething isn't working
    oss-scannerFound by Anthropic's OSS Scanner
    gnu-compatBehaviour differs from GNU
    on Nov 9, 2026
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

    bugSomething isn't workinggnu-compatBehaviour differs from GNUgood first issueGood for newcomersoss-scannerFound by Anthropic's OSS Scanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions