diff --git a/pkg/watch/notify_test.go b/pkg/watch/notify_test.go index 05dd15e5a..2e26a67dd 100644 --- a/pkg/watch/notify_test.go +++ b/pkg/watch/notify_test.go @@ -277,6 +277,32 @@ func TestWriteGoodLink(t *testing.T) { f.assertEvents(goodFile, link) } +func TestWatchBrokenLink(t *testing.T) { + f := newNotifyFixture(t) + defer f.tearDown() + + newRoot, err := NewDir(t.Name()) + if err != nil { + t.Fatal(err) + } + defer newRoot.TearDown() + + link := filepath.Join(newRoot.Path(), "brokenLink") + missingFile := filepath.Join(newRoot.Path(), "missingFile") + err = os.Symlink(missingFile, link) + if err != nil { + t.Fatal(err) + } + + err = f.notify.Add(newRoot.Path()) + if err != nil { + t.Fatal(err) + } + + os.Remove(link) + f.assertEvents(link) +} + type notifyFixture struct { t *testing.T root *TempDir diff --git a/pkg/watch/watcher_linux.go b/pkg/watch/watcher_linux.go index 20e2f2fb8..d1adca60e 100644 --- a/pkg/watch/watcher_linux.go +++ b/pkg/watch/watcher_linux.go @@ -1,6 +1,7 @@ package watch import ( + "fmt" "io/ioutil" "log" "os" @@ -28,26 +29,27 @@ type linuxNotify struct { func (d *linuxNotify) Add(name string) error { fi, err := os.Stat(name) if err != nil && !os.IsNotExist(err) { - return err + return fmt.Errorf("notify.Add(%q): %v", name, err) } + // if it's a file that doesn't exist watch it's parent if os.IsNotExist(err) { parent := filepath.Join(name, "..") err = d.watcher.Add(parent) if err != nil { - return err + return fmt.Errorf("notify.Add(%q): %v", name, err) } d.watchList[parent] = true } else if fi.IsDir() { err = d.watchRecursively(name) if err != nil { - return err + return fmt.Errorf("notify.Add(%q): %v", name, err) } d.watchList[name] = true } else { err = d.watcher.Add(name) if err != nil { - return err + return fmt.Errorf("notify.Add(%q): %v", name, err) } d.watchList[name] = true } @@ -61,7 +63,14 @@ func (d *linuxNotify) watchRecursively(dir string) error { return err } - return d.watcher.Add(path) + err = d.watcher.Add(path) + if err != nil { + if os.IsNotExist(err) { + return nil + } + return fmt.Errorf("watcher.Add(%q): %v", path, err) + } + return nil }) }