Skip to content

Commit cdfb74d

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 7c2bb78 commit cdfb74d

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

compiler.go

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

9393
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-
}
10594

10695
for k, v := range c.TaskfileEnv.All() {
10796
if err := rangeFunc(k, v); err != nil {
@@ -119,6 +108,23 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
119108
return nil, err
120109
}
121110
}
111+
// Calculate the t.Dir.
112+
cache := &templater.Cache{Vars: result}
113+
rangeDir := c.Dir
114+
if len(t.Dir) == 0 {
115+
// Use the saved include task.Dir.
116+
t.Dir = templater.Replace(t.IncludeTaskDir, cache)
117+
} else {
118+
// Calculate based on include.Dir and t.Dir.
119+
t.Dir = templater.Replace(t.Dir, cache)
120+
rangeDir = filepathext.SmartJoin(c.Dir, t.Dir) // This first, before t.Dir is finalized (it may be a relative path).
121+
t.Dir = filepathext.SmartJoin(t.IncludeDir, t.Dir)
122+
}
123+
if err := cache.Err(); err != nil {
124+
return nil, err
125+
}
126+
taskRangeFunc = getRangeFunc(rangeDir)
127+
122128
for k, v := range t.IncludedTaskfileVars.All() {
123129
if err := taskRangeFunc(k, v); err != nil {
124130
return nil, err

taskfile/ast/task.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type Task struct {
4444
Location *Location
4545
// Populated during merging
4646
Namespace string
47+
IncludeDir string
48+
IncludeTaskDir string
4749
IncludeVars *Vars
4850
IncludedTaskfileVars *Vars
4951

taskfile/ast/tasks.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"gopkg.in/yaml.v3"
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)