-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
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
Environment variable value not reloading when ".env changed, restarting server..." if process.env. is mutated once #17689
Comments
Start a new pull request in StackBlitz Codeflow. |
Remix uses |
I have also realized today that Astro is doing the same thing. Nonetheless, I also think this isn't technically a bug in Vite so we can close this? |
Please do not close this one, I would argue this is a bug in Vite. What we are currently sure is that it is at least unintended behaviour of Vite dev server. (actually loadEnv func) I would expect whenever Vite dev server reloads, that it takes latest value in updated .env file, and that I can use process.env in Vite config. Not working like that without any warnings to developer is an issue. PR #17712 fixes the issue/bug completely. |
For dev ssr, repeating loading I'm not sure if this is a bug either as overwriting |
I've hit this same issue a few times now and definitely think this is an area that needs attention from vite / rollup. Consider this plugins situation - https://stackblitz.com/~/github.com/SuperStreaming/intlayerapp?file=vite.config.ts 4 plugins -
You will get different envs set, in different ways, based on the order you add the plugins in your config,
gives you different to...
I'm not sure if this is the correct (or safe?) way to even set env vars from a plugin because I can't even find docs on env vars for plugins in the vite/rollup docs. For me, I'd prefer a one, and one only safe way to set env vars The errors I have hit, I'm just lucky that I initially hit it myself, so had an understanding of what actually was going on, and that the plugin I used was open source - with a really communicative responsive dev. If I had just installed that plugin / another plugin that did similar, I'd have zero awareness of env var shenanigans going on. |
Describe the bug
I was thinking it is a critical issue but when recreating the minimal reproducible PoC for the issue I found the root cause.
I still think this should be resolved even if it is more related to the developer's mistake than a bug.
Long story short;
I was looking to use process.env. in the vite.config.js file and I found this example https://stackoverflow.com/a/70711383 but that response is not compatible with the current vite at all and will create all sorts of issues if used in dev mode.
The issue lies in the mutation of the process.env object because as soon as the process.env is set loadEnv will not override the values from process.env
The reason I'm opening this as a bug is as I see the way to resolve this issue such that it works in both cases, and this doesn't seem like intended behaviour, we could save the state of process.env at the first run of the server and consider this for the last loop in loadEnv for subsequent server reloads, making snippet valid. As this is only used to prioritise inline env vars according to the comment.
I've also left updated solution on StackOverflow
Reproduction
https://stackblitz.com/edit/vitejs-vite-wrbmyr?file=.env
Steps to reproduce
No response
System Info
System: OS: Linux 5.0 undefined CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: 1.0 - /bin/jsh Binaries: Node: 18.20.3 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 10.2.3 - /usr/local/bin/npm pnpm: 8.15.6 - /usr/local/bin/pnpm npmPackages: vite: ^5.3.2 => 5.3.3
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: