Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,17 @@ func (c *Compiler) getVariables(t *ast.Task, call *Call, evaluateShVars bool) (*
if t != nil {
// NOTE(@andreynering): We're manually joining these paths here because
// this is the raw task, not the compiled one.
cache := &templater.Cache{Vars: result}
dir := templater.Replace(t.Dir, cache)
if err := cache.Err(); err != nil {
return nil, err
// Resolve t.Dir lazily so that template variables (e.g. dir: '{{.VAR}}')
// can reference taskfile-level vars that are processed before task vars.
taskRangeFunc = func(k string, v ast.Var) error {
cache := &templater.Cache{Vars: result}
dir := templater.Replace(t.Dir, cache)
if err := cache.Err(); err != nil {
return err
}
dir = filepathext.SmartJoin(c.Dir, dir)
return getRangeFunc(dir)(k, v)
}
dir = filepathext.SmartJoin(c.Dir, dir)
taskRangeFunc = getRangeFunc(dir)
}

for k, v := range c.TaskfileEnv.All() {
Expand Down Expand Up @@ -157,15 +161,16 @@ func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string,
if c.dynamicCache == nil {
c.dynamicCache = make(map[string]string, 30)
}
if result, ok := c.dynamicCache[*v.Sh]; ok {
return result, nil
}

// NOTE(@andreynering): If a var have a specific dir, use this instead
if v.Dir != "" {
dir = v.Dir
}

cacheKey := dir + "|" + *v.Sh
if result, ok := c.dynamicCache[cacheKey]; ok {
return result, nil
}

var stdout bytes.Buffer
opts := &execext.RunCommandOptions{
Command: *v.Sh,
Expand All @@ -183,7 +188,7 @@ func (c *Compiler) HandleDynamicVar(v ast.Var, dir string, e []string) (string,
result := strings.TrimSuffix(stdout.String(), "\r\n")
result = strings.TrimSuffix(result, "\n")

c.dynamicCache[*v.Sh] = result
c.dynamicCache[cacheKey] = result
c.Logger.VerboseErrf(logger.Magenta, "task: dynamic variable: %q result: %q\n", *v.Sh, result)

return result, nil
Expand Down
2 changes: 1 addition & 1 deletion taskfile/ast/taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (t1 *Taskfile) Merge(t2 *Taskfile, include *Include) error {
}
t1.Vars.Merge(t2.Vars, include)
t1.Env.Merge(t2.Env, include)
return t1.Tasks.Merge(t2.Tasks, include, t1.Vars)
return t1.Tasks.Merge(t2.Tasks, include, t2.Vars)
}

func (tf *Taskfile) UnmarshalYAML(node *yaml.Node) error {
Expand Down
4 changes: 3 additions & 1 deletion taskfile/ast/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ func (t1 *Tasks) Merge(t2 *Tasks, include *Include, includedTaskfileVars *Vars)
task.IncludeVars = NewVars()
}
task.IncludeVars.Merge(include.Vars, nil)
task.IncludedTaskfileVars = includedTaskfileVars.DeepCopy()
if task.IncludedTaskfileVars == nil || task.IncludedTaskfileVars.Len() == 0 {
task.IncludedTaskfileVars = includedTaskfileVars.DeepCopy()
}
}

if _, ok := t1.Get(taskName); ok {
Expand Down