Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 38 additions & 24 deletions watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/puzpuzpuz/xsync/v4"

"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/fingerprint"
"github.com/go-task/task/v3/internal/fsnotifyext"
"github.com/go-task/task/v3/internal/logger"
Expand Down Expand Up @@ -68,6 +67,12 @@ func (e *Executor) watchTasks(calls ...*Call) error {

closeOnInterrupt(w)

watchFiles, err := e.collectSources(calls)
if err != nil {
cancel()
return err
}

go func() {
for {
select {
Expand All @@ -78,34 +83,43 @@ func (e *Executor) watchTasks(calls ...*Call) error {
}
e.Logger.VerboseErrf(logger.Magenta, "task: received watch event: %v\n", event)

// Check if this watch event should be ignored.
if ShouldIgnore(event.Name) {
e.Logger.VerboseErrf(logger.Magenta, "task: event skipped for being an ignored dir: %s\n", event.Name)
continue
}
if event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) {
if event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) || event.Has(fsnotify.Write) {

Without this I think writes to existing files that are not in the sources would still trigger cancellation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks. I've added that.

if !slices.Contains(watchFiles, event.Name) {
relPath := event.Name
if rel, err := filepath.Rel(e.Dir, event.Name); err == nil {
relPath = rel
}
e.Logger.VerboseErrf(logger.Magenta, "task: skipped for file not in sources: %s\n", relPath)
continue
}
}
if event.Has(fsnotify.Create) {
watchFiles, err := e.collectSources(calls)
if err != nil {
e.Logger.Errf(logger.Red, "%v\n", err)
continue
}
if !slices.Contains(watchFiles, event.Name) {
relPath := event.Name
if rel, err := filepath.Rel(e.Dir, event.Name); err == nil {
relPath = rel
}
e.Logger.VerboseErrf(logger.Magenta, "task: skipped for file not in sources: %s\n", relPath)
continue
}
}

// The watch event is good, restart the task calls.
cancel()
ctx, cancel = context.WithCancel(context.Background())

e.Compiler.ResetCache()

for _, c := range calls {
go func() {
if ShouldIgnore(event.Name) {
e.Logger.VerboseErrf(logger.Magenta, "task: event skipped for being an ignored dir: %s\n", event.Name)
return
}
t, err := e.GetTask(c)
if err != nil {
e.Logger.Errf(logger.Red, "%v\n", err)
return
}
baseDir := filepathext.SmartJoin(e.Dir, t.Dir)
files, err := e.collectSources(calls)
if err != nil {
e.Logger.Errf(logger.Red, "%v\n", err)
return
}

if !event.Has(fsnotify.Remove) && !slices.Contains(files, event.Name) {
relPath, _ := filepath.Rel(baseDir, event.Name)
e.Logger.VerboseErrf(logger.Magenta, "task: skipped for file not in sources: %s\n", relPath)
return
}
err = e.RunTask(ctx, c)
if err == nil {
e.Logger.Errf(logger.Green, "task: task \"%s\" finished running\n", c.Task)
Expand Down
Loading