Skip to content

fix: dynamic variable caching does not take into account current dir #2160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
19 changes: 19 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 20 additions & 0 deletions testdata/includes_shell_vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -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

10 changes: 10 additions & 0 deletions testdata/includes_shell_vars/sub1/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'

vars:
files:
sh: "find ."

tasks:
all:
cmds:
- cmd: 'echo "{{.files}}"'
1 change: 1 addition & 0 deletions testdata/includes_shell_vars/sub1/a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
AAA
10 changes: 10 additions & 0 deletions testdata/includes_shell_vars/sub2/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3'

vars:
files:
sh: "find ."

tasks:
all:
cmds:
- cmd: 'echo "{{.files}}"'
1 change: 1 addition & 0 deletions testdata/includes_shell_vars/sub2/b
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BBB