Skip to content

Commit

Permalink
fix: experiment flags not working when .env is not in cwd (#1478)
Browse files Browse the repository at this point in the history
  • Loading branch information
pd93 authored Jan 26, 2024
1 parent d0efc1c commit e9392df
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion internal/experiments/experiments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit e9392df

Please sign in to comment.