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

[BUG] env utility breaks Vite dotenv conventions #5803

Open
seniorquico opened this issue Mar 31, 2024 · 3 comments
Open

[BUG] env utility breaks Vite dotenv conventions #5803

seniorquico opened this issue Mar 31, 2024 · 3 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@seniorquico
Copy link

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.

(reference)

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).

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

  1. Create a Refine/Vite project.
  2. Create a .env file with the content VITE_TITLE=production.
  3. Create a .env.development file with the content VITE_TITLE=development.
  4. Run npm run refine dev.
  5. 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.

@seniorquico seniorquico added the bug Something isn't working label Mar 31, 2024
@BatuhanW
Copy link
Member

BatuhanW commented Apr 3, 2024

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.

@BatuhanW BatuhanW added the good first issue Good for newcomers label Apr 3, 2024
@BatuhanW
Copy link
Member

BatuhanW commented Apr 3, 2024

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.

@BatuhanW
Copy link
Member

BatuhanW commented May 2, 2024

We are open to contributions for this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants