Skip to content

cannot reference loop variable using .ref #2505

@zleonid1

Description

@zleonid1

Description

in a for loop cmd, the loop variable cannot be passed using .ref method

this works:

for:
  var: any_iterable
  as: this
task: process
vars:
  thing: '{{ .this }}'

This does not:

for:
  var: any_iterable
  as: this
task: process
vars:
  thing:
    ref: .this

Version

3.45.4

Operating system

linux

Experiments Enabled

No response

Example Taskfile

version: 3

silent: true

vars:
  global_var: global
  any_iterable: some thing


tasks:

  process:
    vars:
      thing:
        ref: default "undefined" .thing
    cmd: echo '{{ .thing }}'

  default:
    cmds:
      - task: cannot-pass-loop-variable--by-ref
      - task: cannot-pass-loop-variable--by-ref-default-name
      - task: cannot-pass-loop-variable--default-by-caller-does-not-see-it
      - task: can-pass-variable-by-ref
      - task: can-pass-loop-variable--by-template

  can-pass-variable-by-ref:
    cmds:
      - echo '[!!!] no loop | using .ref with a global variable'
      - task: process
        vars:
          thing:
            ref: .global_var

  cannot-pass-loop-variable--by-ref-default-name:
    cmds:
      - echo '[!!!] loop | using .ITEM'
      - for:
          var: any_iterable
        task: process
        vars:
          thing:
            ref: .ITEM

  cannot-pass-loop-variable--by-ref:
    cmds:
      - echo '[!!!] loop | using varible alias'
      - for:
          var: any_iterable
          as: this
        task: process
        vars:
          thing:
            ref: .this

  can-pass-loop-variable--by-template:
    cmds:
      - echo '[!!!] loop | using template'
      - for:
          var: any_iterable
          as: this
        task: process
        vars:
          thing: '{{ .this }}'

  cannot-pass-loop-variable--default-by-caller-does-not-see-it:
    cmds:
      - echo '[!!!] loop | setting fallback on (invoker) task'
      - for:
          var: any_iterable
          as: this
        task: process
        vars:
          thing:
            ref: default "invoker_default" .this

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

    Issue actions