fix: skip optional remote includes on offline cache miss

This commit is contained in:
Tide 2026-10-25 22:28:07 +08:00
parent ad2782e170
commit 47a73d6ec9
4 changed files with 79 additions and 0 deletions

View file

@ -1323,6 +1323,44 @@ func TestIncludesRemote(t *testing.T) {
}
}
func TestIncludesRemoteOptionalOfflineCacheMiss(t *testing.T) {
t.Parallel()
t.Run("optional include is skipped", func(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
e := task.NewExecutor(
task.WithDir("testdata/includes_remote_optional_offline"),
task.WithStdout(&buff),
task.WithStderr(&buff),
task.WithOffline(true),
task.WithRemoteCacheDir(t.TempDir()),
)
require.NoError(t, e.Setup())
require.NoError(t, e.Run(t.Context(), &task.Call{Task: "local"}))
assert.Contains(t, buff.String(), "Hello from local")
})
t.Run("non-optional include still fails", func(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
e := task.NewExecutor(
task.WithDir("testdata/includes_remote_nonoptional_offline"),
task.WithStdout(&buff),
task.WithStderr(&buff),
task.WithOffline(true),
task.WithRemoteCacheDir(t.TempDir()),
)
err := e.Setup()
require.Error(t, err)
assert.Contains(t, err.Error(), "was not found in the cache")
})
}
func TestIncludeCycle(t *testing.T) {
t.Parallel()

View file

@ -213,6 +213,26 @@ func (r *Reader) include(ctx context.Context, node Node) error {
// 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
}
}
}
return err
}

View file

@ -0,0 +1,10 @@
version: "3"
includes:
remote:
taskfile: https://github.com/go-task/task.git//Taskfile.yml
tasks:
local:
cmds:
- echo "Hello from local"

View file

@ -0,0 +1,11 @@
version: "3"
includes:
remote:
taskfile: https://github.com/go-task/task.git//Taskfile.yml
optional: true
tasks:
local:
cmds:
- echo "Hello from local"