go-task / task

A task runner / simpler Make alternative written in Go
https://taskfile.dev
MIT License
11.01k stars 584 forks source link

Working directory of included taskfiles differs depending on called task's location #903

Open deepbrook opened 1 year ago

deepbrook commented 1 year ago

Given the following Taskfiles:

# test/Taskfile.yaml
---
version: 3

includes:
  foo:
    taskfile: lib/foo.yaml
    dir: ./

tasks:
  pwd:
    cmds:
      - echo $PWD
  toplevel:foo-bar-pwd:
    cmds:
      - task: foo:bar:pwd
# test/lib/foo.yaml
---
version: 3

includes:
  bar:
    taskfile: ./bar.yaml
    dir: ./

tasks:
  pwd:
    cmds:
      - echo $PWD

  foolevel:bar-pwd:
    cmds:
      - task: bar:root
# test/lib/bar.yaml
---
version: 3
tasks:
  pwd:
    cmds:
      - echo $PWD

We get the following behaviour when calling the above tasks:

🐟 ❯ task pwd
task: [pwd] echo $PWD
/tmp/test

🐟 ❯ task foo:pwd
task: [foo:pwd] echo $PWD
/tmp/test

took 10s
🐟 ❯ task foo:foolevel:bar-pwd
task: [foo:bar:pwd] echo $PWD
/tmp/test/lib

🐟 ❯ task foo:bar:pwd
task: [foo:bar:pwd] echo $PWD
/tmp/test/lib

IMO, this is unexpected behaviour and a bug - as I'm calling all tasks from test/Taskfile.yaml, which specifically declares the exec directory should be ./ for the included taskfiles.

In otherwords, the correct behaviour should be as follows if running foo.yaml in the lib directory:

🐟 ❯ task -t foo.yaml pwd
task: [pwd] echo $PWD
/tmp/test/lib

🐟 ❯ task -t foo.yaml foolevel:bar-pwd
task: [bar:pwd] echo $PWD
/tmp/test/lib

🐟 ❯ task -t foo.yaml bar:pwd
task: [bar:pwd] echo $PWD
/tmp/test/lib

tl;dr: It looks like the value of dir is dependent on the location of task being called within the taskfiles includes tree, instead of being correctly merged/updated with the parent taskfile's dir when recursing into the included files.

Task Version

🐟 ❯ task --version
Task version: v3.17.0 (h1:tDkE4X8VUUSNzFMTQNuEEhR/3cfI9hILLuwm365sNCM=)
simonrouse9461 commented 4 months ago

Any update on this?

ShoGinn commented 3 months ago

So this is closest to the problem I'm experiencing.

Including a task file does change the working directory.

It is not a problem since I created a variable for my PROJECT_DIR

  PROJECT_DIR:
    sh: "git rev-parse --show-toplevel"

Here is my example using your examples:

# test/Taskfile.yaml
---
version: 3

vars:
  PROJECT_DIR:
    sh: pwd

includes:
  foo:
    taskfile: lib/foo.yaml
    dir: ./

tasks:
  pwd:
    cmds:
      - echo $PWD
  toplevel:foo-bar-pwd:
    cmds:
      - task: foo:bar:pwd
      - task: foo:bar:pwd:root
# No change to this file
# test/lib/foo.yaml
---
version: 3

includes:
  bar:
    taskfile: ./bar.yaml
    dir: ./

tasks:
  pwd:
    cmds:
      - echo $PWD

  foolevel:bar-pwd:
    cmds:
      - task: bar:root
# test/lib/bar.yaml
---
version: 3
tasks:
  pwd:
    dir: "{{ .PROJECT_DIR }}"
    cmds:
      - echo $PWD
  pwd:root:
    dir: "/{{ .PROJECT_DIR }}"
    cmds:
      - echo $PWD

I just want you to see the bar file where I added explicit directories to the tasks

Notice that the pwd:root has a preceding slash for the directory

{{.PROJECT_DIR}} in this case is dynamically set in the Taskfile.yaml

the output is this

 task toplevel:foo-bar-pwd
task: [foo:bar:pwd] echo $PWD
/task_bug/lib/task_bug
task: [foo:bar:pwd:root] echo $PWD
/task_bug

Not sure why you would need to specify the extra / my guess is they are linked somehow