-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
docs: add note that bun's env loading behavior interferes with Vite's one #20947
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
Conversation
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
|
@copilot try again |
Could you please clarify what needs to be changed or re-attempted? The documentation has been added with the warning about Bun's .env loading behavior, and all tests are passing. If there's something specific you'd like me to adjust, please let me know! |
|
I linked oven-sh/bun#5515 but actually there's no workaround written there... |
Copilot wrote three ways as a workaround, but neither is good:
|
bluwy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Yeah I think we can link to the Bun issue for workarounds for now as there's many tradeoffs for each one.
Summary
Added a warning block in the
.envFiles section of the documentation to inform Bun users about potential conflicts between Bun's automatic.envfile loading and Vite'sloadEnv()function.The warning explains:
loadEnv()as it respects existingprocess.envvalues--env-fileflag, using--no-envflag, or using dotenv package directlyOriginal prompt
This section details on the original issue you should resolve
<issue_title>Bun's built-in .env loading overrides explicit environment loading from libraries like Vite's
loadEnv</issue_title><issue_description>### Describe the bug
When running a script with Bun, its automatic, built-in
.envfile loading takes precedence over explicit environment loading logic within the script itself. This was discovered while using Vite'sloadEnvutility function.I have a script that explicitly calls
loadEnv('production', ...), which is designed to load variables from a.env.productionfile.node, it works as expected and loads from.env.production.bun, it incorrectly loads variables from.env.developmentinstead.This suggests that Bun pre-loads the environment from
.env.development(its default behavior whenNODE_ENVis not set), and this pre-loaded state cannot be overridden by the explicit library call within the code. This behavior is counter-intuitive and can lead to silent but critical bugs in production builds, as developers expect their explicit function calls to be the source of truth.A workaround is to use the
bun --env-file=.env.production ...flag, which forces Bun to use the correct file. However, this reinforces the core issue: Bun's implicit default behavior overrides the explicit logic within a user's script.Reproduction
A repository is not required as the issue can be reproduced with just three local files. (Also it's not possible to reproduce in
vite.new)Steps to reproduce
Project Structure:
Create a directory with the following files:
.
├── .env.development
├── .env.production
└── debug-env.mjs
File Contents:
.env.development:.env.production:debug-env.mjs(requiresviteas a dev dependency):Steps to reproduce:
Create the files as described in the "Project Structure" section.
Install Vite as a development dependency:
bun add -d viteRun the script using Node. It will correctly load from
.env.production.Command:
node ./debug-env.mjsCorrect Output:
Run the same script using Bun. It will incorrectly load from
.env.development.Command:
bun ./debug-env.mjsIncorrect Output:
(Optional) Demonstrate the workaround by running Bun with the
--env-fileflag. This produces the correct output, proving the issue is with Bun's default env loading.Command:
bun --env-file=.env.production ./debug-env.mjsCorrect Output with Workaround:
System Info
System: OS: Linux 5.15 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish) CPU: (12) x64 Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz Memory: 44.23 GB / 62.67 GB Container: Yes Shell: 5.1.16 - /bin/bash Binaries: Node: 22.17.1 - /home/genrem/.nvm/versions/node/v22.17.1/bin/node npm: 11.5.2 - /home/genrem/.nvm/versions/node/v22.17.1/bin/npm bun: 1.3.0 - /home/genrem/.bun/bin/bun Browsers: Firefox: 143.0.4 Firefox Developer Edition: 143.0.4Used Package Manager
bun
Logs
No response
Validations
Fixes #20942
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.