Skip to content

Commit 698d5d2

Browse files
committed
Fix handling of cli flag dir.
1 parent c62f9c7 commit 698d5d2

5 files changed

Lines changed: 29 additions & 6 deletions

File tree

compiler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Compiler struct {
2222
Dir string
2323
Entrypoint string
2424
UserWorkingDir string
25+
RootDir string
2526

2627
TaskfileEnv *ast.Vars
2728
TaskfileVars *ast.Vars
@@ -203,8 +204,8 @@ func (c *Compiler) ResetCache() {
203204
func (c *Compiler) getSpecialVars(t *ast.Task, call *Call) (map[string]string, error) {
204205
allVars := map[string]string{
205206
"TASK_EXE": filepath.ToSlash(os.Args[0]),
206-
"ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint),
207-
"ROOT_DIR": c.Dir,
207+
"ROOT_TASKFILE": filepathext.SmartJoin(c.RootDir, c.Entrypoint),
208+
"ROOT_DIR": c.RootDir,
208209
"USER_WORKING_DIR": c.UserWorkingDir,
209210
"TASK_VERSION": version.GetVersion(),
210211
}

executor.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"io"
66
"os"
7+
"path/filepath"
78
"sync"
89
"time"
910

@@ -69,6 +70,7 @@ type (
6970
OutputStyle ast.Output
7071
TaskSorter sort.Sorter
7172
UserWorkingDir string
73+
RootDir string
7274
EnableVersionCheck bool
7375

7476
fuzzyModel *fuzzy.Model
@@ -102,6 +104,7 @@ func NewExecutor(opts ...ExecutorOption) *Executor {
102104
OutputStyle: ast.Output{},
103105
TaskSorter: sort.AlphaNumericWithRootTasksFirst,
104106
UserWorkingDir: "",
107+
RootDir: "",
105108
fuzzyModel: nil,
106109
concurrencySemaphore: nil,
107110
taskCallCount: map[string]*int32{},
@@ -132,7 +135,15 @@ type dirOption struct {
132135
}
133136

134137
func (o *dirOption) ApplyToExecutor(e *Executor) {
135-
e.Dir = o.dir
138+
if len(o.dir) > 0 {
139+
if filepath.IsAbs(o.dir) {
140+
e.Dir = o.dir
141+
} else {
142+
if d, err := filepath.Abs(o.dir); err == nil {
143+
e.Dir = d
144+
}
145+
}
146+
}
136147
}
137148

138149
// WithEntrypoint sets the entrypoint (main Taskfile) of the [Executor]. By

internal/flags/flags.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ func (o *flagsOption) ApplyToExecutor(e *task.Executor) {
275275
if err == nil {
276276
dir = home
277277
}
278+
} else {
279+
if len(dir) > 0 {
280+
// Use the cli provided directory.
281+
if d, err := filepath.Abs(dir); err == nil {
282+
dir = d
283+
}
284+
}
278285
}
279286

280287
e.Options(

setup.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ func (e *Executor) getRootNode() (taskfile.Node, error) {
6767
if err != nil {
6868
return nil, err
6969
}
70-
e.Dir = node.Dir()
70+
e.RootDir = node.Dir()
71+
if len(e.Dir) == 0 {
72+
// Only set the executor dir if it was not already set.
73+
e.Dir = node.Dir()
74+
}
7175
e.Entrypoint = node.Location()
7276
return node, err
7377
}
@@ -216,11 +220,11 @@ func (e *Executor) setupCompiler() error {
216220
return err
217221
}
218222
}
219-
220223
e.Compiler = &Compiler{
221224
Dir: e.Dir,
222225
Entrypoint: e.Entrypoint,
223226
UserWorkingDir: e.UserWorkingDir,
227+
RootDir: e.RootDir,
224228
TaskfileEnv: e.Taskfile.Env,
225229
TaskfileVars: e.Taskfile.Vars,
226230
Logger: e.Logger,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{.TEST_DIR}}/testdata/special_vars/foo
1+
{{.TEST_DIR}}/testdata/special_vars/subdir/foo

0 commit comments

Comments
 (0)