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
19 changes: 19 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,25 @@ func TestIncludeCycle(t *testing.T) {
assert.Contains(t, err.Error(), "task: include cycle detected between")
}

func TestIncludesMissingTaskfile(t *testing.T) {
t.Parallel()

const dir = "testdata/includes_missing_taskfile"

var buff bytes.Buffer
e := task.NewExecutor(
task.WithDir(dir),
task.WithStdout(&buff),
task.WithStderr(&buff),
task.WithSilent(true),
)

err := e.Setup()
require.Error(t, err)
assert.NotContains(t, err.Error(), "include cycle detected", "should not report a cycle error for a missing taskfile field")
assert.Contains(t, err.Error(), "missing the required \"taskfile\" field")
}

func TestIncludesIncorrect(t *testing.T) {
t.Parallel()

Expand Down
21 changes: 21 additions & 0 deletions taskfile/ast/include.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ast

import (
"fmt"
"iter"
"sync"

Expand Down Expand Up @@ -133,6 +134,26 @@ func (includes *Includes) UnmarshalYAML(node *yaml.Node) error {
return errors.NewTaskfileDecodeError(err, node)
}

// Validate that the include has a Taskfile path specified.
// If it doesn't, suggest the user may have meant to use 'vars:' instead
// of 'includes:' — a common copy-paste mistake that otherwise produces
// a misleading "include cycle detected" error downstream.
if v.Taskfile == "" {
rawKeys := []string{}
for j := 0; j < len(valueNode.Content); j += 2 {
rawKeys = append(rawKeys, valueNode.Content[j].Value)
}
keysStr := ""
if len(rawKeys) > 0 {
keysStr = fmt.Sprintf(" (unexpected keys: %v)", rawKeys)
}
err := fmt.Errorf(
"include \"%s\" is missing the required \"taskfile\" field%s; did you mean to use \"vars:\" instead?",
keyNode.Value, keysStr,
)
return errors.NewTaskfileDecodeError(err, valueNode)
}

// Set the include namespace
v.Namespace = keyNode.Value

Expand Down
9 changes: 9 additions & 0 deletions testdata/includes_missing_taskfile/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: '3'

includes:
common: ./TaskfileCommon.yml

tasks:
test:
cmds:
- task: common:test
10 changes: 10 additions & 0 deletions testdata/includes_missing_taskfile/TaskfileCommon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'

includes:
GOBIN:
sh: echo $(go env GOPATH)/bin

tasks:
test:
cmds:
- "{{.GOBIN}}/richgo test ./..."