Skip to content

Defer no longer supporting passing vars (not interpolation) #1909

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
toby-griffiths opened this issue Nov 6, 2024 · 4 comments · May be fixed by #2173
Open

Defer no longer supporting passing vars (not interpolation) #1909

toby-griffiths opened this issue Nov 6, 2024 · 4 comments · May be fixed by #2173
Labels
area: variables Changes related to variables.

Comments

@toby-griffiths
Copy link

We are using for loops to deploy things for a list of tenants, but for some reason the deferred command is missing any vars interpolated into the vars of the deferred task.

Example…

version: '3'

tasks:
  loop:
    vars:
      NAMES: 'Ana Bob Charlie'
    cmds:
      - for: { var: NAMES }
        task: 'test'
        vars:
          NAME: '{{ .ITEM }}'

  test:
    requires:
      vars: [ 'NAME' ]
    cmds:
      - task: 'x'
        vars:
          NAME: 'During: {{.NAME}}'
      - defer:
          task: 'x'
          vars:
            NAME: 'Deferred: {{.NAME}}'

  x:
    vars:
      requires: ['NAME']
    cmd: 'echo "{{.NAME}}"'

Results in …

task: [x] echo "During: Ana"
During: Ana
task: [x] echo "Deferred: "
Deferred:
task: [x] echo "During: Bob"
During: Bob
task: [x] echo "Deferred: "
Deferred:
task: [x] echo "During: Charlie"
During: Charlie
task: [x] echo "Deferred: "
Deferred:

You'll see from this example that the regular commands display the names, but the deferred ones do not.

If you interpolate into a direct command, rather than a task this works fine…

tasks:
  loop:
    vars:
      NAMES: 'Ana Bob Charlie'
    cmds:
      - for: { var: NAMES }
        task: 'test'
        vars:
          NAME: '{{ .ITEM }}'

  test:
    requires:
      vars: [ 'NAME' ]
    cmds:
      - task: 'x'
        vars:
          NAME: 'During: {{.NAME}}'
      - defer: 'echo "Deferred: {{.NAME}}"'

  x:
    vars:
      requires: ['NAME']
    cmd: 'echo "{{.NAME}}"'

Resulting in…

$ task loop
task: [x] echo "During: Ana"
During: Ana
task: [test] echo "Deferred: Ana"
Deferred: Ana
task: [x] echo "During: Bob"
During: Bob
task: [test] echo "Deferred: Bob"
Deferred: Bob
task: [x] echo "During: Charlie"
During: Charlie
task: [test] echo "Deferred: Charlie"
Deferred: Charlie
  • Task version: v3.39.2 (h1:Zt7KXHmMNq5xWZ1ihphDb+n2zYLCo4BdRe09AnMMIgA=)
  • Operating system: MacOS 14.6.1 (23G93)
  • Experiments enabled: None
@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Nov 6, 2024
@faffa82
Copy link

faffa82 commented Mar 3, 2025

I've encountered a similar issue using 3.41.0 on Win10
Here is a simple example

version: '3'

env:
  VAR3: Hey, there!

tasks:
  defer:debug:
    cmds:
      - echo '{{.VAR1}}'
      - echo '{{.VAR2}}'
      - echo '{{.VAR3}}'

  defer:main:
    cmds:
      - defer: {task: defer:debug, vars: {VAR1: '{{.VAR1}}', VAR2: 'value2', VAR3: '{{.VAR3}}'}}
      - echo '{{.VAR1}}'
      - echo '{{.VAR2}}'
      - echo '{{.VAR3}}'

  defer:sub:
    cmds:
      - task: defer:main
        vars:
          VAR1: 'sub1'
          VAR2: 'sub2'
          VAR3: 'sub3'

if I run task defer:main VAR1=test VAR2=hi VAR3=hello
I get this output:

test
hi
hello
test
value2
hello

and this is fine and it is what I was expecting.
But if I run task defer:sub
I get this output:

sub1
sub2
sub3

value2
Hey, there!

Basically, variables that are passed to a task from another task are ignored by the defer, and this seems the same problem @toby-griffiths was experiencing.
In the deferred task, in this scenario they are set to empty or to the value from env (if any). They are correctly evaluated if values are passed directly from CLI to the task with the defer.

@totahuanocotl
Copy link

totahuanocotl commented Mar 27, 2025

I think it doesn't respect any vars defined in the task itself.
Simplifying @faffa82 's example:

version: '3'

env:
  VAR3: Hey, there!

tasks:
  defer:debug:
    cmds:
      - echo '{{.VAR1}}'
      - echo '{{.VAR2}}'
      - echo '{{.VAR3}}'

  defer:main:
    vars:
      VAR1: 'sub1'
      VAR2: 'sub2'
      VAR3: 'sub3'
    cmds:
      - defer: {task: defer:debug, vars: {VAR1: '{{.VAR1}}', VAR2: 'value2', VAR3: '{{.VAR3}}'}}
      - echo '{{.VAR1}}'
      - echo '{{.VAR2}}'
      - echo '{{.VAR3}}'

Running task defer:main also outputs:

sub1
sub2
sub3

value2
Hey, there!

@odormond
Copy link

odormond commented Apr 1, 2025

Still broken in 3.42.1.

Taskfile.yml:

version: '3'
tasks:
  foo:
    cmds:
      - 'echo "foo: {{.FOO}}"'
      - defer:
          task: bar
          vars: {BAR: 'defer: {{.FOO}}'}
      - task: bar
        vars: {BAR: 'direct: {{.FOO}}'}
    vars:
      FOO: "foobar"

  bar:
    cmds:
      - 'echo "bar: {{.BAR}}"'

Execution:

$ task foo
task: [foo] echo "foo: foobar"
foo: foobar
task: [bar] echo "bar: direct: foobar"
bar: direct: foobar
task: [bar] echo "bar: defer: "
bar: defer:

@Rhionin
Copy link

Rhionin commented Apr 9, 2025

Confirmed that the break came from v3.39.0:

version: '3'
tasks:
  parent:
    vars:
      VAR1: "value-from-parent"
    cmds:
      - defer:
          task: child
          vars:
            VAR1: 'task deferred {{.VAR1}}'
      - task: child
        vars:
          VAR1: 'task immediate {{.VAR1}}'
  child:
    cmds:
      - cmd: echo "child {{.VAR1}}"

Output comparison from 3.38.0 to 3.39.0:

$ go run github.com/go-task/task/v3/cmd/[email protected] parent # working
task: [child] echo "child task immediate value-from-parent"
child task immediate value-from-parent
task: [child] echo "child task deferred value-from-parent"
child task deferred value-from-parent

$ go run github.com/go-task/task/v3/cmd/[email protected] parent # broken
task: [child] echo "child task immediate value-from-parent"
child task immediate value-from-parent
task: [child] echo "child task deferred "
child task deferred

@vmaerten vmaerten added area: variables Changes related to variables. and removed state: needs triage Waiting to be triaged by a maintainer. labels Apr 10, 2025
@vmaerten vmaerten linked a pull request Apr 10, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: variables Changes related to variables.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants