diff --git a/cmd/task/task.go b/cmd/task/task.go index b424bfacd7..2c33fc4a80 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -5,7 +5,6 @@ import ( "fmt" "log" "os" - "path/filepath" "strings" "time" @@ -202,10 +201,6 @@ func run() error { if flags.dir != "" && flags.entrypoint != "" { return errors.New("task: You can't set both --dir and --taskfile") } - if flags.entrypoint != "" { - flags.dir = filepath.Dir(flags.entrypoint) - flags.entrypoint = filepath.Base(flags.entrypoint) - } if flags.output.Name != "group" { if flags.output.Group.Begin != "" { diff --git a/setup.go b/setup.go index c3430e7770..73fb2fe95d 100644 --- a/setup.go +++ b/setup.go @@ -55,8 +55,17 @@ func (e *Executor) Setup() error { } func (e *Executor) setCurrentDir() error { - // If the entrypoint is already set, we don't need to do anything - if e.Entrypoint != "" { + if e.Entrypoint != "" && e.Dir != "" { + return errors.New("cannot set both entrypoint and dir") + } + + // Remove the file protocol prefix if it exists + e.Entrypoint = strings.TrimPrefix(e.Entrypoint, "file://") + + // If the entrypoint is already set and isn't a remote file + if e.Entrypoint != "" && !strings.Contains(e.Entrypoint, "://") { + e.Dir = filepath.Dir(e.Entrypoint) + e.Entrypoint = filepath.Base(e.Entrypoint) return nil } diff --git a/task_test.go b/task_test.go index 8370afce59..48f4f24e64 100644 --- a/task_test.go +++ b/task_test.go @@ -56,7 +56,7 @@ func (fct fileContentTest) Run(t *testing.T) { for name, expectContent := range fct.Files { t.Run(fct.name(name), func(t *testing.T) { - path := filepathext.SmartJoin(fct.Dir, name) + path := filepathext.SmartJoin(e.Dir, name) b, err := os.ReadFile(path) require.NoError(t, err, "Error reading file") s := string(b) @@ -1162,8 +1162,7 @@ func TestIncludesOptionalExplicitFalse(t *testing.T) { func TestIncludesFromCustomTaskfile(t *testing.T) { tt := fileContentTest{ - Dir: "testdata/includes_yaml", - Entrypoint: "Custom.ext", + Entrypoint: "testdata/includes_yaml/Custom.ext", Target: "default", TrimSpace: true, Files: map[string]string{ @@ -1525,16 +1524,12 @@ func TestDotenvShouldIncludeAllEnvFiles(t *testing.T) { } func TestDotenvShouldErrorWhenIncludingDependantDotenvs(t *testing.T) { - const dir = "testdata/dotenv/error_included_envs" - const entry = "Taskfile.yml" - var buff bytes.Buffer e := task.Executor{ - Dir: dir, - Entrypoint: entry, - Summary: true, - Stdout: &buff, - Stderr: &buff, + Dir: "testdata/dotenv/error_included_envs", + Summary: true, + Stdout: &buff, + Stderr: &buff, } err := e.Setup()