fix(taskfile): find decode errors wrapped by yaml v4

yaml v4 collects errors returned by UnmarshalYAML hooks into
*yaml.LoadErrors, which errors.As cannot unwrap. A Taskfile with a
wrongly typed field lost its snippet and file location, and exited
with code 109 instead of 102.
This commit is contained in:
Valentin Maerten 2026-11-09 11:42:26 +01:00
parent 7539a34325
commit 4571284f62
No known key found for this signature in database
GPG key ID: 2F8E54DDF815C341
3 changed files with 39 additions and 4 deletions

View file

@ -25,7 +25,7 @@ type (
func NewTaskfileDecodeError(err error, node *yaml.Node) *TaskfileDecodeError {
// If the error is already a DecodeError, return it
if taskfileInvalidErr, ok := errors.AsType[*TaskfileDecodeError](err); ok {
if taskfileInvalidErr, ok := AsTaskfileDecodeError(err); ok {
return taskfileInvalidErr
}
return &TaskfileDecodeError{
@ -36,6 +36,23 @@ func NewTaskfileDecodeError(err error, node *yaml.Node) *TaskfileDecodeError {
}
}
// AsTaskfileDecodeError finds a TaskfileDecodeError in err's chain. yaml v4
// collects errors returned by UnmarshalYAML hooks into *yaml.LoadErrors,
// which errors.As cannot see through.
func AsTaskfileDecodeError(err error) (*TaskfileDecodeError, bool) {
if decodeErr, ok := errors.AsType[*TaskfileDecodeError](err); ok {
return decodeErr, true
}
if loadErrs, ok := errors.AsType[*yaml.LoadErrors](err); ok {
for _, loadErr := range loadErrs.Errors {
if decodeErr, ok := errors.AsType[*TaskfileDecodeError](loadErr); ok {
return decodeErr, true
}
}
}
return nil, false
}
func (err *TaskfileDecodeError) Error() string {
buf := &bytes.Buffer{}
@ -43,8 +60,9 @@ func (err *TaskfileDecodeError) Error() string {
if err.Message != "" {
fmt.Fprintln(buf, color.RedString("err: %s", err.Message))
} else {
// Extract the errors from the TypeError
if te, ok := errors.AsType[*yaml.TypeError](err.Err); ok {
// Extract the errors from the TypeError. LoadErrors synthesizes it with
// the compact "line N: message" entries, which LoadError.Error() lacks.
if te, ok := errors.AsType[*yaml.TypeError](err.Err); ok { //nolint:staticcheck // see above
if len(te.Errors) > 1 {
fmt.Fprintln(buf, color.RedString("errs:"))
for _, message := range te.Errors {

View file

@ -8,6 +8,7 @@ import (
"github.com/stretchr/testify/require"
"go.yaml.in/yaml/v4"
"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/taskfile/ast"
)
@ -175,3 +176,19 @@ func TestTimeoutParseError(t *testing.T) {
})
}
}
func TestTaskfileDecodeErrorLocation(t *testing.T) {
t.Parallel()
const content = `version: '3'
tasks:
a:
cmds: 12
`
var tf ast.Taskfile
err := yaml.Unmarshal([]byte(content), &tf)
decodeErr, ok := errors.AsTaskfileDecodeError(err)
require.True(t, ok, "expected a TaskfileDecodeError, got %v", err)
assert.Equal(t, 4, decodeErr.Line)
assert.Equal(t, 5, decodeErr.Column)
}

View file

@ -270,7 +270,7 @@ func (r *Reader) readNode(ctx context.Context, node Node) (*ast.Taskfile, error)
var tf ast.Taskfile
if err := yaml.Unmarshal(b, &tf); err != nil {
// Decode the taskfile and add the file info the any errors
if taskfileDecodeErr, ok := errors.AsType[*errors.TaskfileDecodeError](err); ok {
if taskfileDecodeErr, ok := errors.AsTaskfileDecodeError(err); ok {
snippet := NewSnippet(b,
WithLine(taskfileDecodeErr.Line),
WithColumn(taskfileDecodeErr.Column),