dir in task is buggy when using variables
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
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.
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
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
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
Same issue here. The problem arises from code calling filepathext.SmartJoin while the dir content is a variable that has not yet been expanded, therefore the dir is seen as relative even if it may be an absolute path This line here is wrong when task.Dir is a variable that may be expanded later: https://github.com/go-task/task/blob/main/taskfile/ast/tasks.go#L206
Moreover it looks like the current code is working around this issue by using a hack for know-absolute variable:
https://github.com/go-task/task/blob/main/internal/filepathext/filepathext.go#L28
IHOM a proper fix to this issue should also lead to remove this hack
var knownAbsDirs = []string{
".ROOT_DIR",
".TASKFILE_DIR",
".USER_WORKING_DIR",
}
func isSpecialDir(dir string) bool {
for _, d := range knownAbsDirs {
if strings.Contains(dir, d) {
return true
}
}
return false
}
I have the exact same issue as the OP above when using dir inside included tasks.
Adding the leading / sorts it but I appreciate it is a hack for now so I will eagerly wait for any update to this 🤗
kubernetes.taskfile.yml
# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: "3"
tasks:
info:
dir: "{{.CHEF_REPO_DIR}}"
cmds:
- echo "Root {{.ROOT_DIR}}"
- echo "Current {{.TASKFILE_DIR}}"
- echo "Working {{.USER_WORKING_DIR}}"
- echo "CHEF {{.CHEF_REPO_DIR}}"
- echo "pwd $(pwd)"
Taskfile.yml
# yaml-language-server: $schema=https://taskfile.dev/schema.json
version: "3"
includes:
kubernetes:
taskfile: "{{.MHM_PERSONAL_REPO_CHECKOUT}}/kubernetes.taskfile.yml"
aliases:
- k8s
vars:
CHEF_REPO_DIR:
ref: .CHEF_REPO_DIR
vars:
# Set the path to your checkout of chef-repo
CHEF_REPO_DIR: "/home/mhm/Documents/chef-repo"
MHM_PERSONAL_REPO_CHECKOUT: "/home/mhm/mhm_personal"
Output
mhm@mhm:~/mhm_personal$ task k8s:info
task: [kubernetes:info] echo "Root /home/mhm/mhm_personal"
Root /home/mhm/mhm_personal
task: [kubernetes:info] echo "Current /home/mhm/mhm_personal"
Current /home/mhm/mhm_personal
task: [kubernetes:info] echo "Working /home/mhm/mhm_personal"
Working /home/mhm/mhm_personal
task: [kubernetes:info] echo "CHEF /home/mhm/Documents/chef-repo"
CHEF /home/mhm/Documents/chef-repo
task: [kubernetes:info] echo "pwd $(pwd)"
pwd /home/mhm/mhm_personal/home/mhm/Documents/chef-repo
mhm@mhm:~/mhm_personal$ ls -lah
total 128K
drwxrwxr-x 10 mhm mhm 4.0K Aug 21 13:10 .
drwxr-x--- 39 mhm mhm 4.0K Aug 21 12:44 ..
-rw-rw-r-- 1 mhm mhm 11K Aug 21 13:11 kubernetes.taskfile.yml
-rw-rw-r-- 1 mhm mhm 18K Aug 21 13:06 Taskfile.yml
ACTUAL: /home/mhm/mhm_personal/home/mhm/Documents/chef-repo
EXPECTED: /home/mhm/Documents/chef-repo
This issue could be resolved by PR #2445, you might want to try that PR and see if it helps.
Duplicate of #951 #2443
Thanks for taking care of htis @trulede Just for you know, Github has a feature to close with duplicated and not completed: