diff --git a/compiler.go b/compiler.go index 311fd58423..68a586bca6 100644 --- a/compiler.go +++ b/compiler.go @@ -115,6 +115,13 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (* } } if t != nil { + if call != nil { + if match, ok := call.Vars.Get("MATCH"); ok { + if err := rangeFunc("MATCH", match); err != nil { + return nil, err + } + } + } for k, v := range t.IncludeVars.All() { if err := rangeFunc(k, v); err != nil { return nil, err diff --git a/task_test.go b/task_test.go index 9d54af9740..43b6dc4d53 100644 --- a/task_test.go +++ b/task_test.go @@ -1271,6 +1271,23 @@ func TestIncludesInterpolation(t *testing.T) { // nolint:paralleltest // cannot } } +func TestIncludeWildcardVarsExposeMatch(t *testing.T) { + t.Parallel() + + var buff bytes.Buffer + e := task.NewExecutor( + task.WithDir("testdata/includes_wildcard_vars"), + task.WithSilent(true), + task.WithStdout(&buff), + task.WithStderr(&buff), + ) + require.NoError(t, e.Setup()) + + err := e.Run(t.Context(), &task.Call{Task: "stack:prod:show"}) + require.NoError(t, err) + assert.Equal(t, "prod\n", buff.String()) +} + func TestIncludesWithExclude(t *testing.T) { t.Parallel() diff --git a/testdata/includes_wildcard_vars/Taskfile.stack.yml b/testdata/includes_wildcard_vars/Taskfile.stack.yml new file mode 100644 index 0000000000..19994814c5 --- /dev/null +++ b/testdata/includes_wildcard_vars/Taskfile.stack.yml @@ -0,0 +1,6 @@ +version: '3' + +tasks: + show: + cmds: + - echo '{{.ENV}}' diff --git a/testdata/includes_wildcard_vars/Taskfile.yml b/testdata/includes_wildcard_vars/Taskfile.yml new file mode 100644 index 0000000000..5ed3e073f5 --- /dev/null +++ b/testdata/includes_wildcard_vars/Taskfile.yml @@ -0,0 +1,7 @@ +version: '3' + +includes: + 'stack:*': + taskfile: ./Taskfile.stack.yml + vars: + ENV: '{{index .MATCH 0}}'