diff --git a/executor_test.go b/executor_test.go index d963fa6d3f..e9f7bd87ff 100644 --- a/executor_test.go +++ b/executor_test.go @@ -872,7 +872,9 @@ func TestForCmds(t *testing.T) { {name: "loop-vars"}, {name: "loop-vars-sh"}, {name: "loop-task"}, + {name: "loop-task-ref"}, {name: "loop-task-as"}, + {name: "loop-task-ref-as"}, {name: "loop-different-tasks"}, } @@ -915,7 +917,9 @@ func TestForDeps(t *testing.T) { {name: "loop-vars"}, {name: "loop-vars-sh"}, {name: "loop-task"}, + {name: "loop-task-ref"}, {name: "loop-task-as"}, + {name: "loop-task-ref-as"}, {name: "loop-different-tasks"}, } diff --git a/internal/templater/templater.go b/internal/templater/templater.go index c37bae3626..0f0608770e 100644 --- a/internal/templater/templater.go +++ b/internal/templater/templater.go @@ -32,6 +32,10 @@ func (r *Cache) Err() error { } func ResolveRef(ref string, cache *Cache) any { + return ResolveRefWithExtra(ref, cache, nil) +} + +func ResolveRefWithExtra(ref string, cache *Cache, extra map[string]any) any { // If there is already an error, do nothing if cache.err != nil { return nil @@ -42,15 +46,21 @@ func ResolveRef(ref string, cache *Cache) any { cache.cacheMap = cache.Vars.ToCacheMap() } + data := cache.cacheMap + if extra != nil { + data = maps.Clone(cache.cacheMap) + maps.Copy(data, extra) + } + if ref == "." { - return cache.cacheMap + return data } t, err := template.New("resolver").Funcs(templateFuncs).Parse(fmt.Sprintf("{{%s}}", ref)) if err != nil { cache.err = err return nil } - val, err := t.Resolve(cache.cacheMap) + val, err := t.Resolve(data) if err != nil { cache.err = err return nil @@ -132,7 +142,7 @@ func ReplaceVar(v ast.Var, cache *Cache) ast.Var { func ReplaceVarWithExtra(v ast.Var, cache *Cache, extra map[string]any) ast.Var { if v.Ref != "" { - return ast.Var{Value: ResolveRef(v.Ref, cache)} + return ast.Var{Value: ResolveRefWithExtra(v.Ref, cache, extra)} } return ast.Var{ Value: ReplaceWithExtra(v.Value, cache, extra), diff --git a/testdata/for/cmds/Taskfile.yml b/testdata/for/cmds/Taskfile.yml index e9a95234be..f53acacc0a 100644 --- a/testdata/for/cmds/Taskfile.yml +++ b/testdata/for/cmds/Taskfile.yml @@ -105,6 +105,18 @@ tasks: vars: FILE: "{{.ITEM}}" + # Loop over another task using ref to the default loop variable + loop-task-ref: + vars: + FOO: foo.txt bar.txt + cmds: + - for: + var: FOO + task: looped-task + vars: + FILE: + ref: .ITEM + # Loop over another task with the variable named differently loop-task-as: vars: @@ -117,6 +129,19 @@ tasks: vars: FILE: "{{.FILE}}" + # Loop over another task using ref to a named loop variable + loop-task-ref-as: + vars: + FOO: foo.txt bar.txt + cmds: + - for: + var: FOO + as: FILE + task: looped-task + vars: + FILE: + ref: .FILE + # Loop over different tasks using the variable loop-different-tasks: vars: diff --git a/testdata/for/cmds/testdata/TestForCmds-loop-task-ref-as.golden b/testdata/for/cmds/testdata/TestForCmds-loop-task-ref-as.golden new file mode 100644 index 0000000000..3bd1f0e297 --- /dev/null +++ b/testdata/for/cmds/testdata/TestForCmds-loop-task-ref-as.golden @@ -0,0 +1,2 @@ +foo +bar diff --git a/testdata/for/cmds/testdata/TestForCmds-loop-task-ref.golden b/testdata/for/cmds/testdata/TestForCmds-loop-task-ref.golden new file mode 100644 index 0000000000..3bd1f0e297 --- /dev/null +++ b/testdata/for/cmds/testdata/TestForCmds-loop-task-ref.golden @@ -0,0 +1,2 @@ +foo +bar diff --git a/testdata/for/deps/Taskfile.yml b/testdata/for/deps/Taskfile.yml index 66567cc662..f4eb919e0a 100644 --- a/testdata/for/deps/Taskfile.yml +++ b/testdata/for/deps/Taskfile.yml @@ -125,6 +125,18 @@ tasks: vars: FILE: "{{.ITEM}}" + # Loop over another task using ref to the default loop variable + loop-task-ref: + vars: + FOO: foo.txt bar.txt + deps: + - for: + var: FOO + task: looped-task + vars: + FILE: + ref: .ITEM + # Loop over another task with the variable named differently loop-task-as: vars: @@ -137,6 +149,19 @@ tasks: vars: FILE: "{{.FILE}}" + # Loop over another task using ref to a named loop variable + loop-task-ref-as: + vars: + FOO: foo.txt bar.txt + deps: + - for: + var: FOO + as: FILE + task: looped-task + vars: + FILE: + ref: .FILE + # Loop over different tasks using the variable loop-different-tasks: vars: diff --git a/testdata/for/deps/testdata/TestForDeps-loop-task-ref-as.golden b/testdata/for/deps/testdata/TestForDeps-loop-task-ref-as.golden new file mode 100644 index 0000000000..128976523b --- /dev/null +++ b/testdata/for/deps/testdata/TestForDeps-loop-task-ref-as.golden @@ -0,0 +1,2 @@ +bar +foo diff --git a/testdata/for/deps/testdata/TestForDeps-loop-task-ref.golden b/testdata/for/deps/testdata/TestForDeps-loop-task-ref.golden new file mode 100644 index 0000000000..128976523b --- /dev/null +++ b/testdata/for/deps/testdata/TestForDeps-loop-task-ref.golden @@ -0,0 +1,2 @@ +bar +foo