diff --git a/internal/compiler/v3/compiler_v3.go b/internal/compiler/v3/compiler_v3.go index a5c6a48a9b..afb4f4660b 100644 --- a/internal/compiler/v3/compiler_v3.go +++ b/internal/compiler/v3/compiler_v3.go @@ -114,6 +114,9 @@ func (c *CompilerV3) getVariables(t *taskfile.Task, call *taskfile.Call, evaluat return result, nil } + if err := t.Env.Range(taskRangeFunc); err != nil { + return nil, err + } if err := call.Vars.Range(rangeFunc); err != nil { return nil, err } @@ -148,8 +151,10 @@ func (c *CompilerV3) HandleDynamicVar(v taskfile.Var, dir string) (string, error opts := &execext.RunCommandOptions{ Command: v.Sh, Dir: dir, - Stdout: &stdout, - Stderr: c.Logger.Stderr, + // TODO: Should we pass the current environment to the command? + Env: []string{}, + Stdout: &stdout, + Stderr: c.Logger.Stderr, } if err := execext.RunCommand(context.Background(), opts); err != nil { return "", fmt.Errorf(`task: Command "%s" failed: %s`, opts.Command, err) diff --git a/task_test.go b/task_test.go index 43df8bf26d..8949ac62de 100644 --- a/task_test.go +++ b/task_test.go @@ -1542,6 +1542,18 @@ func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) { tt.Run(t) } +func TestDotenvShouldLoadBeforeTaskVars(t *testing.T) { + tt := fileContentTest{ + Dir: "testdata/dotenv/load_dotenv_before_vars", + Target: "default", + TrimSpace: false, + Files: map[string]string{ + "include.txt": "AWS_ACCESS_KEY_ID='123'\n", + }, + } + tt.Run(t) +} + func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) { const dir = "testdata/dotenv/error_included_envs" const entry = "Taskfile.yml" diff --git a/testdata/dotenv/load_dotenv_before_vars/.env b/testdata/dotenv/load_dotenv_before_vars/.env new file mode 100644 index 0000000000..b6a9b84d92 --- /dev/null +++ b/testdata/dotenv/load_dotenv_before_vars/.env @@ -0,0 +1 @@ +SHT_DO_SPACES_ACCESS_ID=123 diff --git a/testdata/dotenv/load_dotenv_before_vars/Taskfile.yml b/testdata/dotenv/load_dotenv_before_vars/Taskfile.yml new file mode 100644 index 0000000000..1140a25eec --- /dev/null +++ b/testdata/dotenv/load_dotenv_before_vars/Taskfile.yml @@ -0,0 +1,11 @@ +version: '3' + +dotenv: ['.env'] + +tasks: + default: + vars: + AWS_ACCESS_KEY_ID: + sh: echo $SHT_DO_SPACES_ACCESS_ID + cmds: + - echo "AWS_ACCESS_KEY_ID='$AWS_ACCESS_KEY_ID'" > include.txt