Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
20 changes: 20 additions & 0 deletions taskfile/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
10 changes: 10 additions & 0 deletions testdata/includes_remote_nonoptional_offline/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions testdata/includes_remote_optional_offline/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -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"