Skip to content

Commit

Permalink
fix: load dotenv before vars
Browse files Browse the repository at this point in the history
Signed-off-by: Yordis Prieto <[email protected]>
  • Loading branch information
yordis committed Oct 30, 2023
1 parent b681ef9 commit fbcfd68
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/compiler/v3/compiler_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions testdata/dotenv/load_dotenv_before_vars/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SHT_DO_SPACES_ACCESS_ID=123
11 changes: 11 additions & 0 deletions testdata/dotenv/load_dotenv_before_vars/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit fbcfd68

Please sign in to comment.