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

Add envDir: false option #19373

Open
4 tasks done
Raanelom opened this issue Feb 6, 2025 · 8 comments · May be fixed by #19503
Open
4 tasks done

Add envDir: false option #19373

Raanelom opened this issue Feb 6, 2025 · 8 comments · May be fixed by #19503
Labels
enhancement New feature or request

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!

@sapphi-red
Copy link
Member

Thanks. I see. A variable that you don't expect to be loaded (placed in .env file) is automatically loaded by Vite. (I had a assumption that you were overriding a variable.)

I think having envDir: false is a straightforward solution for this.

@Raanelom
Copy link
Author

Ok, it could still cause weird side-effects with the default-settings. Now that I'm aware of this behaviour I'm fine with the provided solution, but I think it'd be nice to have a more intuitive solution for users that are new to Dotenvx + Vite.

@Raanelom
Copy link
Author

envDir: false should be envDir: "false" -> Astro requires a String

@sapphi-red sapphi-red changed the title Support for dotenvx Add envDir: false option Feb 18, 2025
@sapphi-red sapphi-red added enhancement New feature or request and removed enhancement: pending triage labels Feb 18, 2025
@sapphi-red
Copy link
Member

It's because envDir: false is not supported now.

@OnlyWick
Copy link
Contributor

Is this about adding a false option to envDir to disable Vite from loading all environment variables?

@sapphi-red
Copy link
Member

Yes, this is about adding a false option to envDir to disable Vite from loading .env files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants