task icon indicating copy to clipboard operation
task copied to clipboard

flatten include default shell working directory is wrong

Open gergely-pepper opened this issue 6 months ago • 1 comments

Description

Minimal reproducible demo: Taskfile.yml:

version: '3'

includes:
  backend:
    taskfile: taskfiles/backend.yml
    flatten: true

taskfiles/backend.yml:

version: '3'

includes:
  common:
    taskfile: ./common.yml
    flatten: true

tasks:
  echo-pwd:
    desc: Prints current working directory and list of files.
    cmds:
      - echo {{.PWD}} && ls -l
      - task: echo-pwd2

taskfiles/common.yml:

version: '3'

tasks:
  echo-pwd2:
    desc: Prints current working directory and list of files.
    cmds:
      - echo {{.PWD}} && ls -l

Output:

> task echo-pwd
task: [echo-pwd] echo /tmp/task-include-dir && ls -l
/tmp/task-include-dir
total 8
-rw-r--r--@ 1 panda  staff   89 Jún  9 11:57 Taskfile.yml
drwxr-xr-x  4 panda  staff  128 Jún  9 11:58 taskfiles
task: [echo-pwd2] echo /tmp/task-include-dir && ls -l
/tmp/task-include-dir
total 16
-rw-r--r--@ 1 panda  staff  226 Jún  9 11:58 backend.yml
-rw-r--r--@ 1 panda  staff  137 Jún  9 11:58 common.yml

It seems that {{.PWD}} correctly contains /tmp/task-include-dir, but the shell PWD is actually the taskfiles subdirectory. The correct output should look like

> task echo-pwd
task: [echo-pwd] echo /tmp/task-include-dir && ls -l
/tmp/task-include-dir
total 8
-rw-r--r--@ 1 panda  staff   89 Jún  9 11:57 Taskfile.yml
drwxr-xr-x  4 panda  staff  128 Jún  9 11:58 taskfiles
task: [echo-pwd2] echo /tmp/task-include-dir && ls -l
/tmp/task-include-dir
total 8
-rw-r--r--@ 1 panda  staff   89 Jún  9 11:57 Taskfile.yml
drwxr-xr-x  4 panda  staff  128 Jún  9 11:58 taskfiles

Version

3.43.3

Operating system

MacOS M1 Sequoia 15.5

Experiments Enabled

No response

Example Taskfile


gergely-pepper avatar Jun 09 '25 10:06 gergely-pepper

Typically PWD is an environment variable, it's probably coming from your shell when you start task.

"Include" documentation suggests that this is working, default value for dir is the parent Taskfile directory, therefore you might want to explicitly specify the dir with one of the special variables.

trulede avatar Jun 10 '25 23:06 trulede

PWD is not set by tasks and will most likely be the Current Working Directory, when the user called task.

Parent File ..../parent/Taskfile.yaml

version: '3'

tasks:
  echo-parent:
    cmd: echo "PWD {{ .PWD }}, TASKFILE_DIR {{ .TASKFILE_DIR }}"

Sub File parent/sub/Tasfile.yaml

version: '3'

includes:
  parent:
    taskfile: ../Taskfile.yaml
    flatten: true


tasks:
  echo-sub:
    cmd: echo "PWD {{ .PWD }}, TASKFILE_DIR {{ .TASKFILE_DIR }}"

Then in parent/sub directory call

task echo-sub echo-parent ❯ t echo-sub echo-parent --silent PWD /Users/noob/parent/sub, TASKFILE_DIR /Users/noob/parent/sub PWD /Users/noob/parent/sub, TASKFILE_DIR /Users/noob/parent

So the correct Variable to use would be TASKFILE_DIR if I am not mistaken

deicon avatar Jun 19 '25 16:06 deicon

Hello!

@trulede and @deicon are right

There are two things to note:

  • {{.PWD}} : This will always be /tmp/task-include-dir, as it comes from your shell when you start task.
  • The working directory : This is where the commands are executed. You can customize it with dir, but by default, it’s the parent’s directory.

Examples:

  • Backend included by Taskfile.yml → dir = /tmp/task-include-dir
  • Common included by taskfile/backend.yml → dir = /tmp/task-include-dir

vmaerten avatar Aug 10 '25 15:08 vmaerten