Skip to content

Fix handling of CLI provided global vars. #2288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion cmd/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ func run() error {
return err
}
calls, globals := args.Parse(cliArgsPreDash...)

// If there are no calls, run the default task instead
if len(calls) == 0 {
calls = append(calls, &task.Call{Task: "default"})
Expand All @@ -165,6 +164,10 @@ 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})
// Merge the CLI globals before the Taskfile globals.
e.Taskfile.Vars.ReverseMerge(globals)
// Do a normal merge to ensure that CLI provided global values have priority (as the
// reverse merge will give priority to the Taskfile globals - last value wins).
e.Taskfile.Vars.Merge(globals, nil)

if !flags.Watch {
Expand Down
14 changes: 14 additions & 0 deletions taskfile/ast/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ func (vars *Vars) Merge(other *Vars, include *Include) {
}
}

// Reverse the merge order; first `other` then merge in the existing `vars`.
func (vars *Vars) ReverseMerge(other *Vars) {
if vars == nil || vars.om == nil || other == nil {
return
}
defer other.mutex.RUnlock()
other.mutex.RLock()
new := other.DeepCopy().om
for pair := vars.om.Front(); pair != nil; pair = pair.Next() {
new.Set(pair.Key, pair.Value)
}
vars.om = new
}

func (vs *Vars) DeepCopy() *Vars {
if vs == nil {
return nil
Expand Down
Loading