task icon indicating copy to clipboard operation
task copied to clipboard

Paths are not correctly resolved when using "dir"

Open Nantero1 opened this issue 3 years ago • 4 comments

  • Task version: v3.14.1 (h1:myTmEIbMbQx2D+g5lJvnbSqX531HmxiaQuefAqz8ISA=)
  • Operating System: 46 ~20.04.1-Ubuntu SMP Thu Jul 14 15:20:17 UTC 2022

Since minor version v3.14.1 we have problems to chdir into the correct directory using the following setup. Version 3.14.0 had no issues.

Example Taskfile showing the issue

Directory structure:

/home/user/git/repo/projectname/Taskfile.yaml
/home/user/git/repo/.Taskfiles/Generic.yaml

Taskfile.yaml:

# https://taskfile.dev

version: "3"

vars:
  project: "projectname"

includes:
  generics:
    taskfile: "../.Taskfiles/Generic.yaml"

tasks:
  default:
    cmds:
      - task -l
    silent: true

Generic.yaml:

version: "3"

vars:
  gitBase:
    sh: git rev-parse --show-toplevel

tasks:

  plan:
    desc: Example on strange "dir" variable templating
    dir: "{{.gitBase}}/{{.project}}"
    cmds:
      - pwd
      - echo "{.gitBase}{.project} {{.gitBase}}/{{.project}}"
      - echo "{.gitBase} {{.gitBase}}"

Expected behaviour

When using task version v3.14.0, the current dir is correct:

cd /home/user/git/repo/
git init
cd /home/user/git/repo/projectname/
task generics:plan

output:

task: [generics:plan] pwd
/home/user/git/repo/projectname
task: [generics:plan] echo "{.gitBase}{.project} /home/user/git/repo/projectname"
{.gitBase}{.project} /home/user/git/repo/projectname
task: [generics:plan] echo "{.gitBase} /home/user/git/repo"
{.gitBase} /home/user/git/repo

Actual behaviour

When using task version v3.14.1, the current dir is wrong:

cd /home/user/git/repo/projectname/
task generics:plan

output:

task: [generics:plan] pwd
/home/user/git/repo/projectname/home/user/git/repo/projectname
task: [generics:plan] echo "{.gitBase}{.project} /home/user/git/repo/projectname"
{.gitBase}{.project} /home/user/git/repo/projectname
task: [generics:plan] echo "{.gitBase} /home/user/git/repo"
{.gitBase} /home/user/git/repo

Please note the weird double path /home/user/git/repo/projectname/home/user/git/repo/projectname resulting the pwd command, see the first line. Other commands are only for debug purposes and show that the templating works for these other commands.

Due to this error no files in the current folder can be found.

Nantero1 avatar Aug 05 '22 09:08 Nantero1

I was planning to report the same thing. Thanks!

lucasfcnunes avatar Aug 06 '22 20:08 lucasfcnunes

Hi @Nantero1 and @lucasfcnunes,

I just took quite some time debugging this issue, and it's a bit tricky to fix. I'll write it here to help myself when I back to this.

The source of the problem is that you're using a value in dir: that starts with a template like {{.DIRECTORY}}.

There's a place in the code where the Taskfile dir in joined with the task dir like this:

/path/to/project + sub/dir = /path/to/project/sub/dir

There's an exception: if the task dir is already absolute (starts with / in linux/mac or C:/ in Windows) the joining is skipped.

The problem is that interpolation only happens in a later stage, so it's failing to detect that {{.DIRECTORY}} holds an absolute path, so it'll be joined like:

/path/to/project/{{.DIRECTORY}} which will be later expanded as /path/to/project/path/to/project.

While this is not fixed, you can change dir to have a relative path like ../../

andreynering avatar Aug 06 '22 21:08 andreynering

To fix that "issue", I did this:

tasks
  foo:
    dir: /$TMPDIR/{{.COMPONENT_NAME}}

The trick is to start with / even if $TMPDIR contains the slash… it's a way to say "force absolute". It's tricky and a bit ugly, but it works for now.

A better solution (like "if it starts with $ or {{, then it's absolute…) is still welcome 😇

davinkevin avatar Mar 22 '24 13:03 davinkevin