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

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

Closed
sassman opened this issue Apr 18, 2024 · 5 comments
Closed

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

sassman opened this issue Apr 18, 2024 · 5 comments

Comments

@sassman
Copy link

sassman commented Apr 18, 2024

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

  • checking the env var KEY2 should show the value $NOKEY, so without the regular env variable substitution.
    • what we currently get is the value is an empty string
  • same goes for KEY3 where the value should be $NOKEY+valueU

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
Copy link

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
Copy link
Author

sassman commented Apr 19, 2024

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
Copy link

aidenfarley commented Apr 19, 2024

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
Copy link

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
Copy link
Owner

allan2 commented Oct 5, 2024

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

@allan2 allan2 closed this as completed Oct 5, 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

4 participants