From 57998ba992e6676d0a7eba307daacaf7fd080151 Mon Sep 17 00:00:00 2001 From: kjasn Date: Wed, 22 Jul 2026 20:08:05 +0800 Subject: [PATCH 1/2] fix: cache key holds dir and sh --- compiler.go | 16 ++++++++++++---- task_test.go | 2 ++ testdata/includes/Taskfile.yml | 20 ++++++++++++++------ testdata/includes/common/Taskfile.yml | 10 ++++++++++ 4 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 testdata/includes/common/Taskfile.yml 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..c6ab715dac 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", + "output1/result.txt": "output1", + "output2/result.txt": "output2", }, } t.Run("", func(t *testing.T) { diff --git a/testdata/includes/Taskfile.yml b/testdata/includes/Taskfile.yml index 8ed9e416de..21c00f7b88 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: ./output1 + include_common_task2: + taskfile: ./common/Taskfile.yml + dir: ./output2 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' From 7e343ca0cf4c0f18e1b04bf782d25ea2daceac2a Mon Sep 17 00:00:00 2001 From: kjasn Date: Wed, 22 Jul 2026 20:35:33 +0800 Subject: [PATCH 2/2] fix: use the existing dir in tests --- task_test.go | 4 ++-- testdata/includes/Taskfile.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/task_test.go b/task_test.go index c6ab715dac..1135c46a4a 100644 --- a/task_test.go +++ b/task_test.go @@ -1052,8 +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", - "output1/result.txt": "output1", - "output2/result.txt": "output2", + "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 21c00f7b88..0c9512a7b1 100644 --- a/testdata/includes/Taskfile.yml +++ b/testdata/includes/Taskfile.yml @@ -16,10 +16,10 @@ includes: included_os: ./Taskfile_{{OS}}.yml include_common_task1: taskfile: ./common/Taskfile.yml - dir: ./output1 + dir: . include_common_task2: taskfile: ./common/Taskfile.yml - dir: ./output2 + dir: ./common tasks: default: