Skip to content

Commit b5861b1

Browse files
Lewin671vmaerten
andauthored
fix: reject include without taskfile or dir (#2892)
Co-authored-by: Valentin Maerten <maerten.valentin@gmail.com>
1 parent b5f671e commit b5861b1

4 files changed

Lines changed: 41 additions & 3 deletions

File tree

task_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,6 +1240,25 @@ func TestIncludesIncorrect(t *testing.T) {
12401240
assert.Contains(t, err.Error(), "Failed to parse testdata/includes_incorrect/incomplete.yml:", err.Error())
12411241
}
12421242

1243+
func TestIncludesMissingTaskfile(t *testing.T) {
1244+
t.Parallel()
1245+
1246+
const dir = "testdata/includes_missing_taskfile"
1247+
1248+
var buff bytes.Buffer
1249+
e := task.NewExecutor(
1250+
task.WithDir(dir),
1251+
task.WithStdout(&buff),
1252+
task.WithStderr(&buff),
1253+
task.WithSilent(true),
1254+
)
1255+
1256+
err := e.Setup()
1257+
require.Error(t, err)
1258+
assert.Contains(t, err.Error(), "include must specify taskfile or dir")
1259+
assert.NotContains(t, err.Error(), "include cycle detected")
1260+
}
1261+
12431262
func TestIncludesEmptyMain(t *testing.T) {
12441263
t.Parallel()
12451264

taskfile/ast/include.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ast
22

33
import (
44
"iter"
5+
"strings"
56
"sync"
67

78
"github.com/elliotchance/orderedmap/v3"
@@ -171,6 +172,9 @@ func (include *Include) UnmarshalYAML(node *yaml.Node) error {
171172
if err := node.Decode(&includedTaskfile); err != nil {
172173
return errors.NewTaskfileDecodeError(err, node)
173174
}
175+
if strings.TrimSpace(includedTaskfile.Taskfile) == "" && strings.TrimSpace(includedTaskfile.Dir) == "" {
176+
return errors.NewTaskfileDecodeError(nil, node).WithMessage("include must specify taskfile or dir")
177+
}
174178
include.Taskfile = includedTaskfile.Taskfile
175179
include.Dir = includedTaskfile.Dir
176180
include.Optional = includedTaskfile.Optional
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version: '3'
2+
3+
includes:
4+
GOBIN:
5+
sh: echo $(go env GOPATH)/bin

website/src/public/schema.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,13 @@
718718
"properties": {
719719
"taskfile": {
720720
"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.",
721-
"type": "string"
721+
"type": "string",
722+
"minLength": 1
722723
},
723724
"dir": {
724725
"description": "The working directory of the included tasks when run.",
725-
"type": "string"
726+
"type": "string",
727+
"minLength": 1
726728
},
727729
"optional": {
728730
"description": "If `true`, no errors will be thrown if the specified file does not exist.",
@@ -758,7 +760,15 @@
758760
"description": "The checksum of the file you expect to include. If the checksum does not match, the file will not be included.",
759761
"type": "string"
760762
}
761-
}
763+
},
764+
"anyOf": [
765+
{
766+
"required": ["taskfile"]
767+
},
768+
{
769+
"required": ["dir"]
770+
}
771+
]
762772
}
763773
]
764774
}

0 commit comments

Comments
 (0)