fix: reject include without taskfile or dir (#2892)

Co-authored-by: Valentin Maerten <maerten.valentin@gmail.com>
This commit is contained in:
qingyingliu
2026-07-01 12:48:47 -07:00
committed by GitHub
parent b5f671e098
commit b5861b1e27
4 changed files with 41 additions and 3 deletions

View File

@@ -1240,6 +1240,25 @@ func TestIncludesIncorrect(t *testing.T) {
assert.Contains(t, err.Error(), "Failed to parse testdata/includes_incorrect/incomplete.yml:", err.Error())
}
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.Contains(t, err.Error(), "include must specify taskfile or dir")
assert.NotContains(t, err.Error(), "include cycle detected")
}
func TestIncludesEmptyMain(t *testing.T) {
t.Parallel()

View File

@@ -2,6 +2,7 @@ package ast
import (
"iter"
"strings"
"sync"
"github.com/elliotchance/orderedmap/v3"
@@ -171,6 +172,9 @@ func (include *Include) UnmarshalYAML(node *yaml.Node) error {
if err := node.Decode(&includedTaskfile); err != nil {
return errors.NewTaskfileDecodeError(err, node)
}
if strings.TrimSpace(includedTaskfile.Taskfile) == "" && strings.TrimSpace(includedTaskfile.Dir) == "" {
return errors.NewTaskfileDecodeError(nil, node).WithMessage("include must specify taskfile or dir")
}
include.Taskfile = includedTaskfile.Taskfile
include.Dir = includedTaskfile.Dir
include.Optional = includedTaskfile.Optional

View File

@@ -0,0 +1,5 @@
version: '3'
includes:
GOBIN:
sh: echo $(go env GOPATH)/bin

View File

@@ -718,11 +718,13 @@
"properties": {
"taskfile": {
"description": "The path for the Taskfile or directory to be included. If a directory, Task will look for files named `Taskfile.yml` or `Taskfile.yaml` inside that directory. If a relative path, resolved relative to the directory containing the including Taskfile.",
"type": "string"
"type": "string",
"minLength": 1
},
"dir": {
"description": "The working directory of the included tasks when run.",
"type": "string"
"type": "string",
"minLength": 1
},
"optional": {
"description": "If `true`, no errors will be thrown if the specified file does not exist.",
@@ -758,7 +760,15 @@
"description": "The checksum of the file you expect to include. If the checksum does not match, the file will not be included.",
"type": "string"
}
}
},
"anyOf": [
{
"required": ["taskfile"]
},
{
"required": ["dir"]
}
]
}
]
}