Skip to content
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

Global vars can't use .CLI_ARGS special var as default value #2090

Open
angelmadames opened this issue Feb 25, 2025 · 2 comments · May be fixed by #2095
Open

Global vars can't use .CLI_ARGS special var as default value #2090

angelmadames opened this issue Feb 25, 2025 · 2 comments · May be fixed by #2095
Labels
state: needs triage Waiting to be triaged by a maintainer.

Comments

@angelmadames
Copy link

Description

I wanted to use a global vars: key to default to the .CLI_ARGS special var.

vars:
  ENV: '{{.ENV | default .CLI_ARGS}}'

And then use it in a task, so both of these examples work:

task debug -- dev  # Should use 'dev' as value for `ENV`
task debug ENV=dev # Should behave similarly as `-- dev`

The above behavior works if vars: is used directly in the task definition:

tasks:
  debug:
    vars:
      ENV: '{{.ENV | default .CLI_ARGS}}'
    cmds:
      - echo '{{.ENV}}'

Only when using vars: globally .CLI_ARGS comes empty and doesn't work.

Version

v3.41.0 (h1:giUddhe0XZLbEWIQ/MuTPipR9ek+teulIA5xf/2IHXg=)

Operating system

macOS Sequioa 15.3.1 (24D70)

Experiments Enabled

No response

Example Taskfile

version: '3'

vars:
  ENV: '{{.ENV | default .CLI_ARGS}}'

tasks:
  debug:
    cmds:
      - echo '{{.ENV}}'
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Feb 25, 2025
@trulede
Copy link

trulede commented Feb 26, 2025

Some of the special vars are set here:

func (c *Compiler) getSpecialVars(t *ast.Task, call *Call) (map[string]string, error) {

and the CLI args are set here, I think after the task has been compiled, which means they are not available to any previous global vars which have already been resolved (i.e. this issue):

globals.Set("CLI_ARGS", ast.Var{Value: cliArgs})

EDIT:
Could be that the CLI special vars are added to the task.go global vars, after those in the Taskfile and IIRC Vars are evaluated in order which would mean that the global var which references a CLI special var would not evaluate (since that reference would not have been resolved).

@trulede
Copy link

trulede commented Feb 28, 2025

This call loads the global vars:

err := e.Setup()

Then this one adds the CLI special vars:

e.Taskfile.Vars.Merge(globals, nil)

since they are after the global vars, they will not be resolved by the templating (since that is ordered).

Conversely, the other special vars are loaded by the compiler here:

specialVars, err := c.getSpecialVars(t, call)

these go in after the env vars, but before the global vars ... so those work.

Solution would be to either; relocate the CLI special vars to getSpecialVars() (which might be tricky), or to change the order that the CLI special vars are merged into e.Taskfile.Vars.Merge(globals, nil). I think the former is the better solution, if it could be achieved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs triage Waiting to be triaged by a maintainer.
Projects
None yet
3 participants