-
Notifications
You must be signed in to change notification settings - Fork 51
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
Expand only after all env configs are loaded #101
Expand only after all env configs are loaded #101
Conversation
I think some users might have some env variables were they do want intermediate expansion (so they can e.g. set a PORT in one file and construct an URL from that port in another file). Maybe to avoid breaking changes, we should have a different flag? |
Would the use case be something like this? # .local.env
URL=http://localhost:${PORT}/api/v1
# .env
PORT=3344 Then you do
And I'm still getting expected results (I think at least):
|
Yes, but reversed, values from the first file, can be used in values from later files. I'm not sure if anybody relies on this, but this is something that would break so I don't think it's something we can change # .local.env
PORT=3344
# .env
URL=http://localhost:${PORT}/api/v1 and then running node cli.js -e .local.env -e .env -- printenv |
@entropitor I would have to agree. I would expect the first file to contribute to the second, and for the second to override the first. |
I did one test. The command I'm executing is:
File contents .env.local # This overrides TEST_PASS from .env
TEST_PASS=some\$pass
# This will use TEST_PORT_REVERSE from .env
TEST_URL_REVERSE=http://localhost:${TEST_PORT_REVERSE}/api/v1
# I want this to contribute to TEST_URL in .env
TEST_PORT=3000 .env TEST_PASS=
TEST_URL=http://localhost:${TEST_PORT}/api/v1
TEST_PORT_REVERSE=4000
# Default value for port if no other specifies it
TEST_PORT=5000 Results
Results before
To comment on @Swivelgames
This is satisfied.
This is not how it works and wasn't even before this change, at least how I understand it. |
Hi @entropitor , can you check my comment above. I think the solution is covering use cases you mentioned. |
Looks good! Nice solution! |
Consider this example with two env files,
.local.env
and.env
:where the files are like this:
What happens with the things as they're implemented now:
.local.env
loaded and expanded, we have process.env.TEST_PASS == 'some$pass' now.env
loaded and expanded, but now dotenv-expand loads from process.env, so TEST_PASS='some$pass' is passed toexpand()
. Consequently, after expansion TEST_PASS == 'some'. $pass variable doesn't exist, so it is replaced with an empty string.Proposed solution in this PR
Just do expand after all the config files are loaded.