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

Support for dotenvx #19373

Open
4 tasks done
Raanelom opened this issue Feb 6, 2025 · 2 comments
Open
4 tasks done

Support for dotenvx #19373

Raanelom opened this issue Feb 6, 2025 · 2 comments

Comments

@Raanelom
Copy link

Raanelom commented Feb 6, 2025

Description

Currently, Dotenvx is not properly supported by Vite.

We are using dotenvx (from the creator of dotenv) to manage our environment variables in Astro and Vite.
This means that a .env and a .env.production is available for every app in our monorepo with encrypted values, as follows:

SOME_ENV_VAR="encrypted:BA/01aCs2DHMHJc2SFeg...etcetc...f1TZg="

However, this currently results in Vite picking up the encrypted values without decrypting them. This results in unexpected situations, such as: running a production build with Astro as follows:

npx dotenvx run -f .env.production --strict -- astro build

I expect this command to only inject production .env values. However, Vite is automatically inserting the available .env file with encrypted values alongside the requested .env.production. This interferes with our Astro environment variable validation. We validate whether certain variables are present using Astro schema validation. Encrypted variables in the .env file are considered as "valid", even when encrypted (they are valid strings), therefore disrupting our env variable validation.

We currently use a workaround by setting the envDir to "bogus" in our Astro config. This is of course not a desirable solution.

Context @ Vite Discord: https://discord.com/channels/804011606160703521/1327200673518977046

Looking forward to work towards a solution together!

Suggested solution

There are some directions:

  • Adding an option to disable automatic .env-variable inclusion
  • Adding a detection-mechanism for encrypted .env values
  • ...

Alternative

No response

Additional context

No response

Validations

@sapphi-red
Copy link
Member

Assuming that dotenvx loads the variables to process.env, Vite shouldn't be overriding them by the values in .env.
https://vite.dev/guide/env-and-mode.html#:~:text=In%20addition%2C%20environment%20variables%20that%20already%20exist%20when%20Vite%20is%20executed%20have%20the%20highest%20priority%20and%20will%20not%20be%20overwritten%20by%20.env%20files.
If it is doing so, would you create a reproduction so that I can take a look?

@Raanelom
Copy link
Author

Raanelom commented Feb 7, 2025

Hi @sapphi-red , I created the following reproduction-repository: https://github.com/Raanelom/vite-dotenvx-astro-bug-reproduction -> as you can see the variable in .env is automatically loaded by Vite without decrypting it. Looking forward to your findings!

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

No branches or pull requests

2 participants