From 9e261c18b397b88eabc3062369ba623a7742dc4e Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Thu, 20 Dec 2018 12:20:34 -0500 Subject: [PATCH] watch: fix a bug when a file and its ancestor both have direct watches (#863) --- pkg/watch/notify_test.go | 26 ++++++++++++++++++++++++++ pkg/watch/ospath.go | 22 +++------------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/pkg/watch/notify_test.go b/pkg/watch/notify_test.go index 4c7608c1d..7d87def1d 100644 --- a/pkg/watch/notify_test.go +++ b/pkg/watch/notify_test.go @@ -290,6 +290,25 @@ func TestMoveAndReplace(t *testing.T) { f.assertEvents(file) } +func TestWatchBothDirAndFile(t *testing.T) { + f := newNotifyFixture(t) + defer f.tearDown() + + dir := f.JoinPath("foo") + fileA := f.JoinPath("foo", "a") + fileB := f.JoinPath("foo", "b") + f.WriteFile(fileA, "a") + f.WriteFile(fileB, "b") + + f.watch(fileA) + f.watch(dir) + f.fsync() + f.events = nil + + f.WriteFile(fileB, "b-new") + f.assertEvents(fileB) +} + type notifyFixture struct { *tempdir.TempDirFixture notify Notify @@ -318,6 +337,13 @@ func newNotifyFixture(t *testing.T) *notifyFixture { } } +func (f *notifyFixture) watch(path string) { + err := f.notify.Add(path) + if err != nil { + f.T().Fatalf("notify.Add: %s", path) + } +} + func (f *notifyFixture) assertEvents(expected ...string) { f.fsync() diff --git a/pkg/watch/ospath.go b/pkg/watch/ospath.go index e80b6dd2d..2243914e3 100644 --- a/pkg/watch/ospath.go +++ b/pkg/watch/ospath.go @@ -1,24 +1,8 @@ package watch -import ( - "os" - "path/filepath" - "strings" -) +import "github.com/windmilleng/tilt/internal/ospath" func pathIsChildOf(path string, parent string) bool { - relPath, err := filepath.Rel(parent, path) - if err != nil { - return true - } - - if relPath == "." { - return true - } - - if filepath.IsAbs(relPath) || strings.HasPrefix(relPath, ".."+string(os.PathSeparator)) { - return false - } - - return true + _, isChild := ospath.Child(parent, path) + return isChild }