allan2 / dotenvy

A well-maintained fork of the dotenv crate
MIT License
685 stars 43 forks source link

no substition on cases where the variable does not exist #100

Closed sassman closed 7 hours ago

sassman commented 5 months ago

given

an .env file with 3 entries, where 2 are using a $ sign in their value. The $ does not reference any valid env variable. e.g.:

KEY1=value1
KEY2=$NOKEY
KEY3=$NOKEY+valueU

expectations

I would argue that behavior is sound because there is no env variable named NOKEY, hence there is no substitution happening.

Note: But I have to acknowledge that bash and other shells behave the way it is currently implemented.

aidenfarley commented 5 months ago

I'm still a little confused by your issue. $NOKEY isn't defined anywhere in your file. Do you want it to be an empty, or is it defined outside of the .env file?

Basically, what is the behavior you're experiencing, what is your issue with it, and what is the behavior you expect?

sassman commented 5 months ago

So right now the issue is that there is a substitution happening and, since NOKEY is not defined, it will be treated as if it would be set to an empty string. So that the KEY2 becomes empty.

But, let's pretend you have never heard of shell variable magic, $NOKEY if foremost a string and you wonder why it is changed at all.

The real-life issue is, that we encountered was a password, that contained at some place a $ sign.

like MY_API_KEY="foobarbak$wahteverfoo" and this got altered at runtime to foobarbak and I was on a hunt to understand why the heck. Then realized of course that in bash the $ is interpreted in strings that are wrapped into double quotes. But not in single quotes.

So the solution for me was to define this variable like MY_API_KEY='foobarbak$wahteverfoo'

But this was far from intuitive, especially I was wondering why the substitution logic is not smart enough to realize there is no variable called $wahteverfoo. Further, since there is no such variable it would make sense to me that also no substitution would happen in such cases where the variable to substitute with is not even defined.

I hope it brings some clarity on the context and the issue at hand.

aidenfarley commented 5 months ago

I am understanding much better now.

The solution to me is to have it never substitute when it is $variablename, and only substitute when it is ${varname}

@allan2 as the maintainer, i'd like to hear your input on what you believe the best solution is.

I also do not know this repository enough internally to know whether or not this functionality is explicit or part of a library we use.

GilShoshan94 commented 5 months ago

Hi, knowing this is a fork from dotenv, I went to look at their repo and in the README.md, there is a clear documentation about Variable substitution

To me this is just a matter of documentation. I think it should be copied as well to show up here as well in docs.rs

allan2 commented 7 hours ago

Thanks for opening this. Substitution is now mentioned in the root README. Sorry that it was unclear before.