Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exit codes for indirect tasks #1270

Merged
merged 2 commits into from
Jul 25, 2023
Merged

Conversation

pd93
Copy link
Member

@pd93 pd93 commented Jul 19, 2023

Fixes #1266

This bug is happening because when we run a command, we are always wrapping the interp.exitStatus error and returning a TaskRunError:

task/task.go

Lines 232 to 243 in 7e7a016

if err := e.runCommand(ctx, t, call, i); err != nil {
if err2 := e.statusOnError(t); err2 != nil {
e.Logger.VerboseErrf(logger.Yellow, "task: error cleaning status on error: %v\n", err2)
}
if execext.IsExitError(err) && t.IgnoreError {
e.Logger.VerboseErrf(logger.Yellow, "task: task error ignored: %v\n", err)
continue
}
return &errors.TaskRunError{TaskName: t.Task, Err: err}
}

This works fine at the top-level (for direct calls) because we can simply unwrap the error in Task's main function:

task/cmd/task/task.go

Lines 84 to 87 in 7e7a016

if err, ok := err.(*errors.TaskRunError); ok && flags.exitCode {
l.Errf(logger.Red, "%v\n", err)
os.Exit(err.TaskExitCode())
}

However, when we run a command indirectly via another task, we are wrapping the TaskRunError in another TaskRunError. This problem is recursive and means that we can't effectively unwrap the error to get the exit code out.

The solution to this is simply to check if the command is being called directly and if not, we return the interp.exitStatus directly instead of wrapping it up. This means that we only wrap once on the top-level error and unwrapping in the main function just works.

I've also updated the tests to cover this case.

@pd93 pd93 added the type: bug Something not working as intended. label Jul 19, 2023
@andreynering
Copy link
Member

Thanks!

@andreynering andreynering merged commit 3908c05 into main Jul 25, 2023
11 checks passed
@andreynering andreynering deleted the fix-exit-code-for-indirect-tasks branch July 25, 2023 00:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something not working as intended.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

--exit-code doesn't work for nested tasks
2 participants