Skip to content

Commit 24a830e

Browse files
rohm1pd93
andauthored
fix: forward env to RunCommand when evaluating sh vars (#1869)
* forward env to RunCommand when evaluating sh vars. fixes #1742 * feat: added tests * fix: test --------- Co-authored-by: Pete Davison <[email protected]>
1 parent fe9f489 commit 24a830e

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

internal/compiler/compiler.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"sync"
1111

12+
"github.com/go-task/task/v3/internal/env"
1213
"github.com/go-task/task/v3/internal/execext"
1314
"github.com/go-task/task/v3/internal/filepathext"
1415
"github.com/go-task/task/v3/internal/logger"
@@ -79,7 +80,7 @@ func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool
7980
return nil
8081
}
8182
// If the variable is dynamic, we need to resolve it first
82-
static, err := c.HandleDynamicVar(newVar, dir)
83+
static, err := c.HandleDynamicVar(newVar, dir, env.GetFromVars(result))
8384
if err != nil {
8485
return err
8586
}
@@ -131,7 +132,7 @@ func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool
131132
return result, nil
132133
}
133134

134-
func (c *Compiler) HandleDynamicVar(v ast.Var, dir string) (string, error) {
135+
func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string, error) {
135136
c.muDynamicCache.Lock()
136137
defer c.muDynamicCache.Unlock()
137138

@@ -158,6 +159,7 @@ func (c *Compiler) HandleDynamicVar(v ast.Var, dir string) (string, error) {
158159
Dir: dir,
159160
Stdout: &stdout,
160161
Stderr: c.Logger.Stderr,
162+
Env: e,
161163
}
162164
if err := execext.RunCommand(context.Background(), opts); err != nil {
163165
return "", fmt.Errorf(`task: Command "%s" failed: %s`, opts.Command, err)

internal/env/env.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ func Get(t *ast.Task) []string {
1212
if t.Env == nil {
1313
return nil
1414
}
15+
16+
return GetFromVars(t.Env)
17+
}
18+
19+
func GetFromVars(env *ast.Vars) []string {
1520
environ := os.Environ()
16-
for k, v := range t.Env.ToCacheMap() {
21+
22+
for k, v := range env.ToCacheMap() {
1723
if !isTypeAllowed(v) {
1824
continue
1925
}

task_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func TestEnv(t *testing.T) {
130130
"global.txt": "FOO='foo' BAR='overridden' BAZ='baz'\n",
131131
"multiple_type.txt": "FOO='1' BAR='true' BAZ='1.1'\n",
132132
"not-overridden.txt": "QUX='from_os'\n",
133+
"dynamic.txt": "foo\n",
133134
},
134135
}
135136
tt.Run(t)

testdata/env/Taskfile.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ tasks:
1717
- task: global
1818
- task: not-overridden
1919
- task: multiple_type
20+
- task: dynamic
2021

2122
local:
2223
vars:
@@ -50,3 +51,11 @@ tasks:
5051
overridden:
5152
cmds:
5253
- echo "QUX='$QUX'" > overridden.txt
54+
55+
dynamic:
56+
silent: true
57+
vars:
58+
DYNAMIC_FOO:
59+
sh: echo $FOO
60+
cmds:
61+
- echo "{{ .DYNAMIC_FOO }}" > dynamic.txt

variables.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/joho/godotenv"
99

1010
"github.com/go-task/task/v3/errors"
11+
"github.com/go-task/task/v3/internal/env"
1112
"github.com/go-task/task/v3/internal/execext"
1213
"github.com/go-task/task/v3/internal/filepathext"
1314
"github.com/go-task/task/v3/internal/fingerprint"
@@ -115,7 +116,7 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
115116
new.Env.Set(k, ast.Var{Value: v.Value})
116117
return nil
117118
}
118-
static, err := e.Compiler.HandleDynamicVar(v, new.Dir)
119+
static, err := e.Compiler.HandleDynamicVar(v, new.Dir, env.GetFromVars(new.Env))
119120
if err != nil {
120121
return err
121122
}

0 commit comments

Comments
 (0)