Skip to content

Commit 1bf8505

Browse files
fix: interpolate vars in defer (#1814)
Co-authored-by: Andrey Nering <[email protected]>
1 parent 0be0579 commit 1bf8505

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

task.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,13 @@ func (e *Executor) runDeferred(t *ast.Task, call *ast.Call, i int, deferredExitC
324324
ctx, cancel := context.WithCancel(context.Background())
325325
defer cancel()
326326

327+
origTask, err := e.GetTask(call)
328+
if err != nil {
329+
return
330+
}
331+
327332
cmd := t.Cmds[i]
328-
vars, _ := e.Compiler.FastGetVariables(t, call)
333+
vars, _ := e.Compiler.FastGetVariables(origTask, call)
329334
cache := &templater.Cache{Vars: vars}
330335
extra := map[string]any{}
331336

task_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1788,7 +1788,7 @@ func TestExitCodeZero(t *testing.T) {
17881788
require.NoError(t, e.Setup())
17891789

17901790
require.NoError(t, e.Run(context.Background(), &ast.Call{Task: "exit-zero"}))
1791-
assert.Equal(t, "EXIT_CODE=", strings.TrimSpace(buff.String()))
1791+
assert.Equal(t, "FOO=bar - EXIT_CODE=", strings.TrimSpace(buff.String()))
17921792
}
17931793

17941794
func TestExitCodeOne(t *testing.T) {
@@ -1802,7 +1802,7 @@ func TestExitCodeOne(t *testing.T) {
18021802
require.NoError(t, e.Setup())
18031803

18041804
require.Error(t, e.Run(context.Background(), &ast.Call{Task: "exit-one"}))
1805-
assert.Equal(t, "EXIT_CODE=1", strings.TrimSpace(buff.String()))
1805+
assert.Equal(t, "FOO=bar - EXIT_CODE=1", strings.TrimSpace(buff.String()))
18061806
}
18071807

18081808
func TestIgnoreNilElements(t *testing.T) {

testdata/exit_code/Taskfile.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ vars:
77

88
tasks:
99
exit-zero:
10+
vars:
11+
FOO: bar
1012
cmds:
11-
- defer: echo {{.PREFIX}}{{.EXIT_CODE}}
13+
- defer: echo FOO={{.FOO}} - {{.PREFIX}}{{.EXIT_CODE}}
1214
- exit 0
1315

1416
exit-one:
17+
vars:
18+
FOO: bar
1519
cmds:
16-
- defer: echo {{.PREFIX}}{{.EXIT_CODE}}
20+
- defer: echo FOO={{.FOO}} - {{.PREFIX}}{{.EXIT_CODE}}
1721
- exit 1

0 commit comments

Comments
 (0)