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

Environment variable value not reloading when ".env changed, restarting server..." if process.env. is mutated once #17689

Open
7 tasks done
gnekich opened this issue Jul 15, 2024 · 5 comments · May be fixed by #17712
Open
7 tasks done

Comments

@gnekich
Copy link

gnekich commented Jul 15, 2024

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.

// !!! DO NOT USE !!!
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default ({ mode }) => {
  process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };

  console.log(process.env);

  return defineConfig({
    plugins: [react()],
  });
};

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

// This is fine but not ideal
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default ({ mode }) => {
  const env = { ...process.env, ...loadEnv(mode, process.cwd()) };

  console.log(env);

  return defineConfig({
    plugins: [react()],
  });
};

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

Copy link

stackblitz bot commented Jul 15, 2024

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Sep 9, 2024

Remix uses Object.assign(process.env, loadEnv(mode, root, "")) to expose env files on server side process.env during dev, so it suffers from the same issue remix-run/remix#9587.

@bluwy
Copy link
Member

bluwy commented Oct 3, 2024

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?

@gnekich
Copy link
Author

gnekich commented Oct 4, 2024

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.

@hi-ogawa
Copy link
Collaborator

hi-ogawa commented Oct 5, 2024

For dev ssr, repeating loading .env and exposing it on process.env is probably a valid and important use case, but I think I found a workaround hi-ogawa/vite-plugins#657.

I'm not sure if this is a bug either as overwriting process.env shouldn't be necessary for SPA use cases at least. For ssr framework, they can have own convention to not rely on process.env, so the issue seems often avoided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants