vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
65.97k stars 5.88k forks source link

Dev server doesn't update overridden env var when doing an automatic reload. #17519

Open alexkreidler opened 2 weeks ago

alexkreidler commented 2 weeks ago

Describe the bug

I have a production API url and a local API URL I am trying to put into .env.local or .env.development. But after upgrading from vite 4.5.3 to 5.3.1, I noticed the dev server is not applying env vars correctly when doing an automatic reload.

Note that the frontend shows the correct value when killing the dev server and manually re-running from terminal

Reproduction

https://stackblitz.com/edit/vitejs-vite-y1ptn5?file=main.js,.env,.env.development&terminal=dev

Steps to reproduce

  1. Put VITE_PUBLIC_API_URL=https://api.mysite.com in .env.
  2. Add VITE_PUBLIC_API_URL=http://localhost:3100 in .env.local. After refreshing the frontend console.log(import.meta.env.VITE_PUBLIC_API_URL); shows the https://api.mysite.com value.
  3. Do the same thing in .env.development, deleting .env.local, frontend still shows the old value

In StackBlitz you may have to delete .env.development, manually restart the server and then re-add it to reproduce. Screenshot 2024-06-19 at 04-00-12 Vitejs - Vite (forked) - StackBlitz

System Info

System:
    OS: Linux 5.15 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
    CPU: (16) x64 AMD Ryzen 7 3800X 8-Core Processor
    Memory: 11.82 GB / 15.56 GB
    Container: Yes
    Shell: 3.7.0 - /usr/bin/fish
  Binaries:
    Node: 18.14.0 - ~/.volta/tools/image/node/18.14.0/bin/node
    Yarn: 3.6.1 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 9.3.1 - ~/.volta/tools/image/node/18.14.0/bin/npm
    pnpm: 8.6.5 - ~/.local/share/pnpm/pnpm
    bun: 0.5.8 - ~/.volta/bin/bun
    Watchman: 2023.12.04.00 - /home/linuxbrew/.linuxbrew/bin/watchman
  Browsers:
    Chrome: 123.0.6312.105
    Chromium: 126.0.6478.61

Used Package Manager

pnpm

Logs

3:41:26 AM [vite] .env.local changed, restarting server...
3:41:26 AM [vite] server restarted.
3:42:06 AM [vite] .env.development changed, restarting server...
3:42:06 AM [vite] server restarted.
3:42:16 AM [vite] .env changed, restarting server...

Validations

stackblitz[bot] commented 2 weeks ago

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