From 5eaf0b2dcd5eb508aba2c8daaf82864c5cd9c12f Mon Sep 17 00:00:00 2001 From: Valentin Maerten Date: Thu, 19 Sep 2024 14:22:39 +0200 Subject: [PATCH] fix: interpolate dynamic vars in defer (#1818) --- task.go | 2 +- task_test.go | 4 ++-- testdata/exit_code/Taskfile.yml | 8 ++++++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/task.go b/task.go index 0b24da07..5c63de34 100644 --- a/task.go +++ b/task.go @@ -330,7 +330,7 @@ func (e *Executor) runDeferred(t *ast.Task, call *ast.Call, i int, deferredExitC } cmd := t.Cmds[i] - vars, _ := e.Compiler.FastGetVariables(origTask, call) + vars, _ := e.Compiler.GetVariables(origTask, call) cache := &templater.Cache{Vars: vars} extra := map[string]any{} diff --git a/task_test.go b/task_test.go index c78cb384..4e31de42 100644 --- a/task_test.go +++ b/task_test.go @@ -1788,7 +1788,7 @@ func TestExitCodeZero(t *testing.T) { require.NoError(t, e.Setup()) require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "exit-zero"})) - assert.Equal(t, "FOO=bar - EXIT_CODE=", strings.TrimSpace(buff.String())) + assert.Equal(t, "FOO=bar - DYNAMIC_FOO=bar - EXIT_CODE=", strings.TrimSpace(buff.String())) } func TestExitCodeOne(t *testing.T) { @@ -1802,7 +1802,7 @@ func TestExitCodeOne(t *testing.T) { require.NoError(t, e.Setup()) require.Error(t, e.Run(context.Background(), &ast.Call{Task: "exit-one"})) - assert.Equal(t, "FOO=bar - EXIT_CODE=1", strings.TrimSpace(buff.String())) + assert.Equal(t, "FOO=bar - DYNAMIC_FOO=bar - EXIT_CODE=1", strings.TrimSpace(buff.String())) } func TestIgnoreNilElements(t *testing.T) { diff --git a/testdata/exit_code/Taskfile.yml b/testdata/exit_code/Taskfile.yml index e78bd2cd..3170b507 100644 --- a/testdata/exit_code/Taskfile.yml +++ b/testdata/exit_code/Taskfile.yml @@ -9,13 +9,17 @@ tasks: exit-zero: vars: FOO: bar + DYNAMIC_FOO: + sh: echo 'bar' cmds: - - defer: echo FOO={{.FOO}} - {{.PREFIX}}{{.EXIT_CODE}} + - defer: echo FOO={{.FOO}} - DYNAMIC_FOO={{.DYNAMIC_FOO}} - {{.PREFIX}}{{.EXIT_CODE}} - exit 0 exit-one: vars: FOO: bar + DYNAMIC_FOO: + sh: echo 'bar' cmds: - - defer: echo FOO={{.FOO}} - {{.PREFIX}}{{.EXIT_CODE}} + - defer: echo FOO={{.FOO}} - DYNAMIC_FOO={{.DYNAMIC_FOO}} - {{.PREFIX}}{{.EXIT_CODE}} - exit 1