Skip to content

fix: ROOT_TASKFILE when Entrypoint is not set #1708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion internal/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-task/task/v3/internal/templater"
"github.com/go-task/task/v3/internal/version"
"github.com/go-task/task/v3/taskfile/ast"
"github.com/go-task/task/v3/taskfile/taskfile"
)

type Compiler struct {
Expand Down Expand Up @@ -180,11 +181,16 @@ func (c *Compiler) ResetCache() {
}

func (c *Compiler) getSpecialVars(t *ast.Task, call *ast.Call) (map[string]string, error) {
entrypoint := c.Entrypoint
if c.Entrypoint == "" {
entrypoint, _ = taskfile.GetAltTaskfile(c.Logger, c.Dir)
}

return map[string]string{
"TASK": t.Task,
"ALIAS": call.Task,
"TASK_EXE": filepath.ToSlash(os.Args[0]),
"ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, c.Entrypoint),
"ROOT_TASKFILE": filepathext.SmartJoin(c.Dir, entrypoint),
"ROOT_DIR": c.Dir,
"TASKFILE": t.Location.Taskfile,
"TASKFILE_DIR": filepath.Dir(t.Location.Taskfile),
Expand Down
5 changes: 3 additions & 2 deletions taskfile/node_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile/taskfile"
)

// A FileNode is a node that reads a taskfile from the local filesystem.
Expand Down Expand Up @@ -53,7 +54,7 @@ func (node *FileNode) Read(ctx context.Context) ([]byte, error) {
func resolveFileNodeEntrypointAndDir(l *logger.Logger, entrypoint, dir string) (string, string, error) {
var err error
if entrypoint != "" {
entrypoint, err = Exists(l, entrypoint)
entrypoint, err = taskfile.Exists(l, entrypoint)
if err != nil {
return "", "", err
}
Expand All @@ -68,7 +69,7 @@ func resolveFileNodeEntrypointAndDir(l *logger.Logger, entrypoint, dir string) (
return "", "", err
}
}
entrypoint, err = ExistsWalk(l, dir)
entrypoint, err = taskfile.ExistsWalk(l, dir)
if err != nil {
return "", "", err
}
Expand Down
3 changes: 2 additions & 1 deletion taskfile/node_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-task/task/v3/internal/execext"
"github.com/go-task/task/v3/internal/filepathext"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile/taskfile"
)

// An HTTPNode is a node that reads a Taskfile from a remote location via HTTP.
Expand Down Expand Up @@ -56,7 +57,7 @@ func (node *HTTPNode) Remote() bool {
}

func (node *HTTPNode) Read(ctx context.Context) ([]byte, error) {
url, err := RemoteExists(ctx, node.logger, node.URL, node.timeout)
url, err := taskfile.RemoteExists(ctx, node.logger, node.URL, node.timeout)
if err != nil {
return nil, err
}
Expand Down
27 changes: 17 additions & 10 deletions taskfile/taskfile.go → taskfile/taskfile/taskfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
)

var (
defaultTaskfiles = []string{
DefaultTaskfiles = []string{
"Taskfile.yml",
"taskfile.yml",
"Taskfile.yaml",
Expand Down Expand Up @@ -71,7 +71,7 @@ func RemoteExists(ctx context.Context, l *logger.Logger, u *url.URL, timeout tim

// If the request was not successful, append the default Taskfile names to
// the URL and return the URL of the first successful request
for _, taskfile := range defaultTaskfiles {
for _, taskfile := range DefaultTaskfiles {
// Fixes a bug with JoinPath where a leading slash is not added to the
// path if it is empty
if u.Path == "" {
Expand All @@ -97,6 +97,17 @@ func RemoteExists(ctx context.Context, l *logger.Logger, u *url.URL, timeout tim
return nil, errors.TaskfileNotFoundError{URI: u.String(), Walk: false}
}

func GetAltTaskfile(l *logger.Logger, path string) (string, error) {
for _, taskfile := range DefaultTaskfiles {
alt := filepathext.SmartJoin(path, taskfile)
if _, err := os.Stat(alt); err == nil {
l.VerboseOutf(logger.Magenta, "task: [%s] Not found - Using alternative (%s)\n", path, taskfile)
return alt, nil
}
}
return "", errors.TaskfileNotFoundError{URI: "", Walk: false}
}

// Exists will check if a file at the given path Exists. If it does, it will
// return the path to it. If it does not, it will search for any files at the
// given path with any of the default Taskfile files names. If any of these
Expand All @@ -114,15 +125,11 @@ func Exists(l *logger.Logger, path string) (string, error) {
return filepath.Abs(path)
}

for _, taskfile := range defaultTaskfiles {
alt := filepathext.SmartJoin(path, taskfile)
if _, err := os.Stat(alt); err == nil {
l.VerboseOutf(logger.Magenta, "task: [%s] Not found - Using alternative (%s)\n", path, taskfile)
return filepath.Abs(alt)
}
alt, err := GetAltTaskfile(l, path)
if err != nil {
return "", err
}

return "", errors.TaskfileNotFoundError{URI: path, Walk: false}
return filepath.Abs(alt)
}

// ExistsWalk will check if a file at the given path exists by calling the
Expand Down
Loading