1
0
mirror of https://github.com/go-task/task.git synced 2025-01-08 04:04:08 +02:00

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 <pd93.uk@outlook.com>
This commit is contained in:
rohm1 2025-01-02 21:07:25 +01:00 committed by GitHub
parent fe9f489702
commit 24a830e384
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 23 additions and 4 deletions

View File

@ -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"
@ -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
}
@ -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()
@ -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)

8
internal/env/env.go vendored
View File

@ -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
}

View File

@ -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)

View File

@ -17,6 +17,7 @@ tasks:
- task: global
- task: not-overridden
- task: multiple_type
- task: dynamic
local:
vars:
@ -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

View File

@ -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"
@ -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
}