Skip to content

Commit 4afc0e8

Browse files
committed
Fixed some bugs and regressions regarding dynamic variables and directories
Closes #426
1 parent 141b377 commit 4afc0e8

File tree

10 files changed

+104
-30
lines changed

10 files changed

+104
-30
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fixed some bugs and regressions regarding dynamic variables and directories
6+
([#426](https://github.com/go-task/task/issues/426)).
57
- The [slim-sprig](https://github.com/go-task/slim-sprig) package was updated
68
with the upstream [sprig](https://github.com/Masterminds/sprig).
79

internal/compiler/v3/compiler_v3.go

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,48 @@ func (c *CompilerV3) GetVariables(t *taskfile.Task, call taskfile.Call) (*taskfi
3232
result := compiler.GetEnviron()
3333
result.Set("TASK", taskfile.Var{Static: t.Task})
3434

35-
// NOTE(@andreynering): We're manually joining these paths here because
36-
// this is the raw task, not the compiled one.
37-
dir := t.Dir
38-
if !filepath.IsAbs(dir) {
39-
dir = filepath.Join(c.Dir, dir)
40-
}
41-
42-
rangeFunc := func(k string, v taskfile.Var) error {
43-
tr := templater.Templater{Vars: result, RemoveNoValue: true}
44-
v = taskfile.Var{
45-
Static: tr.Replace(v.Static),
46-
Sh: tr.Replace(v.Sh),
47-
}
48-
if err := tr.Err(); err != nil {
49-
return err
50-
}
51-
static, err := c.HandleDynamicVar(v, dir)
52-
if err != nil {
53-
return err
35+
getRangeFunc := func(dir string) func(k string, v taskfile.Var) error {
36+
return func(k string, v taskfile.Var) error {
37+
tr := templater.Templater{Vars: result, RemoveNoValue: true}
38+
39+
v = taskfile.Var{
40+
Static: tr.Replace(v.Static),
41+
Sh: tr.Replace(v.Sh),
42+
Dir: v.Dir,
43+
}
44+
if err := tr.Err(); err != nil {
45+
return err
46+
}
47+
static, err := c.HandleDynamicVar(v, dir)
48+
if err != nil {
49+
return err
50+
}
51+
result.Set(k, taskfile.Var{Static: static})
52+
return nil
5453
}
55-
result.Set(k, taskfile.Var{Static: static})
56-
return nil
5754
}
55+
rangeFunc := getRangeFunc(c.Dir)
5856

5957
if err := c.TaskfileVars.Range(rangeFunc); err != nil {
6058
return nil, err
6159
}
6260
if err := call.Vars.Range(rangeFunc); err != nil {
6361
return nil, err
6462
}
65-
if err := t.Vars.Range(rangeFunc); err != nil {
63+
64+
// NOTE(@andreynering): We're manually joining these paths here because
65+
// this is the raw task, not the compiled one.
66+
tr := templater.Templater{Vars: result, RemoveNoValue: true}
67+
dir := tr.Replace(t.Dir)
68+
if err := tr.Err(); err != nil {
69+
return nil, err
70+
}
71+
if !filepath.IsAbs(dir) {
72+
dir = filepath.Join(c.Dir, dir)
73+
}
74+
taskRangeFunc := getRangeFunc(dir)
75+
76+
if err := t.Vars.Range(taskRangeFunc); err != nil {
6677
return nil, err
6778
}
6879

@@ -84,6 +95,11 @@ func (c *CompilerV3) HandleDynamicVar(v taskfile.Var, dir string) (string, error
8495
return result, nil
8596
}
8697

98+
// NOTE(@andreynering): If a var have a specific dir, use this instead
99+
if v.Dir != "" {
100+
dir = v.Dir
101+
}
102+
87103
var stdout bytes.Buffer
88104
opts := &execext.RunCommandOptions{
89105
Command: v.Sh,

task_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func TestGenerates(t *testing.T) {
308308
const (
309309
srcTask = "sub/src.txt"
310310
relTask = "rel.txt"
311-
absTask = "sub/abs.txt"
311+
absTask = "abs.txt"
312312
fileWithSpaces = "my text file.txt"
313313
)
314314

@@ -805,7 +805,10 @@ func TestDynamicVariablesShouldRunOnTheTaskDir(t *testing.T) {
805805
Target: "default",
806806
TrimSpace: false,
807807
Files: map[string]string{
808-
"subdirectory/dir.txt": "subdirectory\n",
808+
"subdirectory/from_root_taskfile.txt": "subdirectory\n",
809+
"subdirectory/from_included_taskfile.txt": "subdirectory\n",
810+
"subdirectory/from_included_taskfile_task.txt": "subdirectory\n",
811+
"subdirectory/from_interpolated_dir.txt": "subdirectory\n",
809812
},
810813
}
811814
tt.Run(t)

taskfile/read/taskfile.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ func Taskfile(dir string, entrypoint string) (*taskfile.Taskfile, error) {
9797
}
9898

9999
if includedTask.AdvancedImport {
100+
for k, v := range includedTaskfile.Vars.Mapping {
101+
o := v
102+
o.Dir = filepath.Join(dir, includedTask.Dir)
103+
includedTaskfile.Vars.Mapping[k] = o
104+
}
105+
for k, v := range includedTaskfile.Env.Mapping {
106+
o := v
107+
o.Dir = filepath.Join(dir, includedTask.Dir)
108+
includedTaskfile.Env.Mapping[k] = o
109+
}
110+
100111
for _, task := range includedTaskfile.Tasks {
101112
if !filepath.IsAbs(task.Dir) {
102113
task.Dir = filepath.Join(includedTask.Dir, task.Dir)

taskfile/var.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ type Var struct {
9898
Static string
9999
Live interface{}
100100
Sh string
101+
Dir string
101102
}
102103

103104
// UnmarshalYAML implements yaml.Unmarshaler interface.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
subdirectory/dir.txt
1+
*.txt
Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,33 @@
11
version: '3'
22

3+
includes:
4+
sub:
5+
taskfile: subdirectory
6+
dir: subdirectory
7+
8+
vars:
9+
DIRECTORY: subdirectory
10+
311
tasks:
412
default:
13+
- task: from-root-taskfile
14+
- task: sub:from-included-taskfile
15+
- task: sub:from-included-taskfile-task
16+
- task: from-interpolated-dir
17+
18+
from-root-taskfile:
519
cmds:
6-
- echo '{{.FOLDER}}' > dir.txt
20+
- echo '{{.TASK_DIR}}' > from_root_taskfile.txt
721
dir: subdirectory
822
vars:
9-
FOLDER:
23+
TASK_DIR:
1024
sh: basename $(pwd)
1125
silent: true
26+
27+
from-interpolated-dir:
28+
cmds:
29+
- echo '{{.INTERPOLATED_DIR}}' > from_interpolated_dir.txt
30+
dir: '{{.DIRECTORY}}'
31+
vars:
32+
INTERPOLATED_DIR:
33+
sh: basename $(pwd)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: '3'
2+
3+
vars:
4+
TASKFILE_DIR:
5+
sh: basename $(pwd)
6+
7+
tasks:
8+
from-included-taskfile:
9+
cmds:
10+
- echo '{{.TASKFILE_DIR}}' > from_included_taskfile.txt
11+
silent: true
12+
13+
from-included-taskfile-task:
14+
cmds:
15+
- echo '{{.TASKFILE_TASK_DIR}}' > from_included_taskfile_task.txt
16+
silent: true
17+
vars:
18+
TASKFILE_TASK_DIR:
19+
sh: basename $(pwd)

testdata/dir/dynamic_var/subdirectory/dir.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

testdata/generates/Taskfile.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
version: '3'
22

33
vars:
4-
BUILD_DIR: $pwd
4+
BUILD_DIR:
5+
sh: pwd
56

67
tasks:
7-
sub/abs.txt:
8+
abs.txt:
89
desc: generates dest file based on absolute paths
910
deps:
1011
- sub/src.txt

0 commit comments

Comments
 (0)