Skip to content

Dynamic Variables only calculated once if "sh:" is identical #524

@is-jonreeves

Description

@is-jonreeves
  • Task version: 3.6
  • Operating System: Windows 10, Alpine Linux 3.13

It seems like Dynamic Variables that share the same verbatim sh: line will not get recomputed when run in their new context. For example if a global var is set to the current dir, but then a task with dir: ../ re-sets it, this value does not update for that task. If you make a slight tweak to the sh: command, it does work.

Example Taskfile showing the issue

version: '3'
silent: true

vars:
  PROJECT_ROOT:
    sh: echo $PWD

tasks:

  # Use Global Dynamic Var
  example1:
    cmds:
      - echo "Example 1"
      - echo "{{.PROJECT_ROOT}}"

  # Redefine Global Dynamic Var as Local Dynamic Var (with own dir context)
  example2:
    dir: ../
    vars:
      PROJECT_ROOT:
        sh: echo $PWD
    cmds:
      - echo "Example 2"
      - echo "{{.PROJECT_ROOT}}"

  # Create a new Local Dynamic Var (with own dir context)
  example3:
    dir: ../../
    vars:
      REPO_ROOT:
        sh: echo $PWD
    cmds:
      - echo "Example 3"
      - echo "{{.REPO_ROOT}}"

Expected:

Example 1
C:\Users\jon\Projects\taskfile-test
Example 2
C:\Users\jon\Projects
Example 3
C:\Users\jon

Actual:

Example 1
C:\Users\jon\Projects\taskfile-test
Example 2
C:\Users\jon\Projects\taskfile-test
Example 2
C:\Users\jon\Projects\taskfile-test

Workaround

If you tweak the sh: command to be unique each time its used, then it appears to force it to rerun correctly. So something like this will allow it to work:

sh: echo $PWD
sh: echo $PWD''
sh: echo $PWD''''  

Side-Note/Feature on Directories

I'll probably open a feature request for this, but the reason that I am jumping through the above hoops is that there doesn't seem to be an official "special variable" to get the Current Working Directory, which would be really handy. For commands that requires a Abs paths like Docker, obtaining these in a cross-platform manner can be annoyingly repetitive to mange. If there was a "special variable" with the current working directory and it was correctly affected by the dir: within each task, this would help a lot especially with the "slim-sprig" functions.

Metadata

Metadata

Assignees

Labels

No fields configured for support.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions