fix: pre-check remote cache before recursing into optional includes

This commit is contained in:
Tidehyc 2026-11-02 08:10:40 +08:00
parent 47a73d6ec9
commit e2e82dcdbe
6 changed files with 95 additions and 20 deletions

View file

@ -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) {

View file

@ -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
}

View file

@ -0,0 +1,11 @@
version: "3"
includes:
opt:
taskfile: ./opt.yml
optional: true
tasks:
local:
cmds:
- echo "Hello from local"

View file

@ -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"

View file

@ -0,0 +1,10 @@
version: "3"
includes:
local:
taskfile: ./local.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:
nested:
cmds:
- echo "Hello from nested"