From be5400f9c5e52d5899680b8d7372fc92b8eb1cca Mon Sep 17 00:00:00 2001 From: Viktoras Makauskas Date: Fri, 4 Apr 2025 13:19:20 +0300 Subject: [PATCH 1/6] wip --- task_test.go | 15 ++++++++++++++ testdata/includes_shell_vars/Taskfile.yml | 20 +++++++++++++++++++ .../includes_shell_vars/sub1/Taskfile.yml | 15 ++++++++++++++ testdata/includes_shell_vars/sub1/a | 0 .../includes_shell_vars/sub2/Taskfile.yml | 15 ++++++++++++++ testdata/includes_shell_vars/sub2/b | 0 6 files changed, 65 insertions(+) create mode 100644 testdata/includes_shell_vars/Taskfile.yml create mode 100644 testdata/includes_shell_vars/sub1/Taskfile.yml create mode 100644 testdata/includes_shell_vars/sub1/a create mode 100644 testdata/includes_shell_vars/sub2/Taskfile.yml create mode 100644 testdata/includes_shell_vars/sub2/b diff --git a/task_test.go b/task_test.go index 0a2c982122..13057852a2 100644 --- a/task_test.go +++ b/task_test.go @@ -3274,6 +3274,21 @@ 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"})) +} + 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..b7939fd0d1 --- /dev/null +++ b/testdata/includes_shell_vars/sub1/Taskfile.yml @@ -0,0 +1,15 @@ +version: '3' + +vars: + pwd1: + sh: "pwd" + +tasks: + all: + dir: . + vars: + pwd1_local: + sh: "pwd" + cmds: + - cmd: 'echo "global var: {{ .pwd1 }}"' + - cmd: 'echo "local var: {{ .pwd1_local }}"' diff --git a/testdata/includes_shell_vars/sub1/a b/testdata/includes_shell_vars/sub1/a new file mode 100644 index 0000000000..e69de29bb2 diff --git a/testdata/includes_shell_vars/sub2/Taskfile.yml b/testdata/includes_shell_vars/sub2/Taskfile.yml new file mode 100644 index 0000000000..5be6370ffd --- /dev/null +++ b/testdata/includes_shell_vars/sub2/Taskfile.yml @@ -0,0 +1,15 @@ +version: '3' + +vars: + pwd2: + sh: "pwd" + +tasks: + all: + dir: . + vars: + pwd2_local: + sh: "pwd" + cmds: + - cmd: 'echo "global var: {{ .pwd2 }}"' + - cmd: 'echo "local var: {{ .pwd2_local }}"' diff --git a/testdata/includes_shell_vars/sub2/b b/testdata/includes_shell_vars/sub2/b new file mode 100644 index 0000000000..e69de29bb2 From 6b7d0c1f8cabeefd6dcbf5e1b785588b6684ead6 Mon Sep 17 00:00:00 2001 From: Viktoras Makauskas Date: Fri, 4 Apr 2025 13:57:00 +0300 Subject: [PATCH 2/6] wip --- task_test.go | 6 ++++++ testdata/includes_shell_vars/Taskfile.yml | 4 ++-- testdata/includes_shell_vars/sub1/Taskfile.yml | 12 ++++++------ testdata/includes_shell_vars/sub1/a | 1 + testdata/includes_shell_vars/sub2/Taskfile.yml | 12 ++++++------ testdata/includes_shell_vars/sub2/b | 1 + 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/task_test.go b/task_test.go index 13057852a2..ca5786d5cd 100644 --- a/task_test.go +++ b/task_test.go @@ -3287,6 +3287,12 @@ func TestResolveShellVarsInSubdirs(t *testing.T) { ) require.NoError(t, e.Setup()) require.NoError(t, e.Run(context.Background(), &task.Call{Task: "all"})) + + output := buff.String() + require.Contains(t, output, `[s2:all] global var: BBB`) + require.Contains(t, output, `[s1:all] global var: AAA`) + require.Contains(t, output, `[s1:all] local var: AAA`) + require.Contains(t, output, `[s2:all] local var: BBB`) } func TestVarInheritance(t *testing.T) { diff --git a/testdata/includes_shell_vars/Taskfile.yml b/testdata/includes_shell_vars/Taskfile.yml index 8882acb6b5..654cdaa72b 100644 --- a/testdata/includes_shell_vars/Taskfile.yml +++ b/testdata/includes_shell_vars/Taskfile.yml @@ -15,6 +15,6 @@ includes: tasks: all: deps: - - s1:all - - s2:all + - s1:run + - s2:run diff --git a/testdata/includes_shell_vars/sub1/Taskfile.yml b/testdata/includes_shell_vars/sub1/Taskfile.yml index b7939fd0d1..ff873c40f6 100644 --- a/testdata/includes_shell_vars/sub1/Taskfile.yml +++ b/testdata/includes_shell_vars/sub1/Taskfile.yml @@ -1,15 +1,15 @@ version: '3' vars: + contents1: + sh: "cat a" pwd1: sh: "pwd" tasks: - all: - dir: . + run: vars: - pwd1_local: - sh: "pwd" + contents1_local: + sh: "cat a" cmds: - - cmd: 'echo "global var: {{ .pwd1 }}"' - - cmd: 'echo "local var: {{ .pwd1_local }}"' + - cmd: 'echo "contents: {{ .contents1 }} {{ .contents1_local }} {{ .pwd1 }}"' diff --git a/testdata/includes_shell_vars/sub1/a b/testdata/includes_shell_vars/sub1/a index e69de29bb2..43d5a8ed6e 100644 --- a/testdata/includes_shell_vars/sub1/a +++ 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 index 5be6370ffd..9b263fd8a6 100644 --- a/testdata/includes_shell_vars/sub2/Taskfile.yml +++ b/testdata/includes_shell_vars/sub2/Taskfile.yml @@ -1,15 +1,15 @@ version: '3' vars: + contents2: + sh: "cat b" pwd2: sh: "pwd" tasks: - all: - dir: . + run: vars: - pwd2_local: - sh: "pwd" + contents2_local: + sh: "cat b" cmds: - - cmd: 'echo "global var: {{ .pwd2 }}"' - - cmd: 'echo "local var: {{ .pwd2_local }}"' + - cmd: 'echo "contents: {{ .contents2 }} {{ .contents2_local }} {{ .pwd2 }}"' diff --git a/testdata/includes_shell_vars/sub2/b b/testdata/includes_shell_vars/sub2/b index e69de29bb2..ba629238ca 100644 --- a/testdata/includes_shell_vars/sub2/b +++ b/testdata/includes_shell_vars/sub2/b @@ -0,0 +1 @@ +BBB From 3e74fc6a47b6d2d186128803118ad252bdbccced Mon Sep 17 00:00:00 2001 From: Viktoras Makauskas Date: Fri, 4 Apr 2025 14:03:34 +0300 Subject: [PATCH 3/6] wip --- testdata/includes_shell_vars/Taskfile.yml | 4 ++-- testdata/includes_shell_vars/sub1/Taskfile.yml | 10 ++++++---- testdata/includes_shell_vars/sub2/Taskfile.yml | 14 ++++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/testdata/includes_shell_vars/Taskfile.yml b/testdata/includes_shell_vars/Taskfile.yml index 654cdaa72b..8882acb6b5 100644 --- a/testdata/includes_shell_vars/Taskfile.yml +++ b/testdata/includes_shell_vars/Taskfile.yml @@ -15,6 +15,6 @@ includes: tasks: all: deps: - - s1:run - - s2:run + - s1:all + - s2:all diff --git a/testdata/includes_shell_vars/sub1/Taskfile.yml b/testdata/includes_shell_vars/sub1/Taskfile.yml index ff873c40f6..3abf83d5fb 100644 --- a/testdata/includes_shell_vars/sub1/Taskfile.yml +++ b/testdata/includes_shell_vars/sub1/Taskfile.yml @@ -1,15 +1,17 @@ version: '3' vars: + pwd1: + sh: "find ." contents1: sh: "cat a" - pwd1: - sh: "pwd" tasks: - run: + all: + dir: . vars: contents1_local: sh: "cat a" cmds: - - cmd: 'echo "contents: {{ .contents1 }} {{ .contents1_local }} {{ .pwd1 }}"' + - cmd: 'echo "contents: {{ .contents1 }} {{ .contents1_local }}"' + - cmd: 'echo {{.pwd1}}' diff --git a/testdata/includes_shell_vars/sub2/Taskfile.yml b/testdata/includes_shell_vars/sub2/Taskfile.yml index 9b263fd8a6..cc4cbd1b2e 100644 --- a/testdata/includes_shell_vars/sub2/Taskfile.yml +++ b/testdata/includes_shell_vars/sub2/Taskfile.yml @@ -1,15 +1,17 @@ version: '3' vars: - contents2: - sh: "cat b" pwd2: - sh: "pwd" + sh: "find ." + contents1: + sh: "cat b" tasks: - run: + all: + dir: . vars: contents2_local: - sh: "cat b" + sh: "cat a" cmds: - - cmd: 'echo "contents: {{ .contents2 }} {{ .contents2_local }} {{ .pwd2 }}"' + - cmd: 'echo "contents: {{ .contents2 }} {{ .contents2_local }}"' + - cmd: 'echo {{.pwd2}}' From d10c99263f5e3969e7486b5e3c53477a9fcd3633 Mon Sep 17 00:00:00 2001 From: Viktoras Makauskas Date: Fri, 4 Apr 2025 14:10:19 +0300 Subject: [PATCH 4/6] wip --- task_test.go | 32 +++++++++---------- .../includes_shell_vars/sub1/Taskfile.yml | 9 +----- .../includes_shell_vars/sub2/Taskfile.yml | 9 +----- 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/task_test.go b/task_test.go index ca5786d5cd..5fb3ae86af 100644 --- a/task_test.go +++ b/task_test.go @@ -3277,22 +3277,22 @@ 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, `[s2:all] global var: BBB`) - require.Contains(t, output, `[s1:all] global var: AAA`) - require.Contains(t, output, `[s1:all] local var: AAA`) - require.Contains(t, output, `[s2:all] local var: BBB`) + for range 10 { // multiple runs just in case it passes on accident + 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) { diff --git a/testdata/includes_shell_vars/sub1/Taskfile.yml b/testdata/includes_shell_vars/sub1/Taskfile.yml index 3abf83d5fb..62caf4867f 100644 --- a/testdata/includes_shell_vars/sub1/Taskfile.yml +++ b/testdata/includes_shell_vars/sub1/Taskfile.yml @@ -3,15 +3,8 @@ version: '3' vars: pwd1: sh: "find ." - contents1: - sh: "cat a" tasks: all: - dir: . - vars: - contents1_local: - sh: "cat a" cmds: - - cmd: 'echo "contents: {{ .contents1 }} {{ .contents1_local }}"' - - cmd: 'echo {{.pwd1}}' + - cmd: 'echo "{{.pwd1}}"' diff --git a/testdata/includes_shell_vars/sub2/Taskfile.yml b/testdata/includes_shell_vars/sub2/Taskfile.yml index cc4cbd1b2e..daede65f10 100644 --- a/testdata/includes_shell_vars/sub2/Taskfile.yml +++ b/testdata/includes_shell_vars/sub2/Taskfile.yml @@ -3,15 +3,8 @@ version: '3' vars: pwd2: sh: "find ." - contents1: - sh: "cat b" tasks: all: - dir: . - vars: - contents2_local: - sh: "cat a" cmds: - - cmd: 'echo "contents: {{ .contents2 }} {{ .contents2_local }}"' - - cmd: 'echo {{.pwd2}}' + - cmd: 'echo "{{.pwd2}}"' From 68ed022c1c89a5b05d5eb331d88e112da065c336 Mon Sep 17 00:00:00 2001 From: Viktoras Makauskas Date: Fri, 4 Apr 2025 14:23:41 +0300 Subject: [PATCH 5/6] wip --- compiler.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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 From 3995cd9d6944c727351127d5f742f6619cb555f0 Mon Sep 17 00:00:00 2001 From: Viktoras Makauskas Date: Fri, 4 Apr 2025 14:34:41 +0300 Subject: [PATCH 6/6] wip --- task_test.go | 30 +++++++++---------- .../includes_shell_vars/sub1/Taskfile.yml | 4 +-- .../includes_shell_vars/sub2/Taskfile.yml | 4 +-- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/task_test.go b/task_test.go index 5fb3ae86af..b0ae31c9a4 100644 --- a/task_test.go +++ b/task_test.go @@ -3277,22 +3277,20 @@ func TestReference(t *testing.T) { func TestResolveShellVarsInSubdirs(t *testing.T) { t.Parallel() - for range 10 { // multiple runs just in case it passes on accident - 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`) - } + 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) { diff --git a/testdata/includes_shell_vars/sub1/Taskfile.yml b/testdata/includes_shell_vars/sub1/Taskfile.yml index 62caf4867f..b7363fa400 100644 --- a/testdata/includes_shell_vars/sub1/Taskfile.yml +++ b/testdata/includes_shell_vars/sub1/Taskfile.yml @@ -1,10 +1,10 @@ version: '3' vars: - pwd1: + files: sh: "find ." tasks: all: cmds: - - cmd: 'echo "{{.pwd1}}"' + - cmd: 'echo "{{.files}}"' diff --git a/testdata/includes_shell_vars/sub2/Taskfile.yml b/testdata/includes_shell_vars/sub2/Taskfile.yml index daede65f10..b7363fa400 100644 --- a/testdata/includes_shell_vars/sub2/Taskfile.yml +++ b/testdata/includes_shell_vars/sub2/Taskfile.yml @@ -1,10 +1,10 @@ version: '3' vars: - pwd2: + files: sh: "find ." tasks: all: cmds: - - cmd: 'echo "{{.pwd2}}"' + - cmd: 'echo "{{.files}}"'