diff --git a/executor_test.go b/executor_test.go index e0ce4e2786..e502fe7a0f 100644 --- a/executor_test.go +++ b/executor_test.go @@ -936,6 +936,7 @@ func TestForCmds(t *testing.T) { }, {name: "loop-sources"}, {name: "loop-sources-glob"}, + {name: "sources-variable"}, {name: "loop-generates"}, {name: "loop-generates-glob"}, {name: "loop-vars"}, diff --git a/testdata/for/cmds/Taskfile.yml b/testdata/for/cmds/Taskfile.yml index 589cc57797..f1be06a3e7 100644 --- a/testdata/for/cmds/Taskfile.yml +++ b/testdata/for/cmds/Taskfile.yml @@ -152,3 +152,9 @@ tasks: task-3: internal: true cmd: echo "3" + + sources-variable: + sources: + - "*.txt" + cmds: + - echo "{{.sources}}" diff --git a/testdata/for/cmds/testdata/TestForCmds-sources-variable.golden b/testdata/for/cmds/testdata/TestForCmds-sources-variable.golden new file mode 100644 index 0000000000..f732516bbe --- /dev/null +++ b/testdata/for/cmds/testdata/TestForCmds-sources-variable.golden @@ -0,0 +1 @@ +[bar.txt foo.txt] diff --git a/variables.go b/variables.go index c2085bd1ea..1b53f7e739 100644 --- a/variables.go +++ b/variables.go @@ -227,6 +227,20 @@ func (e *Executor) compiledTask(call *Call, evaluateShVars bool) (*ast.Task, err cache.ResetCache() } } + + matchedSources, err := fingerprint.Globs(new.Dir, new.Sources, gitignore) + if err != nil { + return nil, err + } + + for i, m := range matchedSources { + if matchedSources[i], err = filepath.Rel(new.Dir, m); err != nil { + return nil, err + } + } + + vars.Set("sources", ast.Var{Live: matchedSources}) + cache.ResetCache() } if len(origTask.Cmds) > 0 {