Skip to content

Commit d24f90f

Browse files
committed
Included taskfiles save both include.Dir and task.Dir so that
when the task is compiled the task.Dir can be determined using the template cache (not available when the AST is being constructed).
1 parent 7901cce commit d24f90f

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

compiler.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,6 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
9090
}
9191
rangeFunc := getRangeFunc(c.Dir)
9292

93-
var taskRangeFunc func(k string, v ast.Var) error
94-
if t != nil {
95-
// NOTE(@andreynering): We're manually joining these paths here because
96-
// this is the raw task, not the compiled one.
97-
cache := &templater.Cache{Vars: result}
98-
dir := templater.Replace(t.Dir, cache)
99-
if err := cache.Err(); err != nil {
100-
return nil, err
101-
}
102-
dir = filepathext.SmartJoin(c.Dir, dir)
103-
taskRangeFunc = getRangeFunc(dir)
104-
}
105-
10693
for k, v := range c.TaskfileEnv.All() {
10794
if err := rangeFunc(k, v); err != nil {
10895
return nil, err
@@ -113,31 +100,48 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
113100
return nil, err
114101
}
115102
}
103+
116104
if t != nil {
117105
for k, v := range t.IncludeVars.All() {
118106
if err := rangeFunc(k, v); err != nil {
119107
return nil, err
120108
}
121109
}
110+
111+
// Calculate the t.Dir now based on values saved during AST parsing
112+
// and then get a Task RangeFunc.
113+
dir := c.Dir
114+
if len(t.Dir) == 0 {
115+
// Use the saved include task.Dir.
116+
t.Dir = t.IncludeTaskDir
117+
} else {
118+
cache := &templater.Cache{Vars: result}
119+
taskDir := templater.Replace(t.Dir, cache)
120+
if err := cache.Err(); err != nil {
121+
return nil, err
122+
}
123+
t.Dir = filepathext.SmartJoin(t.IncludeDir, taskDir)
124+
// Update dir (esp. if taskDir is a relative path).
125+
dir = filepathext.SmartJoin(c.Dir, taskDir)
126+
}
127+
taskRangeFunc := getRangeFunc(dir)
128+
122129
for k, v := range t.IncludedTaskfileVars.All() {
123130
if err := taskRangeFunc(k, v); err != nil {
124131
return nil, err
125132
}
126133
}
127-
}
128-
129-
if t == nil || call == nil {
130-
return result, nil
131-
}
132-
133-
for k, v := range call.Vars.All() {
134-
if err := rangeFunc(k, v); err != nil {
135-
return nil, err
134+
if call != nil {
135+
for k, v := range call.Vars.All() {
136+
if err := rangeFunc(k, v); err != nil {
137+
return nil, err
138+
}
139+
}
136140
}
137-
}
138-
for k, v := range t.Vars.All() {
139-
if err := taskRangeFunc(k, v); err != nil {
140-
return nil, err
141+
for k, v := range t.Vars.All() {
142+
if err := taskRangeFunc(k, v); err != nil {
143+
return nil, err
144+
}
141145
}
142146
}
143147

taskfile/ast/tasks.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"go.yaml.in/yaml/v4"
1212

1313
"github.com/go-task/task/v3/errors"
14-
"github.com/go-task/task/v3/internal/filepathext"
1514
"github.com/go-task/task/v3/internal/sort"
1615
)
1716

@@ -171,7 +170,14 @@ func (t1 *Tasks) Merge(t2 *Tasks, include *Include, includedTaskfileVars *Vars)
171170
}
172171

173172
if include.AdvancedImport {
174-
task.Dir = filepathext.SmartJoin(include.Dir, task.Dir)
173+
// Save the include.Dir and task.Dir so that the final task.dir can
174+
// be calculated when the task is compiled (and templating is available).
175+
task.IncludeDir = include.Dir
176+
task.IncludeTaskDir = task.Dir
177+
// If task.dir is not set then use the include.dir.
178+
if len(task.Dir) == 0 {
179+
task.Dir = include.Dir
180+
}
175181
if task.IncludeVars == nil {
176182
task.IncludeVars = NewVars()
177183
}

0 commit comments

Comments
 (0)