diff --git a/task_test.go b/task_test.go index e08ff940..a81155f4 100644 --- a/task_test.go +++ b/task_test.go @@ -1359,6 +1359,46 @@ func TestIncludesRemoteOptionalOfflineCacheMiss(t *testing.T) { require.Error(t, err) assert.Contains(t, err.Error(), "was not found in the cache") }) + + t.Run("non-optional nested include still fails", func(t *testing.T) { + t.Parallel() + + var buff bytes.Buffer + e := task.NewExecutor( + task.WithDir("testdata/includes_remote_nested_optional_offline"), + task.WithStdout(&buff), + task.WithStderr(&buff), + task.WithOffline(true), + task.WithRemoteCacheDir(t.TempDir()), + ) + + // The nested remote include is not optional, so its offline cache + // miss must fail the run even though the parent include is + // optional: it must not be silently skipped. + err := e.Setup() + require.Error(t, err) + assert.Contains(t, err.Error(), "was not found in the cache") + }) + + t.Run("nested optional include is skipped", func(t *testing.T) { + t.Parallel() + + var buff bytes.Buffer + e := task.NewExecutor( + task.WithDir("testdata/includes_remote_nested_optional_offline_skip"), + task.WithStdout(&buff), + task.WithStderr(&buff), + task.WithOffline(true), + task.WithRemoteCacheDir(t.TempDir()), + ) + + // The symmetric case: an optional remote include nested inside a + // local include is still skipped on an offline cache miss, and + // the parent Taskfile keeps working. + require.NoError(t, e.Setup()) + require.NoError(t, e.Run(t.Context(), &task.Call{Task: "local"})) + assert.Contains(t, buff.String(), "Hello from local") + }) } func TestIncludeCycle(t *testing.T) { diff --git a/taskfile/reader.go b/taskfile/reader.go index cc5fbb5b..e3a84e2e 100644 --- a/taskfile/reader.go +++ b/taskfile/reader.go @@ -211,28 +211,21 @@ func (r *Reader) include(ctx context.Context, node Node) error { return err } - // Recurse into the included Taskfile - if err := r.include(ctx, includeNode); err != nil { - // An optional include that cannot be resolved from - // the offline cache is skipped instead of failing - // the run. Other errors (and non-optional includes) - // still fail. - if include.Optional { - if _, ok := errors.AsType[*errors.TaskfileCacheNotFoundError](err); ok { - // The failed read already added a vertex for - // the included Taskfile; remove it so the - // merge does not pick up a vertex with a nil - // Taskfile. If another include already - // attached an edge to it, fail instead of - // leaving a dangling vertex behind. - r.graph.Lock() - removeErr := r.graph.RemoveVertex(includeNode.Location()) - r.graph.Unlock() - if removeErr == nil { - return nil - } + // In offline mode, an optional remote include with no cached + // copy is skipped. The cache is checked up front so a cache + // miss in a nested include cannot be mistaken for this + // include's own miss, and the graph is never touched for a + // skipped include. + if include.Optional && r.offline { + if remote, ok := includeNode.(RemoteNode); ok { + if _, err := NewCacheNode(remote, r.tempDir).Read(); errors.Is(err, os.ErrNotExist) { + return nil } } + } + + // Recurse into the included Taskfile + if err := r.include(ctx, includeNode); err != nil { return err } diff --git a/testdata/includes_remote_nested_optional_offline/Taskfile.yml b/testdata/includes_remote_nested_optional_offline/Taskfile.yml new file mode 100644 index 00000000..5f279751 --- /dev/null +++ b/testdata/includes_remote_nested_optional_offline/Taskfile.yml @@ -0,0 +1,11 @@ +version: "3" + +includes: + opt: + taskfile: ./opt.yml + optional: true + +tasks: + local: + cmds: + - echo "Hello from local" diff --git a/testdata/includes_remote_nested_optional_offline/opt.yml b/testdata/includes_remote_nested_optional_offline/opt.yml new file mode 100644 index 00000000..9b2e6d92 --- /dev/null +++ b/testdata/includes_remote_nested_optional_offline/opt.yml @@ -0,0 +1,10 @@ +version: "3" + +includes: + remote: + taskfile: https://github.com/go-task/task.git//Taskfile.yml + +tasks: + nested: + cmds: + - echo "Hello from nested" diff --git a/testdata/includes_remote_nested_optional_offline_skip/Taskfile.yml b/testdata/includes_remote_nested_optional_offline_skip/Taskfile.yml new file mode 100644 index 00000000..17c2b726 --- /dev/null +++ b/testdata/includes_remote_nested_optional_offline_skip/Taskfile.yml @@ -0,0 +1,10 @@ +version: "3" + +includes: + local: + taskfile: ./local.yml + +tasks: + local: + cmds: + - echo "Hello from local" diff --git a/testdata/includes_remote_nested_optional_offline_skip/local.yml b/testdata/includes_remote_nested_optional_offline_skip/local.yml new file mode 100644 index 00000000..9d0725a5 --- /dev/null +++ b/testdata/includes_remote_nested_optional_offline_skip/local.yml @@ -0,0 +1,11 @@ +version: "3" + +includes: + remote: + taskfile: https://github.com/go-task/task.git//Taskfile.yml + optional: true + +tasks: + nested: + cmds: + - echo "Hello from nested"