go-task / task

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

dir in task is buggy when using variables #1838

Open ccxuy opened 2 months ago

ccxuy commented 2 months ago

Task version: Task version: v3.38.0 (h1:O7kgA6BfwktXHPrheByQO46p3teKtRuq1EpGnFxNzbo=) Operating system: Ubuntu 2204 Experiments enabled: No

version: '3'

includes:
  taska:
    taskfile: tmp/a.yml

tasks:
  empty:
    cmds:
      - task: taska:task1
version: '3'

vars:
  location:
    sh: echo "/tmp"

tasks:
  task1:
    dir: "{{.location}}"
    cmds:
      - echo "dir is {{.location}}"
      - pwd
> task empty
task: [taska:task1] echo "dir is /tmp"
dir is /tmp
task: [taska:task1] pwd
/home/xxxx/tmp

I suppose it should be /tmp? If I change to dir: "/{{.location}}", it become what I expect.

  task1:
    dir: "/{{.location}}"
    cmds:
      - echo "dir is {{.location}}"
      - pwd
> task empty
task: [taska:task1] echo "dir is /tmp"
dir is /tmp
task: [taska:task1] pwd
/tmp

also, if I change to:

includes:
  taska: tmp/a.yml

output is also correct:

> task empty
task: [taska:task1] echo "dir is /tmp"
dir is /tmp
task: [taska:task1] pwd
/tmp
pd93 commented 2 months ago

I don't see anything wrong here. LOC is being set to "tmp" which is then being used as a relative directory in none. When you add the leading /, that path becomes absolute instead.

Some more context about what you're trying to do might help.

ccxuy commented 2 months ago

My bad, I posted a wrong example. Sorry for that, and I have update the original post. The bug is sub-task's dir creates a new sub-folder and execute on that new sub-folder, but location is set to /tmp

ccxuy commented 2 months ago
version: '3'

vars:
  LOC: "/tmp"

tasks:
  test:
    dir: "{{.LOC}}/a"
    vars:
      a:
        sh: "echo 1 |grep 1"
    cmds:
      - echo "dir is {{.LOC}}"
      - pwd
> task test
task: Command "echo 1 |grep 1" failed: chdir /a: no such file or directory
Failed at 55: task test

Also, if use sh for variable also behave buggy, looks like the grep command somehow affecte the location, which should be /tmp/a instead of /a

yonas commented 1 month ago

I'm having the same issue:

> mkdir /tmp/{a,b}
> cd /tmp/a
> task -v
task: [/tmp/a] Not found - Using alternative (Taskfile.yaml)
task: "default" started
task: [default] echo tmpdir = $TMP
tmpdir = /tmp/b
task: [default] echo inside dir $PWD
inside dir /tmp/a
task: "default" finished

Taskfile.yaml

version: '3'

dotenv: [.env]

vars:
  tmpdir: $TMP

tasks:

  default:
    dir: "{{ .tmpdir }}"
    cmds:
      - echo tmpdir = {{ .tmpdir }}
      - echo inside dir $PWD

.env

TMP=/tmp/b