Lazy global dynamic variable · Issue #1240 · go-task/task · GitHub
Skip to content

Lazy global dynamic variable #1240

Open

Description

@lockieluke

Only load global variables with SH when the task needs it:

version: '3'
vars:
  MESSAGE:
    sh: sleep 5 && echo "WORLD"

tasks:
  print:
    cmds:
      - echo "HELLO {{.MESSAGE}}"

  print-no-wait:
    cmds:
      - echo "PRINT THIS INSTANTLY"

Other tasks that don't need the variable should not have it loaded

Activity

  1. vctls commented on Oct 24, 2023

    @vctls

    Just noticed that global dynamic variables are evaluated each time a task is run.
    This can indeed become a performance problem. It could at least be documented.

    I don't see many workarounds in the meantime.
    Commands that take time should not be used in global dynamic variables but rather sub-commands or task dependencies.
    Or, you could add conditions in the expression itself. Local variables aren't available, but, for example, the TASK variable is:

    vars:
      FOO:
        sh: |-
          {{ if .TASK | contains "bar" }}sleep 2 && echo baz{{end}}

    This is kind of ugly and cumbersome, but can be used with task namespaces in order to confine the problem to the tasks that actually need that variable.
    I suppose long running command results could also be cached, when possible.

  2. user2410 commented on Jun 2, 2025

    @user2410

    I have been using this workaround for the issue.

    version: '3'
    
    vars:
      MASTER_IP_CMD: |
        awk '/ master /{print $1}' ./machines.txt
    
    tasks:
      build:
        desc: "Build the cluster"
        cmds:
          - ...
          - vagrant up # This produces ./machines.txt
          - ...
    
      certs:gen:
        desc: "Generate certificates for components"
        vars:
          MASTER_IP:
            sh: |
              {{.MASTER_IP_CMD}}
        cmds:
          # Your commands here
    
      dist:certs:
        desc: "Distribute certificates to VMs"
        vars:
          MASTER_IP:
            sh: |
              {{.MASTER_IP_CMD}}
        cmds:
          # Your commands here

    Because the MASTER_IP variable is only available after vagrant up, resolving the global variable before running task build will result in error.
    This workaround avoids evaluating the global dynamic variable by storing the command as a plain string, deferring its execution until a task runs and actually needs it.

  3. added theissue type on Dec 8, 2025
  4. added and removed
    state: needs triageWaiting to be triaged by a maintainer.
    on Dec 8, 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

    Fields

    No fields configured for feature.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions