From e9448bd4be64ef416e7baca161bbed12be5cb204 Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Tue, 19 Mar 2024 15:02:32 +0000 Subject: [PATCH] fix: advanced import operates on including file instead of included file --- cmd/task/task.go | 2 +- taskfile/ast/graph.go | 27 --------------------------- taskfile/ast/taskfile.go | 6 +++--- taskfile/ast/tasks.go | 12 +++++++++++- taskfile/ast/var.go | 10 ++++++++-- variables.go | 6 +++--- 6 files changed, 26 insertions(+), 37 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index 0d156614..be63b457 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -178,7 +178,7 @@ func run() error { globals.Set("CLI_ARGS", ast.Var{Value: cliArgs}) globals.Set("CLI_FORCE", ast.Var{Value: flags.Force || flags.ForceAll}) - e.Taskfile.Vars.Merge(globals) + e.Taskfile.Vars.Merge(globals, nil) if !flags.Watch { e.InterceptInterruptSignals() diff --git a/taskfile/ast/graph.go b/taskfile/ast/graph.go index 50b23073..5e72b955 100644 --- a/taskfile/ast/graph.go +++ b/taskfile/ast/graph.go @@ -7,8 +7,6 @@ import ( "github.com/dominikbraun/graph" "github.com/dominikbraun/graph/draw" "golang.org/x/sync/errgroup" - - "github.com/go-task/task/v3/internal/filepathext" ) type TaskfileGraph struct { @@ -88,31 +86,6 @@ func (tfg *TaskfileGraph) Merge() (*Taskfile, error) { return fmt.Errorf("task: Failed to get merge options") } - // Handle advanced imports - // i.e. where additional data is given when a Taskfile is included - if include.AdvancedImport { - includedVertex.Taskfile.Vars.Range(func(k string, v Var) error { - o := v - o.Dir = include.Dir - includedVertex.Taskfile.Vars.Set(k, o) - return nil - }) - includedVertex.Taskfile.Env.Range(func(k string, v Var) error { - o := v - o.Dir = include.Dir - includedVertex.Taskfile.Env.Set(k, o) - return nil - }) - for _, task := range includedVertex.Taskfile.Tasks.Values() { - task.Dir = filepathext.SmartJoin(include.Dir, task.Dir) - if task.IncludeVars == nil { - task.IncludeVars = &Vars{} - } - task.IncludeVars.Merge(include.Vars) - task.IncludedTaskfileVars = vertex.Taskfile.Vars - } - } - // Merge the included Taskfile into the parent Taskfile if err := vertex.Taskfile.Merge( includedVertex.Taskfile, diff --git a/taskfile/ast/taskfile.go b/taskfile/ast/taskfile.go index bbe1462f..f8957220 100644 --- a/taskfile/ast/taskfile.go +++ b/taskfile/ast/taskfile.go @@ -52,9 +52,9 @@ func (t1 *Taskfile) Merge(t2 *Taskfile, include *Include) error { if t1.Env == nil { t1.Env = &Vars{} } - t1.Vars.Merge(t2.Vars) - t1.Env.Merge(t2.Env) - t1.Tasks.Merge(t2.Tasks, include) + t1.Vars.Merge(t2.Vars, include) + t1.Env.Merge(t2.Env, include) + t1.Tasks.Merge(t2.Tasks, include, t1.Vars) return nil } diff --git a/taskfile/ast/tasks.go b/taskfile/ast/tasks.go index bdadb482..79d7606d 100644 --- a/taskfile/ast/tasks.go +++ b/taskfile/ast/tasks.go @@ -6,6 +6,7 @@ import ( "gopkg.in/yaml.v3" + "github.com/go-task/task/v3/internal/filepathext" "github.com/go-task/task/v3/internal/omap" ) @@ -44,7 +45,7 @@ func (t *Tasks) FindMatchingTasks(call *Call) []*MatchingTask { return matchingTasks } -func (t1 *Tasks) Merge(t2 Tasks, include *Include) { +func (t1 *Tasks) Merge(t2 Tasks, include *Include, includedTaskfileVars *Vars) { _ = t2.Range(func(k string, v *Task) error { // We do a deep copy of the task struct here to ensure that no data can // be changed elsewhere once the taskfile is merged. @@ -83,6 +84,15 @@ func (t1 *Tasks) Merge(t2 Tasks, include *Include) { } } + if include.AdvancedImport { + task.Dir = filepathext.SmartJoin(include.Dir, task.Dir) + if task.IncludeVars == nil { + task.IncludeVars = &Vars{} + } + task.IncludeVars.Merge(include.Vars, nil) + task.IncludedTaskfileVars = includedTaskfileVars + } + // Add the task to the merged taskfile taskNameWithNamespace := taskNameWithNamespace(k, include.Namespace) task.Task = taskNameWithNamespace diff --git a/taskfile/ast/var.go b/taskfile/ast/var.go index 7ec0a44b..0fe8c05e 100644 --- a/taskfile/ast/var.go +++ b/taskfile/ast/var.go @@ -45,11 +45,17 @@ func (vs *Vars) Range(f func(k string, v Var) error) error { } // Wrapper around OrderedMap.Merge to ensure we don't get nil pointer errors -func (vs *Vars) Merge(other *Vars) { +func (vs *Vars) Merge(other *Vars, include *Include) { if vs == nil || other == nil { return } - vs.OrderedMap.Merge(other.OrderedMap) + other.Range(func(key string, value Var) error { + if include != nil && include.AdvancedImport { + value.Dir = include.Dir + } + vs.Set(key, value) + return nil + }) } // Wrapper around OrderedMap.Len to ensure we don't get nil pointer errors diff --git a/variables.go b/variables.go index 38a296b3..33b31fcf 100644 --- a/variables.go +++ b/variables.go @@ -104,9 +104,9 @@ func (e *Executor) compiledTask(call *ast.Call, evaluateShVars bool) (*ast.Task, } new.Env = &ast.Vars{} - new.Env.Merge(templater.ReplaceVars(e.Taskfile.Env, cache)) - new.Env.Merge(templater.ReplaceVars(dotenvEnvs, cache)) - new.Env.Merge(templater.ReplaceVars(origTask.Env, cache)) + new.Env.Merge(templater.ReplaceVars(e.Taskfile.Env, cache), nil) + new.Env.Merge(templater.ReplaceVars(dotenvEnvs, cache), nil) + new.Env.Merge(templater.ReplaceVars(origTask.Env, cache), nil) if evaluateShVars { err = new.Env.Range(func(k string, v ast.Var) error { // If the variable is not dynamic, we can set it and return