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

manual loading and parsing of dotenv in watch mode #558 #559

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

tandibar
Copy link

@tandibar tandibar commented Oct 14, 2022

I think this could be the fix for the issue #558 that I opened.

The problem with dotenv.config().parsed is that dotenv.config() modifies the process.env. So the fix uses dotenv.parse() directly so that `process.env is untouched.

All tests are green. I will provide an additional test in the next days.

Checklist

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for opening a PR! Can you please add a unit test?

@tandibar
Copy link
Author

tandibar commented Oct 15, 2022

So I added a unit test and implemented the feature. The problem is, that it breaks another feature, see test test/start.test.js should reload the env on restart when watching. In the current implementation I think it is very hard to have both features. For me it would be more consistent if dotenv does not change its behavior in watch mode. But that would mean, that you have to restart your server if you change your .env even in watch mode.
What do you think?

(I intentionally commited with --no-verify so that you can see and test the conflicting tests for yourself.)

@tandibar tandibar requested a review from mcollina October 15, 2022 09:33
@mcollina
Copy link
Member

that would mean, that you have to restart your server if you change your .env even in watch mode.

Why? I think it might be possible to have the env variables defined in process.env to have priority over the ones in .env.
Or viceversa. I think the surprising behavior is lack of consistency.

@tandibar
Copy link
Author

So the problem ist, that dotenv is already loaded before, so we cannot just check which env vars are already present, because all are present. So I decided to check if there is a difference between the process.env and the parsed dotenv. If yes, we should not overwrite the env var. If it is the same, it seems to be managed by dotenv file, so we should overwrite.
Tests are now all passing, so I think that is the solution.

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

Successfully merging this pull request may close these issues.

2 participants