Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c2d75bb

Browse files
nicksndeloof
authored andcommittedFeb 1, 2023
watch: fix a bug when a file and its ancestor both have direct watches (docker#863)
1 parent 1e5a81b commit c2d75bb

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed
 

‎pkg/watch/notify_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,25 @@ func TestMoveAndReplace(t *testing.T) {
290290
f.assertEvents(file)
291291
}
292292

293+
func TestWatchBothDirAndFile(t *testing.T) {
294+
f := newNotifyFixture(t)
295+
defer f.tearDown()
296+
297+
dir := f.JoinPath("foo")
298+
fileA := f.JoinPath("foo", "a")
299+
fileB := f.JoinPath("foo", "b")
300+
f.WriteFile(fileA, "a")
301+
f.WriteFile(fileB, "b")
302+
303+
f.watch(fileA)
304+
f.watch(dir)
305+
f.fsync()
306+
f.events = nil
307+
308+
f.WriteFile(fileB, "b-new")
309+
f.assertEvents(fileB)
310+
}
311+
293312
type notifyFixture struct {
294313
*tempdir.TempDirFixture
295314
notify Notify
@@ -318,6 +337,13 @@ func newNotifyFixture(t *testing.T) *notifyFixture {
318337
}
319338
}
320339

340+
func (f *notifyFixture) watch(path string) {
341+
err := f.notify.Add(path)
342+
if err != nil {
343+
f.T().Fatalf("notify.Add: %s", path)
344+
}
345+
}
346+
321347
func (f *notifyFixture) assertEvents(expected ...string) {
322348
f.fsync()
323349

‎pkg/watch/ospath.go

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
package watch
22

3-
import (
4-
"os"
5-
"path/filepath"
6-
"strings"
7-
)
3+
import "github.com/windmilleng/tilt/internal/ospath"
84

95
func pathIsChildOf(path string, parent string) bool {
10-
relPath, err := filepath.Rel(parent, path)
11-
if err != nil {
12-
return true
13-
}
14-
15-
if relPath == "." {
16-
return true
17-
}
18-
19-
if filepath.IsAbs(relPath) || strings.HasPrefix(relPath, ".."+string(os.PathSeparator)) {
20-
return false
21-
}
22-
23-
return true
6+
_, isChild := ospath.Child(parent, path)
7+
return isChild
248
}

0 commit comments

Comments
 (0)
Please sign in to comment.