task icon indicating copy to clipboard operation
task copied to clipboard

Dynamic variable resolution does not take into account current directory

Open viktorasm opened this issue 9 months ago • 2 comments

Description

when creating dynamic variables like files: sh: "find ." in multiple subfolders, they won't be correctly evaluated. Due to internal caching, only the first result will be used.

Posted PR with reproduction and fix: https://github.com/go-task/task/pull/2160

Sample input

Folder structure:

root/
├── Taskfile.yaml
└── sub1/
    ├── Taskfile.yaml
    └── a
└── sub2/
    ├── Taskfile.yaml
    └── b

Taskfile.yml

version: '3'

run: when_changed
silent: true
output: prefixed

includes:
  s1:
    taskfile: sub1
    dir: sub1
  s2:
    taskfile: sub2
    dir: sub2

tasks:
  all:
    deps:
      - s1:all
      - s2:all

sub1/Taskfile.yaml, sub2/Taskfile.yaml:

version: '3'

vars:
  files:
    sh: "find ."

tasks:
  all:
    cmds:
      - cmd: 'echo  "{{.files}}"'

Actual output:

$ task all
[s2:all] .
[s2:all] ./Taskfile.yml
[s2:all] ./b
[s1:all] .
[s1:all] ./Taskfile.yml
[s1:all] ./b

Note how "s1" lists file "b", which is incorrect.

viktorasm avatar Apr 04 '25 11:04 viktorasm

The annoying thing I found about this bug is that it can be intermittent/randomly reproducible. It's definitely a regression too as it wasn't an issue before.

Using the TASK_DIR special variable has helped avoid cached values.

E.g.:

vars:
  files:
    sh: "find {{.TASK_DIR}}/."

There might be other cases that can't be fixed this way, but in my case I'm just going to stick to this fix regardless of any go-task bug fixes.

eak-jsplc avatar Jul 11 '25 14:07 eak-jsplc

@viktorasm this is likely fixed by #1649 .

Could you retest with the latest version of Task and confirm.

trulede avatar Dec 06 '25 13:12 trulede