From c7ba42b81a996db2da51e641c86511f35e67bdfd Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Thu, 25 Jan 2024 12:36:31 +0000 Subject: [PATCH] fix: resolve directory correctly when using --dir --- taskfile/node.go | 3 ++- taskfile/node_file.go | 4 ++++ taskfile/node_http.go | 4 ++++ taskfile/node_stdin.go | 8 +++++++- taskfile/reader.go | 5 ++--- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/taskfile/node.go b/taskfile/node.go index 7957ca37..8b9dc287 100644 --- a/taskfile/node.go +++ b/taskfile/node.go @@ -16,6 +16,7 @@ type Node interface { Location() string Optional() bool Remote() bool + BaseDir() string } func NewRootNode( @@ -26,7 +27,7 @@ func NewRootNode( // Check if there is something to read on STDIN stat, _ := os.Stdin.Stat() if (stat.Mode()&os.ModeCharDevice) == 0 && stat.Size() > 0 { - return NewStdinNode() + return NewStdinNode(dir) } // If no entrypoint is specified, search for a taskfile if entrypoint == "" { diff --git a/taskfile/node_file.go b/taskfile/node_file.go index bc96148e..45a2a9c3 100644 --- a/taskfile/node_file.go +++ b/taskfile/node_file.go @@ -52,3 +52,7 @@ func (node *FileNode) Read(ctx context.Context) ([]byte, error) { defer f.Close() return io.ReadAll(f) } + +func (node *FileNode) BaseDir() string { + return node.Dir +} diff --git a/taskfile/node_http.go b/taskfile/node_http.go index c19d4672..f4c2ab11 100644 --- a/taskfile/node_http.go +++ b/taskfile/node_http.go @@ -65,3 +65,7 @@ func (node *HTTPNode) Read(ctx context.Context) ([]byte, error) { return b, nil } + +func (node *HTTPNode) BaseDir() string { + return "" +} diff --git a/taskfile/node_stdin.go b/taskfile/node_stdin.go index b942ce87..c16a64a5 100644 --- a/taskfile/node_stdin.go +++ b/taskfile/node_stdin.go @@ -10,12 +10,14 @@ import ( // A StdinNode is a node that reads a taskfile from the standard input stream. type StdinNode struct { *BaseNode + Dir string } -func NewStdinNode() (*StdinNode, error) { +func NewStdinNode(dir string) (*StdinNode, error) { base := NewBaseNode() return &StdinNode{ BaseNode: base, + Dir: dir, }, nil } @@ -38,3 +40,7 @@ func (node *StdinNode) Read(ctx context.Context) ([]byte, error) { } return stdin, nil } + +func (node *StdinNode) BaseDir() string { + return node.Dir +} diff --git a/taskfile/reader.go b/taskfile/reader.go index a09e495c..c8ca8bdf 100644 --- a/taskfile/reader.go +++ b/taskfile/reader.go @@ -48,12 +48,11 @@ func Read( return nil, &errors.TaskfileVersionCheckError{URI: node.Location()} } - // Annotate any included Taskfile reference with a base directory for resolving relative paths - if node, isFileNode := node.(*FileNode); isFileNode { + if dir := node.BaseDir(); dir != "" { _ = tf.Includes.Range(func(namespace string, include ast.Include) error { // Set the base directory for resolving relative paths, but only if not already set if include.BaseDir == "" { - include.BaseDir = node.Dir + include.BaseDir = dir tf.Includes.Set(namespace, include) } return nil