dotenv icon indicating copy to clipboard operation
dotenv copied to clipboard

[dotenv-rails] .env.local should be loaded in test environments

Open erwanst opened this issue 5 years ago • 10 comments

Hi (and since it is my first contribution to the project: thank you maintainers for Dotenv !)

https://github.com/bkeepers/dotenv/pull/280 removed .env.local from the files loaded by Dotenv when Rails is loaded in the test environment.

As discussed in the last comments on the PR, that behavior can be unexpected and is too restrictive for some legitimate (common ?) setups. Having to duplicate variables from .env.local to .env.test.local on the grounds that .env.local should not be used in test environments doesn't really make sense in my opinion. Also, it is up to developers to make sure they configure their CI correctly, I don't think Dotenv should or even can strictly enforce what files are used for what. The documentation is already very clear about what should be ignored from git, and why, and maybe it is enough ?

I created at PR implementing the change: #417

I understand that changing the behavior of Dotenv on that point is tricky because it would probably break things in some projects that rely on .env.local not being loaded for tests, and I hope there is a way forward nonetheless.

Thanks for your time :)

Dotenv: v2.7.6

erwanst avatar Oct 26 '20 13:10 erwanst

Just stopping by to second this. It's counter-intuitive that there's such a special case for .env.local. In my case, database / redis ports live in .env.local, which applies to all (ie. development & test) environments. It means I have to copy everything over to .env.test.local and always remember to make changes in both files.

fschwahn avatar Nov 12 '20 12:11 fschwahn

Maintainers, I would be grateful for your feedback on this issue :)

If the change of behavior introduced by the attached PR is a blocker, and turning it into a conditional, opt-in feature would help, I would gladly amend the PR.

erwanst avatar Dec 28 '20 08:12 erwanst

Any chance this could get in? I also find the current behavior very unintuitive.

codedeleter avatar Mar 23 '21 05:03 codedeleter

Hello maintainers, friendly ping in the hope of getting your feedback on the issue :)

erwanst avatar Apr 20 '21 19:04 erwanst

Hi maintainers, is there a reason why, several months later, this issue still has no feed-back from you ? Again, I would understand if the issue is not a priority or not even a desired change. If that is the case, I'll simply maintain my own fork and be done with it. If not, is there a way forward ?

erwanst avatar Jun 01 '21 09:06 erwanst

I just had same error. Expecting .env.local to be used while running tests, but surprised when it was actually reading .env

I was going to say "docs should be updated" yet I just noticed:

Local overrides. This file is loaded for all environments except test.

Yet, I was confused by the Wherever the file is under environment. Probably the except test should be there as well

jjercx avatar Jun 14 '21 17:06 jjercx

Yup, I’m in the same boat—this behavior makes no sense, breaks expectations, and makes you manually keep values in sync across multiple files for no discernible benefit or reason.

codyrobbins avatar Nov 11 '21 20:11 codyrobbins

Just to contribute to the issue and keep it alive: this is extremely counter-intuituve behaviour with no good rationale behind it. Let's just fix it, it does not sound like a hard think to improve. You even have an existing PR! =)

ktimothy avatar Dec 30 '21 12:12 ktimothy

Not to pile on, but even the original author admits that this quite an opinionated behavior, and would be fine with reverting it: https://github.com/bkeepers/dotenv/pull/280#issuecomment-1011244924

fschwahn avatar Sep 23 '22 13:09 fschwahn

I too just got bit by this... I have my local database credentials in .env.local and was quite surprised that my tests would not work.

dgm avatar Mar 27 '23 17:03 dgm