Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: expand task-level dotenv in brackets #1627

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/templater/templater.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,11 @@ func ReplaceVarsWithExtra(vars *ast.Vars, cache *Cache, extra map[string]any) *a

return &newVars
}

// MergeCacheMap merges a map into the cache
func (vs *Cache) MergeCacheMap(m map[string]any) {
for k, v := range m {
vs.cacheMap[k] = v
}
vs.Vars.MergeCacheMap(vs.cacheMap)
}
32 changes: 32 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,38 @@ func TestTaskDotenv(t *testing.T) {
tt.Run(t)
}

func TestTaskDotenvWithBrackets(t *testing.T) {
tt := fileContentTest{
Dir: "testdata/dotenv_task/default",
Target: "dotenv",
TrimSpace: true,
Files: map[string]string{
"dotenv-2.txt": "foo",
},
}
tt.Run(t)

tt2 := fileContentTest{
Dir: "testdata/dotenv_task/default",
Target: "dotenv",
TrimSpace: true,
Files: map[string]string{
"dotenv-called.txt": "foo",
},
}
tt2.Run(t)

tt3 := fileContentTest{
Dir: "testdata/dotenv_task/default",
Target: "dotenv",
TrimSpace: true,
Files: map[string]string{
"dotenv-called-2.txt": "foo",
},
}
tt3.Run(t)
}

func TestTaskDotenvFail(t *testing.T) {
tt := fileContentTest{
Dir: "testdata/dotenv_task/default",
Expand Down
8 changes: 8 additions & 0 deletions taskfile/ast/var.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ func (vs *Vars) ToCacheMap() (m map[string]any) {
return
}

// FromCacheMap converts a map containing only the static variables
// to Vars
func (vs *Vars) MergeCacheMap(m map[string]any) {
for k, v := range m {
vs.Set(k, Var{Value: v})
}
}

// Wrapper around OrderedMap.Set to ensure we don't get nil pointer errors
func (vs *Vars) Range(f func(k string, v Var) error) error {
if vs == nil {
Expand Down
9 changes: 9 additions & 0 deletions testdata/dotenv_task/default/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ tasks:
dotenv: ['.env']
cmds:
- echo "$FOO" > dotenv.txt
- echo "{{.FOO}}" > dotenv-2.txt
- task: dotenv_called
vars:
FOO: "{{.FOO}}"

dotenv_called:
cmds:
- echo "$FOO" > dotenv-called.txt
- echo "{{.FOO}}" > dotenv-called-2.txt
Comment on lines 8 to +19
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this PR, this variable forwarding finally works. There's still room for improvement if the dotenv vars should be automatically propagated to any underlying task. But that could be handled separately.


dotenv-overridden-by-env:
dotenv: ['.env']
Expand Down
4 changes: 4 additions & 0 deletions variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
new.Env.Merge(templater.ReplaceVars(e.Taskfile.Env, cache), nil)
new.Env.Merge(templater.ReplaceVars(dotenvEnvs, cache), nil)
new.Env.Merge(templater.ReplaceVars(origTask.Env, cache), nil)
new.Env.Merge(templater.ReplaceVars(call.Vars, cache), nil)
if evaluateShVars {
err = new.Env.Range(func(k string, v ast.Var) error {
// If the variable is not dynamic, we can set it and return
Expand All @@ -125,6 +126,9 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
return nil, err
}
}
envCache := new.Env.ToCacheMap()
// merge envCache with cache
cache.MergeCacheMap(envCache)

if len(origTask.Cmds) > 0 {
new.Cmds = make([]*ast.Cmd, 0, len(origTask.Cmds))
Expand Down
Loading