refine
refine copied to clipboard
[BUG] env utility breaks Vite dotenv conventions
Describe the bug
Vite uses dotenv (and a plugin or two) to load values from files with the following conventions:
.env # loaded in all cases
.env.local # loaded in all cases, ignored by git
.env.[mode] # only loaded in specified mode
.env.[mode].local # only loaded in specified mode, ignored by git
Additionally, it behaves according to the following description:
An env file for a specific mode (e.g. .env.production) will take higher priority than a generic one (e.g. .env).
In addition, environment variables that already exist when Vite is executed have the highest priority and will not be overwritten by .env files.
The Refine CLI automatically loads values in the .env
file which appear to persist in the environment when executing the Vite commands (i.e. vite dev
).
https://github.com/refinedev/refine/blob/16eefc493da3e66a095a11d9dcbeff0ec64dca57/packages/cli/src/utils/env/index.ts#L3
These take precedence and effectively break the .env.local
, .env.development
, etc. conventions of Vite.
CRA has slightly different but similar conventions. I assume the Refine CLI would be affecting it similarly?
Steps To Reproduce
- Create a Refine/Vite project.
- Create a
.env
file with the contentVITE_TITLE=production
. - Create a
.env.development
file with the contentVITE_TITLE=development
. - Run
npm run refine dev
. - Observe the value of
import.meta.env.VITE_TITLE
is "production", not "development".
Expected behavior
The Refine CLI shouldn't break Vite's dotenv conventions. In the trivial example provided, the value of import.meta.env.VITE_TITLE
should be "development" from the .env.development
file.
Packages
- "@refinedev/cli": "2.16.27"
- "@refinedev/core": "4.48.0"
- "@refinedev/kbar": "1.3.7"
- "@refinedev/mui": "5.14.5"
- "@refinedev/react-hook-form": "4.8.15"
- "@refinedev/react-router-v6": "4.5.6"
- "vite": "5.2.7"
- package manager: Yarn 4.1.1 w/ node_modules linker
Additional Context
I explicitly renamed my .env
file to .env.production
to "workaround" the issue. That file appears to not be loaded by the Refine CLI, and then my app builds as expected. It's an unfortunate workaround, effectively forcing us to not use the .env
file and "layering" of settings.
Hey @seniorquico thanks for the issue. It seems we need to conditionally follow Vite's env parsing logic for vite projects.
We are open to contributions for this one! Feel free to submit a PR, we'd be more than happy to get it released with the next release.
Hey @seniorquico we've discussed with the team and thinking that parsing dotenv files in CLI may not be needed at all. I think we could get rid of it and let the underlying framework (Vite, NextJs) take care of it.
We are open to contributions for this one.