Skip to content

kqueue: skip broken symlinks in watchDirectoryFiles#734

Open
Yanhu007 wants to merge 1 commit into
fsnotify:mainfrom
Yanhu007:fix/skip-broken-symlinks
Open

kqueue: skip broken symlinks in watchDirectoryFiles#734
Yanhu007 wants to merge 1 commit into
fsnotify:mainfrom
Yanhu007:fix/skip-broken-symlinks

Conversation

@Yanhu007

Copy link
Copy Markdown

Fixes #727

Problem

watchDirectoryFiles calls DirEntry.Info() which follows symlinks. When a symlink points to a nonexistent target, Info() returns an error that propagates up, aborting the entire directory watch. All files listed after the broken symlink are never watched.

Fix

When Info() fails and the entry is a symlink, skip it and continue processing remaining files. This is consistent with the existing pattern for permission errors (EACCES/EPERM) which also skip the file rather than aborting.

fi, err := f.Info()
if err != nil {
    if f.Type()&os.ModeSymlink != 0 {
        continue  // skip broken symlinks
    }
    return fmt.Errorf("%q: %w", path, err)
}

watchDirectoryFiles calls DirEntry.Info() which follows symlinks.
When a symlink points to a nonexistent target, Info() fails and the
error propagates up, aborting the entire directory watch and causing
all subsequent files to be missed.

Skip broken symlinks instead of returning an error, consistent with
the existing handling for permission errors (EACCES/EPERM).

Fixes fsnotify#727
@mattn

mattn commented Apr 21, 2026

Copy link
Copy Markdown
Contributor

I don't think this fixes it. DirEntry.Info() uses lstat on Unix, so it succeeds for broken symlinks — the added continue isn't reached. I verified locally: f.Info() returns no error on a dangling symlink, while os.Open fails with ENOENT.

The real failure is unix.Open at backend_kqueue.go:381 reached via addWatch(..., listDir=true), which surfaces from line 589 (same %q: %w format as the f.Info() branch, likely the source of confusion). Also, no test is added, so this shouldn't be merged as-is.

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.

Watching a directory fails when it encounters a symbolic link to a nonexistent file

2 participants