godotenv
                                
                                 godotenv copied to clipboard
                                
                                    godotenv copied to clipboard
                            
                            
                            
                        Command does not load dotenv files if some do not exist
Hi @joho, I noticed some unexpected behavior when using godotenv from the command line. When passing multiple .env files, if one listed first does not exist, it looks like it skips processing the rest.
For example:
$ cat .env
FOO="Using .env"
$ godotenv -f ".env,.env.doesnotexist" env | grep FOO
FOO=Using .env
$ godotenv -f ".env.doesnotexist,.env" env | grep FOO
# FOO does not get set
Any thoughts on a solution for this?
Thanks in advance!
Hi @evandam,
I have looked at the godotenv project and came to the following conclusion that confirms your assumption.
When the any of the passed fileNames do not exist, it just silently stops the process of 'load env files' and start to execute the command provided.
I believe that behaviour might lead to unexpected results and assumptions which might affect the correctness of the system that utilizes the environment variables using godotenv command And decided to make small PR.
Current behaviour: Silently stopping the process of 'load env files' and continuing to execute the command provided.
$ cat .env
FOO="Using .env"
$ godotenv -f ".env,.env.doesnotexist" env | grep FOO
FOO=Using .env
$ godotenv -f ".env.doesnotexist,.env" env | grep FOO
# FOO does not get set
Considered behaviour: When the fileName do not exist, then print the message "The system cannot find the file spec" and exit.
$ cat .env
FOO="Using .env"
$ godotenv -f ".env,.env.doesnotexist" env | grep FOO
FOO=Using .env
$ godotenv -f ".env.doesnotexist,.env" env | grep FOO
open .env.doesnotexist: The system cannot find the file spec
@egasimov thanks a lot for the PR but I don't think it should error out. It'd be better if it was just skipping whatever file not existing but still load the files found. This is usually the pattern with dotenv.
Agreed with @scalp42. This way we can handle "optional" dotenv files like a .env.local that a developer may or may not have when they run in their environments.
@scalp42 welcome,
Agree with you, my previous change may also introduce breaking changes for the systems that already use it somehow.
After considering your thoughts(@scalp42, @evandam ), I made another PR that simply loads all the files which do exist and prints stderr about the filenames which are missing. I think that printing an informative message - might be helpful for people being aware of which files are not loaded by godotenv.