-
-
Notifications
You must be signed in to change notification settings - Fork 791
Watch: restarts tasks only when at least one watched file changes. #2493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -78,41 +78,46 @@ func (e *Executor) watchTasks(calls ...*Call) error { | |||||||
| } | ||||||||
| e.Logger.VerboseErrf(logger.Magenta, "task: received watch event: %v\n", event) | ||||||||
|
|
||||||||
| cancel() | ||||||||
| ctx, cancel = context.WithCancel(context.Background()) | ||||||||
|
|
||||||||
| e.Compiler.ResetCache() | ||||||||
|
|
||||||||
| // Count the tasks that would be restarted by the watch (_before_ cancelling them). | ||||||||
| watchCount := 0 | ||||||||
| 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) | ||||||||
| } else if !isContextError(err) { | ||||||||
| e.Logger.Errf(logger.Red, "%v\n", err) | ||||||||
| } | ||||||||
| }() | ||||||||
| if ShouldIgnore(event.Name) { | ||||||||
| e.Logger.VerboseErrf(logger.Magenta, "task: event skipped for being an ignored dir: %s\n", event.Name) | ||||||||
| continue | ||||||||
| } | ||||||||
| t, err := e.GetTask(c) | ||||||||
| if err != nil { | ||||||||
| e.Logger.Errf(logger.Red, "%v\n", err) | ||||||||
| continue | ||||||||
| } | ||||||||
| baseDir := filepathext.SmartJoin(e.Dir, t.Dir) | ||||||||
| files, err := e.collectSources(calls) | ||||||||
| if err != nil { | ||||||||
| e.Logger.Errf(logger.Red, "%v\n", err) | ||||||||
| continue | ||||||||
| } | ||||||||
|
Comment on lines
+88
to
+98
|
||||||||
| 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) | ||||||||
| continue | ||||||||
| } | ||||||||
| watchCount++ | ||||||||
| } | ||||||||
| // Cancel _all_ watched tasks, get new context, then restart the tasks. | ||||||||
| if watchCount > 0 { | ||||||||
| cancel() | ||||||||
| ctx, cancel = context.WithCancel(context.Background()) | ||||||||
|
||||||||
| ctx, cancel = context.WithCancel(context.Background()) | |
| ctx, cancel = context.WithCancel(context.Background()) | |
| e.Compiler.ResetCache() |
Copilot
AI
Feb 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goroutine assigns to the outer err variable (err = e.RunTask(...)). This introduces a data race between goroutines and also clobbers the function-scope err (used for watcher creation / error handling). Make err a new local inside the goroutine (e.g., err := ...) so each task run has its own error value.
Copilot
AI
Feb 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new behavior (only cancel/restart when the event matches at least one watched source) is not covered by the existing watch tests. Consider adding a watch_test.go case that triggers a fsnotify event on a file outside sources and asserts that no additional task run happens (and the current run isn't canceled).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ShouldIgnore(event.Name)does not depend on the task call, but it's checked (and logged) once percallsentry, which can spam duplicate "ignored dir" messages on a single event. Consider moving the ignore check outside thefor _, c := range callsloop and returning early when ignored.