Skip to content

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

Closed

Description

@angelmadames

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}}'

Activity

  1. trulede commented on Feb 26, 2025

    @trulede
    Contributor

    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).

  2. trulede commented on Feb 28, 2025

    @trulede
    Contributor

    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.

  3. added theissue type on Dec 6, 2025
  4. added
    area: envChanges related to environment variables.
    and removed
    state: needs triageWaiting to be triaged by a maintainer.
    on Dec 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: envChanges related to environment variables.area: variablesChanges related to variables.

    Type

    Fields

    No fields configured for bug.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions