Skip to content
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

.env: no such file or directory #213

Open
chihao10711 opened this issue Aug 7, 2023 · 5 comments
Open

.env: no such file or directory #213

chihao10711 opened this issue Aug 7, 2023 · 5 comments

Comments

@chihao10711
Copy link

When I use go run it works well. However, when using go build couldn't be found .env.
Please help me with this?

@henrikac
Copy link

henrikac commented Aug 8, 2023

I just tried

// imports

func main() {
  err := godotenv.Load()
  if err != nil {
    log.Fatal("Error loading .env file")
  }
}

and then ran go run main.go and go build main.go && ./main without any issues.

Could you provide some more details?

@arieansyah
Copy link

arieansyah commented Aug 29, 2023

When I use go run it works well. However, when using go build couldn't be found .env. Please help me with this?

try this :
godotenv.Load("full_path")

go clean
go build

@coolaj86
Copy link
Contributor

It looks like this is as stale issue that can be closed?

cc/ @chihao10711

@beiriannydd
Copy link

beiriannydd commented Nov 2, 2023

so my expectation prior to reading the code would be that it would look for .env files in the list you gave it, loading the ones that exist, so that you could give it a list and it would load what it found. The reality is that it stops at the first missing file, due to a file not found error being treated as a hard error. This is a design decision, but is it what the user wants? I ended up writing a
function to filter the list to only existing files before passing to load, but of course this won't work with autoload.
I look in the current directory for .env.${ENVIRONMENT}, .env, then $(dirname ${GOMOD}) for the same.

Basically the Load() function here works like the ruby Load!() which I wouldn't expect given the project goal.

It would be a breaking API change for the folks who've installed and now expect this behavior though, so maybe you just want to add a function to trim down the file list to only existing ones which will then effectively make this work like Load on the result of that function.

@gwthm-in
Copy link

@beiriannydd Even I have similar requirements to try loading all files found and return an error at the end if any errors occur along with the ability to look at the root git directory, also at the go mod directory level.

Probably if you have similar requirements, you can utilise this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants