Skip to content

Commit

Permalink
fix: forward env to RunCommand when evaluating sh vars (#1869)
Browse files Browse the repository at this point in the history
* forward env to RunCommand when evaluating sh vars. fixes #1742

* feat: added tests

* fix: test

---------

Co-authored-by: Pete Davison <[email protected]>
  • Loading branch information
rohm1 and pd93 authored Jan 2, 2025
1 parent fe9f489 commit 24a830e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
6 changes: 4 additions & 2 deletions internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"sync"

"github.com/go-task/task/v3/internal/env"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/logger"
Expand Down Expand Up @@ -79,7 +80,7 @@ func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool
return nil
}
// If the variable is dynamic, we need to resolve it first
static, err := c.HandleDynamicVar(newVar, dir)
static, err := c.HandleDynamicVar(newVar, dir, env.GetFromVars(result))
if err != nil {
return err
}
Expand Down Expand Up @@ -131,7 +132,7 @@ func (c *Compiler) getVariables(t *ast.Task, call *ast.Call, evaluateShVars bool
return result, nil
}

func (c *Compiler) HandleDynamicVar(v ast.Var, dir string) (string, error) {
func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string, error) {
c.muDynamicCache.Lock()
defer c.muDynamicCache.Unlock()

Expand All @@ -158,6 +159,7 @@ func (c *Compiler) HandleDynamicVar(v ast.Var, dir string) (string, error) {
Dir: dir,
Stdout: &stdout,
Stderr: c.Logger.Stderr,
Env: e,
}
if err := execext.RunCommand(context.Background(), opts); err != nil {
return "", fmt.Errorf(`task: Command "%s" failed: %s`, opts.Command, err)
Expand Down
8 changes: 7 additions & 1 deletion internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ func Get(t *ast.Task) []string {
if t.Env == nil {
return nil
}

return GetFromVars(t.Env)
}

func GetFromVars(env *ast.Vars) []string {
environ := os.Environ()
for k, v := range t.Env.ToCacheMap() {

for k, v := range env.ToCacheMap() {
if !isTypeAllowed(v) {
continue
}
Expand Down
1 change: 1 addition & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func TestEnv(t *testing.T) {
"global.txt": "FOO='foo' BAR='overridden' BAZ='baz'\n",
"multiple_type.txt": "FOO='1' BAR='true' BAZ='1.1'\n",
"not-overridden.txt": "QUX='from_os'\n",
"dynamic.txt": "foo\n",
},
}
tt.Run(t)
Expand Down
9 changes: 9 additions & 0 deletions testdata/env/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tasks:
- task: global
- task: not-overridden
- task: multiple_type
- task: dynamic

local:
vars:
Expand Down Expand Up @@ -50,3 +51,11 @@ tasks:
overridden:
cmds:
- echo "QUX='$QUX'" > overridden.txt

dynamic:
silent: true
vars:
DYNAMIC_FOO:
sh: echo $FOO
cmds:
- echo "{{ .DYNAMIC_FOO }}" > dynamic.txt
3 changes: 2 additions & 1 deletion variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/joho/godotenv"

"github.com/go-task/task/v3/errors"
"github.com/go-task/task/v3/internal/env"
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/fingerprint"
Expand Down Expand Up @@ -115,7 +116,7 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
new.Env.Set(k, ast.Var{Value: v.Value})
return nil
}
static, err := e.Compiler.HandleDynamicVar(v, new.Dir)
static, err := e.Compiler.HandleDynamicVar(v, new.Dir, env.GetFromVars(new.Env))
if err != nil {
return err
}
Expand Down

0 comments on commit 24a830e

Please sign in to comment.