Skip to content

Global vars are reevaluated for each task #2003

@Chewie

Description

@Chewie

Description

With the following taskfile:

version: 3

vars:
  TIME: '{{ now }}'

tasks:
  main:
    cmds:
      - task: sub1
      - task: sub2

  sub1:
    cmds:
      - echo {{ .TIME }}

  sub2:
    cmds:
      - echo {{ .TIME }}

I'd expect the value to be the same for both tasks, but instead I get the following output:

task: [sub1] echo 2025-01-15 11:26:00.963791733 +0100 CET m=+0.035128847
2025-01-15 11:26:00.963791733 +0100 CET m=+0.035128847
task: [sub2] echo 2025-01-15 11:26:00.967687754 +0100 CET m=+0.039024858
2025-01-15 11:26:00.967687754 +0100 CET m=+0.039024858

This make it impossible to use that variable as e.g. a version tag for an artifact.

This behavior might be as intended, but there's no mention of that in the docs, and I see no way to make it be evaluated only once :(

Cheers!

Version

Task version: v3.40.0 (h1:1gKx+2UDz06Jtm0MBiN+EqVN87wWEyspuEze4LRGusk=)

Operating system

Linux

Experiments Enabled

No response

Example Taskfile

version: 3

vars:
  TIME: '{{ now }}'

tasks:
  main:
    cmds:
      - task: sub1
      - task: sub2

  sub1:
    cmds:
      - echo {{ .TIME }}

  sub2:
    cmds:
      - echo {{ .TIME }}

Activity

trulede

trulede commented on Jan 26, 2025

@trulede
Contributor

@Chewie The templating is exhibiting correct behaviour. You may use a "dynamic" variable and get the effect you desire.

version: 3

vars:
  TIMEX: '{{ now }}'
  TIME:
    sh: echo "$(date)"

tasks:
  main:
    cmds:
      - task: sub1
      - task: sub2

  sub1:
    cmds:
      - echo {{ .TIME }}
      - echo {{ .TIMEX }}
      - sleep 2
  sub2:
    cmds:
      - echo {{ .TIME }}  ****** this will have the original time from the date command.
      - echo {{ .TIMEX }}
      - echo "$(date)"
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

    state: needs triageWaiting to be triaged by a maintainer.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Chewie@trulede@task-bot

        Issue actions

          Global vars are reevaluated for each task · Issue #2003 · go-task/task