Skip to content

Commit

Permalink
fix: expand variables as well cmds with dotenv
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed May 3, 2024
1 parent 4cb0450 commit fd2e2da
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
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)
}
20 changes: 20 additions & 0 deletions task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,26 @@ func TestTaskDotenvWithBrackets(t *testing.T) {
},
}
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) {
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
8 changes: 8 additions & 0 deletions testdata/dotenv_task/default/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ tasks:
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

dotenv-overridden-by-env:
dotenv: ['.env']
Expand Down
5 changes: 4 additions & 1 deletion 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 @@ -126,6 +127,8 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
}
}
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 Expand Up @@ -162,7 +165,7 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
continue
}
newCmd := cmd.DeepCopy()
newCmd.Cmd = templater.ReplaceWithExtra(cmd.Cmd, cache, envCache)
newCmd.Cmd = templater.Replace(cmd.Cmd, cache)
newCmd.Task = templater.Replace(cmd.Task, cache)
newCmd.Vars = templater.ReplaceVars(cmd.Vars, cache)
new.Cmds = append(new.Cmds, newCmd)
Expand Down

0 comments on commit fd2e2da

Please sign in to comment.