Skip to content

Dynamic variables use the wrong directory when the same Taskfile is included multiple times #2928

Description

@ikshantshukla123

Description

Description

When the same Taskfile is included multiple times with different dir values, dynamic variables may be evaluated using the directory configured for another inclusion.

Each included task works correctly when executed separately. However, when both included tasks are executed in the same Task invocation, their dynamic-variable directory context appears to be incorrectly shared.

I reproduced this on the latest main commit:

81a49a4

Steps to reproduce

Create the following structure:

example/
├── Taskfile.yml
├── included/
│ └── Taskfile.yml
└── workspace/
├── backend/
└── frontend/

Use the following root Taskfile.yml:

version: "3"

includes:
backend:
taskfile: ./included/Taskfile.yml
dir: ./workspace/backend

frontend:
taskfile: ./included/Taskfile.yml
dir: ./workspace/frontend

tasks:
default:
cmds:
- task: backend:show
- task: frontend:show

Use the following included Taskfile at included/Taskfile.yml:

version: "3"

tasks:
show:
vars:
CURRENT_DIR:
sh: basename "$(pwd)"

cmds:
  - 'echo "{{.CURRENT_DIR}}" > result.txt'

Create the required directories:

mkdir -p workspace/backend workspace/frontend

Run both tasks in the same invocation:

task backend:show frontend:show

Inspect the generated files:

cat workspace/backend/result.txt
cat workspace/frontend/result.txt
Expected behavior

Each dynamic variable should be evaluated using its inclusion's configured directory:

workspace/backend/result.txt -> backend
workspace/frontend/result.txt -> frontend
Actual behavior

Both files contain the backend directory name:

workspace/backend/result.txt -> backend
workspace/frontend/result.txt -> backend

The frontend:show command itself executes inside workspace/frontend, but its dynamic variable contains the value evaluated for workspace/backend.

Additional observations

Both tasks work correctly when executed separately:

task backend:show

Produces: backend

task frontend:show

Produces: frontend

The problem occurs when both included tasks are executed within the same Task invocation:

task backend:show frontend:show

The following screenshot demonstrates the difference between running the tasks separately and together:

Dynamic variables produce correct values separately but share the wrong directory when tasks run together Regression test

I also created a regression test using the existing fileContentTest helper.

It currently fails with:

expected: "frontend\n"
actual: "backend\n"

I would be happy to investigate this further and work on a fix if the behavior is confirmed as unintended.

Version

3.52.0

Operating system

ProductName: macOS ProductVersion: 26.3.1 ProductVersionExtra: (a) BuildVersion: 25D771280a arm64

Experiments Enabled

No response

Example Taskfile

version: "3"

includes:
  backend:
    taskfile: ./included/Taskfile.yml
    dir: ./workspace/backend

  frontend:
    taskfile: ./included/Taskfile.yml
    dir: ./workspace/frontend

tasks:
  default:
    cmds:
      - task: backend:show
      - task: frontend:show

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: needs triageWaiting to be triaged by a maintainer.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions