diff --git a/cmd/task/task.go b/cmd/task/task.go index d5f2b7baf0..d4768f6591 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -165,8 +165,7 @@ func run() error { globals.Set("CLI_SILENT", ast.Var{Value: flags.Silent}) globals.Set("CLI_VERBOSE", ast.Var{Value: flags.Verbose}) globals.Set("CLI_OFFLINE", ast.Var{Value: flags.Offline}) - e.Taskfile.Vars.Merge(globals, nil) - + e.Taskfile.Vars.ReverseMerge(globals, nil) if !flags.Watch { e.InterceptInterruptSignals() } diff --git a/taskfile/ast/vars.go b/taskfile/ast/vars.go index 0271ee5c3a..77540a8168 100644 --- a/taskfile/ast/vars.go +++ b/taskfile/ast/vars.go @@ -118,7 +118,7 @@ func (vars *Vars) ToCacheMap() (m map[string]any) { // Merge loops over other and merges it values with the variables in vars. If // the include parameter is not nil and its it is an advanced import, the -// directory is set set to the value of the include parameter. +// directory is set to the value of the include parameter. func (vars *Vars) Merge(other *Vars, include *Include) { if vars == nil || vars.om == nil || other == nil { return @@ -133,6 +133,35 @@ func (vars *Vars) Merge(other *Vars, include *Include) { } } +// ReverseMerge merges other variables with the existing variables in vars, but +// keeps the other variables first in order. If the include parameter is not +// nil and it is an advanced import, the directory is set to the value of the +// include parameter. +func (vars *Vars) ReverseMerge(other *Vars, include *Include) { + if vars == nil || vars.om == nil || other == nil || other.om == nil { + return + } + + newOM := orderedmap.NewOrderedMap[string, Var]() + + other.mutex.RLock() + for pair := other.om.Front(); pair != nil; pair = pair.Next() { + val := pair.Value + if include != nil && include.AdvancedImport { + val.Dir = include.Dir + } + newOM.Set(pair.Key, val) + } + other.mutex.RUnlock() + + vars.mutex.Lock() + for pair := vars.om.Front(); pair != nil; pair = pair.Next() { + newOM.Set(pair.Key, pair.Value) + } + vars.om = newOM + vars.mutex.Unlock() +} + func (vs *Vars) DeepCopy() *Vars { if vs == nil { return nil