From e63cbfba0e7409b5fe3d44d85b8b660049a966f8 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Tue, 31 Jan 2023 11:35:59 +0100 Subject: [PATCH] use tilt watcher to track filesystem changes Signed-off-by: Nicolas De Loof --- go.mod | 4 +--- go.sum | 2 -- pkg/compose/watch.go | 19 +++++++++++++------ pkg/watch/dockerignore.go | 2 +- pkg/watch/watcher_fsevent.go | 4 ++++ 5 files changed, 19 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 992ad91f0..0f6a5774f 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/docker/docker v20.10.20+incompatible // replaced; see replace rule for actual version github.com/docker/go-connections v0.4.0 github.com/docker/go-units v0.5.0 - github.com/fsnotify/fsnotify v1.6.0 + github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/golang/mock v1.6.0 github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-version v1.6.0 @@ -153,8 +153,6 @@ require ( require go.uber.org/goleak v1.1.12 -require github.com/fsnotify/fsevents v0.1.1 - replace ( // Override for e2e tests github.com/cucumber/godog => github.com/laurazard/godog v0.0.0-20220922095256-4c4b17abdae7 diff --git a/go.sum b/go.sum index a6e9d8a18..260f4c64d 100644 --- a/go.sum +++ b/go.sum @@ -205,8 +205,6 @@ github.com/felixge/httpsnoop v1.0.2/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSw github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= -github.com/fsnotify/fsevents v0.1.1 h1:/125uxJvvoSDDBPen6yUZbil8J9ydKZnnl3TWWmvnkw= -github.com/fsnotify/fsevents v0.1.1/go.mod h1:+d+hS27T6k5J8CRaPLKFgwKYcpS7GwW3Ule9+SC2ZRc= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= diff --git a/pkg/compose/watch.go b/pkg/compose/watch.go index 14eb03573..b9ad7f52a 100644 --- a/pkg/compose/watch.go +++ b/pkg/compose/watch.go @@ -24,7 +24,7 @@ import ( "github.com/compose-spec/compose-go/types" "github.com/docker/compose/v2/pkg/api" "github.com/docker/compose/v2/pkg/utils" - "github.com/fsnotify/fsnotify" + "github.com/docker/compose/v2/pkg/watch" "github.com/jonboulle/clockwork" "github.com/mitchellh/mapstructure" "github.com/pkg/errors" @@ -88,25 +88,32 @@ func (s *composeService) Watch(ctx context.Context, project *types.Project, serv } context := service.Build.Context - watcher, err := fsnotify.NewWatcher() + ignore, err := watch.LoadDockerIgnore(context) if err != nil { return err } + + watcher, err := watch.NewWatcher([]string{context}, ignore) + if err != nil { + return err + } + fmt.Println("watching " + context) - err = watcher.Add(context) + err = watcher.Start() if err != nil { return err } + eg.Go(func() error { defer watcher.Close() //nolint:errcheck for { select { case <-ctx.Done(): return nil - case event := <-watcher.Events: - log.Println("fs event :", event.String()) + case event := <-watcher.Events(): + log.Println("fs event :", event.Path()) needRefresh <- service.Name - case err := <-watcher.Errors: + case err := <-watcher.Errors(): return err } } diff --git a/pkg/watch/dockerignore.go b/pkg/watch/dockerignore.go index 6b29aaa5a..e036883ab 100644 --- a/pkg/watch/dockerignore.go +++ b/pkg/watch/dockerignore.go @@ -60,7 +60,7 @@ func (i dockerPathMatcher) MatchesEntireDir(f string) (bool, error) { return true, nil } -func NewDockerIgnoreTester(repoRoot string) (*dockerPathMatcher, error) { +func LoadDockerIgnore(repoRoot string) (*dockerPathMatcher, error) { absRoot, err := filepath.Abs(repoRoot) if err != nil { return nil, err diff --git a/pkg/watch/watcher_fsevent.go b/pkg/watch/watcher_fsevent.go index b4061dbf5..b88799de8 100644 --- a/pkg/watch/watcher_fsevent.go +++ b/pkg/watch/watcher_fsevent.go @@ -19,6 +19,9 @@ package watch +/** +FIXME this implementation requires CGO + import ( "path/filepath" "time" @@ -155,3 +158,4 @@ func newFSEventWatcher(paths []string, ignore PathMatcher) (*fseventNotify, erro } var _ Notify = &fseventNotify{} +**/