Skip to content

Commit 94f82cb

Browse files
authored
fix: make task failure errors include stack of running tasks (#2286)
Previously if a task was run as a dependency of another task, the error message simply reported something like: exit status 1 It is desirable instead to name the root task and all child tasks in the tree to the failing task. After this PR, the error message will read: task: Failed to run task "root": task: Failed to run task "failing-task": exit status 1
1 parent b14318e commit 94f82cb

14 files changed

+39
-15
lines changed

errors/errors_task.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ func (err *TaskRunError) TaskExitCode() int {
5454
return err.Code()
5555
}
5656

57+
func (err *TaskRunError) Unwrap() error {
58+
return err.Err
59+
}
60+
5761
// TaskInternalError when the user attempts to invoke a task that is internal.
5862
type TaskInternalError struct {
5963
TaskName string

executor_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,15 @@ func TestLabel(t *testing.T) {
665665
),
666666
WithTask("foo"),
667667
)
668+
669+
NewExecutorTest(t,
670+
WithName("label in error"),
671+
WithExecutorOptions(
672+
task.WithDir("testdata/label_error"),
673+
),
674+
WithTask("foo"),
675+
WithRunError(),
676+
)
668677
}
669678

670679
func TestPromptInSummary(t *testing.T) {

task.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
150150
release := e.acquireConcurrencyLimit()
151151
defer release()
152152

153-
return e.startExecution(ctx, t, func(ctx context.Context) error {
153+
if err = e.startExecution(ctx, t, func(ctx context.Context) error {
154154
e.Logger.VerboseErrf(logger.Magenta, "task: %q started\n", call.Task)
155155
if err := e.runDeps(ctx, t); err != nil {
156156
return err
@@ -228,16 +228,16 @@ func (e *Executor) RunTask(ctx context.Context, call *Call) error {
228228
deferredExitCode = uint8(exitCode)
229229
}
230230

231-
if call.Indirect {
232-
return err
233-
}
234-
235-
return &errors.TaskRunError{TaskName: t.Task, Err: err}
231+
return err
236232
}
237233
}
238234
e.Logger.VerboseErrf(logger.Magenta, "task: %q finished\n", call.Task)
239235
return nil
240-
})
236+
}); err != nil {
237+
return &errors.TaskRunError{TaskName: t.Name(), Err: err}
238+
}
239+
240+
return nil
241241
}
242242

243243
func (e *Executor) mkdir(t *ast.Task) error {

task_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,9 @@ func TestCyclicDep(t *testing.T) {
569569
task.WithStderr(io.Discard),
570570
)
571571
require.NoError(t, e.Setup())
572-
assert.IsType(t, &errors.TaskCalledTooManyTimesError{}, e.Run(t.Context(), &task.Call{Task: "task-1"}))
572+
err := e.Run(t.Context(), &task.Call{Task: "task-1"})
573+
var taskCalledTooManyTimesError *errors.TaskCalledTooManyTimesError
574+
assert.ErrorAs(t, err, &taskCalledTooManyTimesError)
573575
}
574576

575577
func TestTaskVersion(t *testing.T) {

testdata/label_error/Taskfile.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: '3'
2+
3+
tasks:
4+
foo:
5+
label: "foobar"
6+
cmds:
7+
- "false"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
task: Failed to run task "foobar": exit status 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
task: [foobar] false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
task: precondition not met
1+
task: Failed to run task "impossible": task: precondition not met
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
task: Failed to run task "executes_failing_task_as_cmd": task: precondition not met
1+
task: Failed to run task "executes_failing_task_as_cmd": task: Failed to run task "impossible": task: precondition not met
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
task: precondition not met
1+
task: Failed to run task "depends_on_impossible": task: Failed to run task "impossible": task: precondition not met

0 commit comments

Comments
 (0)