T
Task2w ago
Explorer

dotenv merges in inverse order?

Hi, when specifying two dotenv files that contain common keys, it seems that priority is given to the first file instead of the last file. This seems counter-intuitive to me and is the opposite of what was found in https://discord.com/channels/974121106208354339/1064231491883782215/1064231491883782215. first.env:
A=a
B=b
A=a
B=b
second.env:
B=x
C=c
B=x
C=c
Taskfile.yml:
version: "3"
dotenv: ["first.env", "second.env"]
tasks:
test: echo Variable B holds value $B
version: "3"
dotenv: ["first.env", "second.env"]
tasks:
test: echo Variable B holds value $B
Running "task test" gives as output
Variable B holds value b
Variable B holds value b
while I would expect
Variable B holds value x
Variable B holds value x
Is it a bug or a feature?
Solution:
IIRC there was some discussion at the time on the priority order. We made a deliberate decision to implement this way. The idea is to follow the behavior of how .env files usually work. They do not override values that are already set on the ENV. In the same way, we loop over the list of .env files and only set the ENVs that were not previously set....
Jump to solution
3 Replies
pd93
pd932w ago
I agree that this seem counterintuitive. The docs don't mention precedence, so I'm not sure if this was intended or not. The introduction of dotenv predates my time as a maintainer. Maybe @andreynering knows. The problem with changing this now is that it would probably break many people's Taskfiles. It's something that we could potentially look into changing for a future major version though if it is decided that this is incorrect
Solution
andreynering
andreynering2w ago
IIRC there was some discussion at the time on the priority order. We made a deliberate decision to implement this way. The idea is to follow the behavior of how .env files usually work. They do not override values that are already set on the ENV. In the same way, we loop over the list of .env files and only set the ENVs that were not previously set.
Explorer
ExplorerOP7d ago
Ok, thanks for the clarification!

Did you find this page helpful?