From e9392df30bb6d1e313dbd56419a7a87ce9ddba8e Mon Sep 17 00:00:00 2001 From: Pete Davison Date: Fri, 26 Jan 2024 00:15:02 +0000 Subject: [PATCH] fix: experiment flags not working when .env is not in cwd (#1478) --- internal/experiments/experiments.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/experiments/experiments.go b/internal/experiments/experiments.go index 2ef2d581e8..5eb8b83d83 100644 --- a/internal/experiments/experiments.go +++ b/internal/experiments/experiments.go @@ -4,10 +4,12 @@ import ( "fmt" "io" "os" + "path" "strings" "text/tabwriter" "github.com/joho/godotenv" + "github.com/spf13/pflag" "golang.org/x/exp/slices" "github.com/go-task/task/v3/internal/logger" @@ -59,8 +61,28 @@ func getEnv(xName string) string { return os.Getenv(envName) } +func getEnvFilePath() string { + // Parse the CLI flags again to get the directory/taskfile being run + // We use a flagset here so that we can parse a subset of flags without exiting on error. + var dir, taskfile string + fs := pflag.NewFlagSet("experiments", pflag.ContinueOnError) + fs.StringVarP(&dir, "dir", "d", "", "Sets directory of execution.") + fs.StringVarP(&taskfile, "taskfile", "t", "", `Choose which Taskfile to run. Defaults to "Taskfile.yml".`) + _ = fs.Parse(os.Args[1:]) + // If the directory is set, find a .env file in that directory. + if dir != "" { + return path.Join(dir, ".env") + } + // If the taskfile is set, find a .env file in the directory containing the Taskfile. + if taskfile != "" { + return path.Join(path.Dir(taskfile), ".env") + } + // Otherwise just use the current working directory. + return ".env" +} + func readDotEnv() { - env, _ := godotenv.Read() + env, _ := godotenv.Read(getEnvFilePath()) // If the env var is an experiment, set it. for key, value := range env { if strings.HasPrefix(key, envPrefix) {