diff --git a/internal/compiler/compiler.go b/internal/compiler/compiler.go index fe3bb4b4..527163ac 100644 --- a/internal/compiler/compiler.go +++ b/internal/compiler/compiler.go @@ -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) diff --git a/internal/env/env.go b/internal/env/env.go index 8aded374..1fbb3817 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -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 } diff --git a/task_test.go b/task_test.go index 8920460b..afe09469 100644 --- a/task_test.go +++ b/task_test.go @@ -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) diff --git a/testdata/env/Taskfile.yml b/testdata/env/Taskfile.yml index aa213201..b81a1d1f 100644 --- a/testdata/env/Taskfile.yml +++ b/testdata/env/Taskfile.yml @@ -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 diff --git a/variables.go b/variables.go index 601d6abc..e92ad349 100644 --- a/variables.go +++ b/variables.go @@ -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 }