godotenv icon indicating copy to clipboard operation
godotenv copied to clipboard

proposal: make env file optional

Open urbanishimwe opened this issue 5 years ago • 7 comments

When you call Load() or Overload() without args it returns an error open .env: no such file or directory. There is a situation where .env file is not necessary like deploying with docker or running tests using CI tools like CircleCI where the environment can be set in other ways. as such, it will be good if .env file is made optional so the app can use the existing os Env or get env through os.Args EDITED: check this comment for more https://github.com/joho/godotenv/issues/99#issuecomment-673937686

urbanishimwe avatar Apr 12 '20 13:04 urbanishimwe

I think an easy workaround would be adding an empty .env in Docker.

ivan-avalos avatar Apr 29 '20 22:04 ivan-avalos

I think an easy workaround would be adding an empty .env in Docker.

Kinda a shitty solution.

robbyoconnor avatar May 03 '20 04:05 robbyoconnor

You can check the error against os.IsNotExists instead of adding a workaround.

DeadlySurgeon avatar Aug 13 '20 21:08 DeadlySurgeon

@DeadlySurgeon the thing is, Overload and Load may receive a list of files to read envs from! when say one of the file doesn't exist those functions will return an error immediately without loading envs from subsequent files(the situation may turn worse if that file was not crucial and subsequent files was crucial), the workaround you provided would only work with loading from a single file! I raised a PR #100 for this...

urbanishimwe avatar Aug 14 '20 07:08 urbanishimwe

@urbanishimwe Sure, but in your example you're specifying with no args not with args, and in reality, in your CI/CD you shouldn't have any .env files so it should be fine to ignore the error if os.IsNotExist(err). Realistically, you should not be loading .env files outside of a local development machine and it boils down to a proper workflow. CI/CDs should store secrets, deployed environments should have their environmental variables managed by ops and not deployed in a repository.

DeadlySurgeon avatar Aug 14 '20 12:08 DeadlySurgeon

@DeadlySurgeon you can have secret environments in .env and non-secret environments in .noscret.env, if loading .env fails it shouldn't affect .noscret.env(optionally). I also think that not every project has to follow this model(especially in large scale applications)

CI/CDs should store secrets, deployed environments should have their environmental variables managed by ops and not deployed in a repository.

urbanishimwe avatar Aug 14 '20 13:08 urbanishimwe

The method is os.IsNotExist() not os.IsNotExists() - https://golang.org/pkg/os/#IsNotExist. Here is my implementation:

if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
	log.Fatalln("Error loading .env")
}

guizoxxv avatar Jan 14 '21 11:01 guizoxxv