go-task / task

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

Relative paths to included taskfiles not consistently resolved #822

Closed theunrepentantgeek closed 2 years ago

theunrepentantgeek commented 2 years ago

When resolving taskfile imports using relative paths, the base directory used to resolve the path isn't consistent, resulting in attempts to read the taskfile from the wrong folder.

Example

Directory structure:

# /path/to/project/Taskfile.yaml
includes:
  alpha:
    taskfile: ./alpha/Taskfile.yaml
    dir: ./alpha
  common: 
    taskfile: ./common/Taskfile.yaml
    dir: ./common
# /path/to/project/alpha/Taskfile.yaml
includes:
  common: 
    taskfile: ../common/Taskfile.yaml
    dir: ../common

Expected behaviour

Relative path references to included Taskfiles resolved from the directory containing the including taskfile.

Actual behaviour

The path reference to ../common/Taskfile.yaml from /path/to/project/alpha/Taskfile.yaml is resolved using the folder /path/to/project/, resulting in the path /path/to/common/Taskfile.yaml which isn't found.

Analysis

The function Taskfile() from taskfile/read/taskfile.go reads in the hierarchy of includes, merging included taskfiles in as it goes.

When reading /path/to/project/alpha/Taskfile.yaml the inclusion of ../common/Taskfile.yaml is correctly resolved to /path/to/project/common/Taskfile.yaml and the tasks are read in.

Everything from /path/to/project/alpha/Taskfile.yaml is then merged into root taskfile found in /path/to/project/Taskfile.yaml, including the nested include of ../common/Taskfile.yaml.

This is then processed a second time, resolving to /path/to/common/Taskfile.yaml, triggering an error.

antdking commented 2 years ago

I've also just run into this while setting up common tasks to share amongst all projects.

Tried to work around it by specifying a WORKSPACE_ROOT var, however there's no templating available for the taskfile parameter either (we need to be able to execute task from the workspace root and from project directories).

Thanks for flagging + preparing a patch, hopefully it gets released soon