diff --git a/compiler.go b/compiler.go index 753053abaf..8cbbdbff43 100644 --- a/compiler.go +++ b/compiler.go @@ -156,15 +156,18 @@ func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string, if c.dynamicCache == nil { c.dynamicCache = make(map[string]string, 30) } - if result, ok := c.dynamicCache[*v.Sh]; ok { - return result, nil - } // NOTE(@andreynering): If a var have a specific dir, use this instead if v.Dir != "" { dir = v.Dir } + cacheKey := *v.Sh + "-" + dir + + if result, ok := c.dynamicCache[cacheKey]; ok { + return result, nil + } + var stdout bytes.Buffer opts := &execext.RunCommandOptions{ Command: *v.Sh, @@ -182,7 +185,7 @@ func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string, result := strings.TrimSuffix(stdout.String(), "\r\n") result = strings.TrimSuffix(result, "\n") - c.dynamicCache[*v.Sh] = result + c.dynamicCache[cacheKey] = result c.Logger.VerboseErrf(logger.Magenta, "task: dynamic variable: %q result: %q\n", *v.Sh, result) return result, nil diff --git a/task_test.go b/task_test.go index 0a2c982122..b0ae31c9a4 100644 --- a/task_test.go +++ b/task_test.go @@ -3274,6 +3274,25 @@ func TestReference(t *testing.T) { } } +func TestResolveShellVarsInSubdirs(t *testing.T) { + t.Parallel() + + var buff bytes.Buffer + e := task.NewExecutor( + task.ExecutorWithDir("testdata/includes_shell_vars"), + task.ExecutorWithStdout(&buff), + task.ExecutorWithStderr(&buff), + task.ExecutorWithSilent(true), + task.ExecutorWithForce(true), + ) + require.NoError(t, e.Setup()) + require.NoError(t, e.Run(context.Background(), &task.Call{Task: "all"})) + + output := buff.String() + require.Contains(t, output, `[s1:all] ./a`) + require.Contains(t, output, `[s2:all] ./b`) +} + func TestVarInheritance(t *testing.T) { enableExperimentForTest(t, &experiments.EnvPrecedence, 1) tests := []struct { diff --git a/testdata/includes_shell_vars/Taskfile.yml b/testdata/includes_shell_vars/Taskfile.yml new file mode 100644 index 0000000000..8882acb6b5 --- /dev/null +++ b/testdata/includes_shell_vars/Taskfile.yml @@ -0,0 +1,20 @@ +version: '3' + +run: when_changed +silent: true +output: prefixed + +includes: + s1: + taskfile: sub1 + dir: sub1 + s2: + taskfile: sub2 + dir: sub2 + +tasks: + all: + deps: + - s1:all + - s2:all + diff --git a/testdata/includes_shell_vars/sub1/Taskfile.yml b/testdata/includes_shell_vars/sub1/Taskfile.yml new file mode 100644 index 0000000000..b7363fa400 --- /dev/null +++ b/testdata/includes_shell_vars/sub1/Taskfile.yml @@ -0,0 +1,10 @@ +version: '3' + +vars: + files: + sh: "find ." + +tasks: + all: + cmds: + - cmd: 'echo "{{.files}}"' diff --git a/testdata/includes_shell_vars/sub1/a b/testdata/includes_shell_vars/sub1/a new file mode 100644 index 0000000000..43d5a8ed6e --- /dev/null +++ b/testdata/includes_shell_vars/sub1/a @@ -0,0 +1 @@ +AAA diff --git a/testdata/includes_shell_vars/sub2/Taskfile.yml b/testdata/includes_shell_vars/sub2/Taskfile.yml new file mode 100644 index 0000000000..b7363fa400 --- /dev/null +++ b/testdata/includes_shell_vars/sub2/Taskfile.yml @@ -0,0 +1,10 @@ +version: '3' + +vars: + files: + sh: "find ." + +tasks: + all: + cmds: + - cmd: 'echo "{{.files}}"' diff --git a/testdata/includes_shell_vars/sub2/b b/testdata/includes_shell_vars/sub2/b new file mode 100644 index 0000000000..ba629238ca --- /dev/null +++ b/testdata/includes_shell_vars/sub2/b @@ -0,0 +1 @@ +BBB