fix: skip optional remote includes on offline cache miss (#3081)

This commit is contained in:
Tide 2026-11-06 04:19:18 +08:00 committed by GitHub
parent 218dc43ad7
commit 7e4cccde96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 101 additions and 0 deletions

View file

@ -1323,6 +1323,56 @@ func TestIncludesRemote(t *testing.T) {
}
}
func TestIncludesRemoteOptionalOfflineCacheMiss(t *testing.T) {
t.Parallel()
tests := []struct {
name string
dir string
wantErr string
}{
{
name: "optional include is skipped",
dir: "testdata/includes_remote_optional_offline",
},
{
name: "non-optional include still fails",
dir: "testdata/includes_remote_nonoptional_offline",
wantErr: "was not found in the cache",
},
{
name: "non-optional nested include still fails",
dir: "testdata/includes_remote_optional_parent_offline",
wantErr: "was not found in the cache",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var buff bytes.Buffer
e := task.NewExecutor(
task.WithDir(tt.dir),
task.WithStdout(&buff),
task.WithStderr(&buff),
task.WithOffline(true),
task.WithRemoteCacheDir(t.TempDir()),
)
err := e.Setup()
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
require.NoError(t, e.Run(t.Context(), &task.Call{Task: "local"}))
assert.Contains(t, buff.String(), "Hello from local")
})
}
}
func TestIncludeCycle(t *testing.T) {
t.Parallel()

View file

@ -211,6 +211,15 @@ func (r *Reader) include(ctx context.Context, node Node) error {
return err
}
// Checked before recursing so a nested include's cache miss isn't mistaken for this one's.
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,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"

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"