diff --git a/task_test.go b/task_test.go index 540e7ba8de..0a050812b7 100644 --- a/task_test.go +++ b/task_test.go @@ -2377,6 +2377,28 @@ func TestRunOnceSharedDeps(t *testing.T) { assert.Contains(t, buff.String(), `task: [service-b:build] echo "build b"`) } +func TestRunOnceGlobalInIncludedTaskfile(t *testing.T) { + t.Parallel() + + const dir = "testdata/run_once_included" + + var buff bytes.Buffer + e := task.NewExecutor( + task.WithDir(dir), + task.WithStdout(&buff), + task.WithStderr(&buff), + task.WithForceAll(true), + ) + require.NoError(t, e.Setup()) + require.NoError(t, e.Run(t.Context(), &task.Call{Task: "build"})) + + // The included Taskfile sets `run: once` globally, so its build task should + // execute once even though both service-a and service-b depend on it. + assert.Equal(t, 1, strings.Count(buff.String(), `echo "build library"`)) + assert.Contains(t, buff.String(), `task: [service-a] echo "build a"`) + assert.Contains(t, buff.String(), `task: [service-b] echo "build b"`) +} + func TestRunOnceSharedFailurePropagates(t *testing.T) { t.Parallel() diff --git a/taskfile/ast/taskfile.go b/taskfile/ast/taskfile.go index 20b4476cd5..52260f1d4e 100644 --- a/taskfile/ast/taskfile.go +++ b/taskfile/ast/taskfile.go @@ -76,6 +76,13 @@ func (t1 *Taskfile) Merge(t2 *Taskfile, include *Include) error { } } } + if t2.Run != "" { + for _, t := range t2.Tasks.All(nil) { + if t.Run == "" { + t.Run = t2.Run + } + } + } t1.Vars.Merge(t2.Vars, include) t1.Env.Merge(t2.Env, include) return t1.Tasks.Merge(t2.Tasks, include, t1.Vars) diff --git a/testdata/run_once_included/Taskfile.yml b/testdata/run_once_included/Taskfile.yml new file mode 100644 index 0000000000..664c297872 --- /dev/null +++ b/testdata/run_once_included/Taskfile.yml @@ -0,0 +1,22 @@ +version: '3' + +includes: + library: ./library + +tasks: + build: + deps: + - service-a + - service-b + + service-a: + deps: + - library:build + cmds: + - echo "build a" + + service-b: + deps: + - library:build + cmds: + - echo "build b" diff --git a/testdata/run_once_included/library/Taskfile.yml b/testdata/run_once_included/library/Taskfile.yml new file mode 100644 index 0000000000..f135b1c1af --- /dev/null +++ b/testdata/run_once_included/library/Taskfile.yml @@ -0,0 +1,8 @@ +version: '3' + +run: once + +tasks: + build: + cmds: + - echo "build library"