Skip to content

Commit 76beeaa

Browse files
committed
FIx calculation of task.Dir for tasks in included taskfiles.
1 parent 72991d4 commit 76beeaa

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

compiler.go

Lines changed: 16 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,22 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
119108
return nil, err
120109
}
121110
}
111+
112+
// NOTE: Calculate the task.Dir and taskRangeFunc here, after
113+
// IncludeVars have been added to the templater. The call to SmartJoin()
114+
// will then work as expected (i.e. when `dir` becomes an absolute path
115+
// after the templater.Replace() call).
116+
// NOTE: taskRangeFunc() will be available to subsequent calls
117+
// in this function despite the convoluted code structure here.
118+
cache := &templater.Cache{Vars: result}
119+
dir := templater.Replace(t.Dir, cache)
120+
t.Dir = filepathext.SmartJoin(t.IncludeDir, dir)
121+
if err := cache.Err(); err != nil {
122+
return nil, err
123+
}
124+
dir = filepathext.SmartJoin(c.Dir, dir)
125+
taskRangeFunc = getRangeFunc(dir)
126+
122127
for k, v := range t.IncludedTaskfileVars.All() {
123128
if err := taskRangeFunc(k, v); err != nil {
124129
return nil, err

taskfile/ast/task.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type Task struct {
4444
Location *Location
4545
// Populated during merging
4646
Namespace string
47+
IncludeDir string
4748
IncludeVars *Vars
4849
IncludedTaskfileVars *Vars
4950
}

taskfile/ast/tasks.go

Lines changed: 1 addition & 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,7 @@ 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+
task.IncludeDir = include.Dir
175174
if task.IncludeVars == nil {
176175
task.IncludeVars = NewVars()
177176
}

0 commit comments

Comments
 (0)