diff --git a/compiler.go b/compiler.go index 2c2e56f632..f774b52ed6 100644 --- a/compiler.go +++ b/compiler.go @@ -29,10 +29,15 @@ type Compiler struct { Logger *logger.Logger - dynamicCache map[string]string + dynamicCache map[dynamicCacheKey]string muDynamicCache sync.Mutex } +type dynamicCacheKey struct { + dir string + sh string +} + func (c *Compiler) GetTaskfileVariables() (*ast.Vars, error) { return c.getVariables(nil, nil, true) } @@ -156,9 +161,12 @@ func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string, } if c.dynamicCache == nil { - c.dynamicCache = make(map[string]string, 30) + c.dynamicCache = make(map[dynamicCacheKey]string, 30) } - if result, ok := c.dynamicCache[*v.Sh]; ok { + + key := dynamicCacheKey{dir, *v.Sh} + + if result, ok := c.dynamicCache[key]; ok { return result, nil } @@ -184,7 +192,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[key] = result // Never print the resolved value of a secret variable, even in verbose mode logResult := result if v.Secret { diff --git a/task_test.go b/task_test.go index b56930e77e..1135c46a4a 100644 --- a/task_test.go +++ b/task_test.go @@ -1052,6 +1052,8 @@ func TestIncludes(t *testing.T) { "./module2/included_directory_with_dir.txt": "included_directory_with_dir", "./module2/included_taskfile_with_dir.txt": "included_taskfile_with_dir", "os_include.txt": "os", + "result.txt": "includes", + "common/result.txt": "common", }, } t.Run("", func(t *testing.T) { diff --git a/testdata/includes/Taskfile.yml b/testdata/includes/Taskfile.yml index 8ed9e416de..0c9512a7b1 100644 --- a/testdata/includes/Taskfile.yml +++ b/testdata/includes/Taskfile.yml @@ -4,16 +4,22 @@ includes: included: ./included included_taskfile: ./Taskfile2.yml included_without_dir: - taskfile: ./module1 + taskfile: ./module1 included_taskfile_without_dir: - taskfile: ./module1/Taskfile.yml + taskfile: ./module1/Taskfile.yml included_with_dir: - taskfile: ./module2 - dir: ./module2 + taskfile: ./module2 + dir: ./module2 included_taskfile_with_dir: - taskfile: ./module2/Taskfile.yml - dir: ./module2 + taskfile: ./module2/Taskfile.yml + dir: ./module2 included_os: ./Taskfile_{{OS}}.yml + include_common_task1: + taskfile: ./common/Taskfile.yml + dir: . + include_common_task2: + taskfile: ./common/Taskfile.yml + dir: ./common tasks: default: @@ -26,6 +32,8 @@ tasks: - task: included_with_dir:gen_file - task: included_taskfile_with_dir:gen_dir - task: included_os:gen + - task: include_common_task1:show + - task: include_common_task2:show gen: cmds: diff --git a/testdata/includes/common/Taskfile.yml b/testdata/includes/common/Taskfile.yml new file mode 100644 index 0000000000..2be0367600 --- /dev/null +++ b/testdata/includes/common/Taskfile.yml @@ -0,0 +1,10 @@ +version: '3' + +tasks: + show: + vars: + CURRENT_DIR: + sh: basename "$(pwd)" + + cmds: + - 'echo "{{.CURRENT_DIR}}" > result.txt'