Skip to content

Commit f94223f

Browse files
committed
fix: expand variables as well cmds with dotenv
1 parent 3c6e1f9 commit f94223f

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

internal/templater/templater.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,11 @@ func ReplaceVarsWithExtra(vars *ast.Vars, cache *Cache, extra map[string]any) *a
141141

142142
return &newVars
143143
}
144+
145+
// MergeCacheMap merges a map into the cache
146+
func (vs *Cache) MergeCacheMap(m map[string]any) {
147+
for k, v := range m {
148+
vs.cacheMap[k] = v
149+
}
150+
vs.Vars.MergeCacheMap(vs.cacheMap)
151+
}

task_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,26 @@ func TestTaskDotenvWithBrackets(t *testing.T) {
15711571
},
15721572
}
15731573
tt.Run(t)
1574+
1575+
tt2 := fileContentTest{
1576+
Dir: "testdata/dotenv_task/default",
1577+
Target: "dotenv",
1578+
TrimSpace: true,
1579+
Files: map[string]string{
1580+
"dotenv-called.txt": "foo",
1581+
},
1582+
}
1583+
tt2.Run(t)
1584+
1585+
tt3 := fileContentTest{
1586+
Dir: "testdata/dotenv_task/default",
1587+
Target: "dotenv",
1588+
TrimSpace: true,
1589+
Files: map[string]string{
1590+
"dotenv-called-2.txt": "foo",
1591+
},
1592+
}
1593+
tt3.Run(t)
15741594
}
15751595

15761596
func TestTaskDotenvFail(t *testing.T) {

taskfile/ast/var.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ func (vs *Vars) ToCacheMap() (m map[string]any) {
3636
return
3737
}
3838

39+
// FromCacheMap converts a map containing only the static variables
40+
// to Vars
41+
func (vs *Vars) MergeCacheMap(m map[string]any) {
42+
for k, v := range m {
43+
vs.Set(k, Var{Value: v})
44+
}
45+
}
46+
3947
// Wrapper around OrderedMap.Set to ensure we don't get nil pointer errors
4048
func (vs *Vars) Range(f func(k string, v Var) error) error {
4149
if vs == nil {

testdata/dotenv_task/default/Taskfile.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ tasks:
99
cmds:
1010
- echo "$FOO" > dotenv.txt
1111
- echo "{{.FOO}}" > dotenv-2.txt
12+
- task: dotenv_called
13+
vars:
14+
FOO: "{{.FOO}}"
15+
16+
dotenv_called:
17+
cmds:
18+
- echo "$FOO" > dotenv-called.txt
19+
- echo "{{.FOO}}" > dotenv-called-2.txt
1220

1321
dotenv-overridden-by-env:
1422
dotenv: ['.env']

variables.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
107107
new.Env.Merge(templater.ReplaceVars(e.Taskfile.Env, cache), nil)
108108
new.Env.Merge(templater.ReplaceVars(dotenvEnvs, cache), nil)
109109
new.Env.Merge(templater.ReplaceVars(origTask.Env, cache), nil)
110+
new.Env.Merge(templater.ReplaceVars(call.Vars, cache), nil)
110111
if evaluateShVars {
111112
err = new.Env.Range(func(k string, v ast.Var) error {
112113
// If the variable is not dynamic, we can set it and return
@@ -126,6 +127,8 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
126127
}
127128
}
128129
envCache := new.Env.ToCacheMap()
130+
// merge envCache with cache
131+
cache.MergeCacheMap(envCache)
129132

130133
if len(origTask.Cmds) > 0 {
131134
new.Cmds = make([]*ast.Cmd, 0, len(origTask.Cmds))
@@ -162,7 +165,7 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task,
162165
continue
163166
}
164167
newCmd := cmd.DeepCopy()
165-
newCmd.Cmd = templater.ReplaceWithExtra(cmd.Cmd, cache, envCache)
168+
newCmd.Cmd = templater.Replace(cmd.Cmd, cache)
166169
newCmd.Task = templater.Replace(cmd.Task, cache)
167170
newCmd.Vars = templater.ReplaceVars(cmd.Vars, cache)
168171
new.Cmds = append(new.Cmds, newCmd)

0 commit comments

Comments
 (0)