Skip to content

grep - grep -R silently skips dangling symlinks and directory loops #212

Open

Description

@sylvestre

While recursing with -R (follow all symlinks), GNU grep reports a symlink whose target
does not exist (FILE: No such file or directory) and a symlink that leads back to an
ancestor (DIR: warning: recursive directory loop), and exits 2. uu_grep drops both
without a word and exits 0. Named on the command line the same dangling link is reported by
both tools, so only entries found during the walk are affected.

Related but different: #162 (-R skips FIFOs, sockets and devices).

Exposed by the GNU test symlink (grep -R '^' in a directory with a dangling link and a
link to . must exit 2).

Reproduction

$ mkdir lk && cd lk && echo needle > real.txt && ln -s nowhere broken && ln -s . self

Rust (incorrect)

$ ./target/release/grep -R needle
real.txt:needle
# Exit code: 0

GNU (correct)

$ LC_ALL=C /usr/bin/grep -R needle
/usr/bin/grep: self: warning: recursive directory loop
/usr/bin/grep: broken: No such file or directory
real.txt:needle
# Exit code: 2

Test

#[cfg(unix)]
#[test]
fn dereference_recursive_reports_dangling_link_and_loop() {
    let (scene, mut c) = ucmd();
    scene.fixtures.mkdir_all("top");
    scene.fixtures.write("top/kept.txt", "moss\n");
    scene.fixtures.symlink_file("missing-target", "top/dead");
    scene.fixtures.symlink_dir(".", "top/again");
    c.args(&["-R", "moss", "top"])
        .fails_with_code(2)
        .stdout_is("top/kept.txt:moss\n")
        .stderr_contains("top/dead: No such file or directory")
        .stderr_contains("recursive directory loop");
}

Exposed by the GNU grep test suite: symlink.

Activity

  1. added
    bug
    good first issue
    gnu-compat
    on Nov 17, 2026
  2. added a commit that references this issue on Nov 22, 2026
    c31cebb
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

    buggnu-compatgood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions