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

loading multiple files are not correctly overwriting env variables #498

Closed
jmouka opened this issue Mar 1, 2024 · 1 comment
Closed

Comments

@jmouka
Copy link

jmouka commented Mar 1, 2024

Looks like passing in multiple env files to Dotenv.load(files, overwrite:true) is not correctly overwriting values. It seems to me that when overwrite is true, the input files' order is reversed AND also keys are overwritten, which negates the overwriting. I think NOT reversing the file order would fix it.

Steps to reproduce

I have this issue in my Ruby on Jets project, but it can be reproduced with a simple CLI example.

.env.development has

FOO=123

.env.development.local has

FOO=abc

Running this command: dotenv -f '.env.development,.env.development.local' ruby -e "puts ENV['FOO']"
Correctly prints 123

Running this command: dotenv -o -f '.env.development,.env.development.local' ruby -e "puts ENV['FOO']"
Incorrectly prints 123

Expected behavior

I expect that overwriting would take the last value (ie abc).

Actual behavior

The -o option is not working, it effectively takes the first value set.

System configuration

dotenv version: 3.1.0

Rails version: n/a

Ruby version: 3.2.2

@bkeepers
Copy link
Owner

bkeepers commented May 6, 2024

@jmouka Unless I'm misunderstanding something, I think the current behavior is working as expected. You may just need to switch the order of the files.

Normally, dotenv's behavior is that the first file to set a varaible wins, so you would want to load the most specific files first, then shared or global files. For example:

$ dotenv -f .env.local,.env.global

With -o, you still pass the files in the same order, but the files will be evaluated in reverse order so the most specific file still wins.

$ dotenv -o -f .env.local,.env.global

Let me know if I'm missunderstandinng your question. See #460 for when this was last visited.

@bkeepers bkeepers closed this as completed May 6, 2024
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

2 participants