-
-
Notifications
You must be signed in to change notification settings - Fork 9
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
Environment variables break using vite's intlayerPlugin #91
Comments
Hey Seaders, Thanks again for your Issue! Thank you also for all the details provided and for taking the time to publish an example on StackBlitz. I fixed the I will take the time to investigate the environment issue. I will keep you informed about how it goes. |
I have patched the bug related to the environment variables (v4.1.6). Thank you for your investigation! Your reasoning was very accurate. Detail: Indeed, Intlayer injects environment variables into the Vite configuration. The injected variables are only related to Intlayer's configuration. They allow retrieving the configuration state throughout the entire project. Therefore, Intlayer must read the configuration file's state and load the environment variables to interpret it. Example of import { type IntlayerConfig } from 'intlayer';
const config: IntlayerConfig = {
editor: {
applicationURL: process.env.VITE_INTLAYER_APPLICATION_URL,
},
};
export default config; And indeed, I would have thought that Vite would overwrite these variables, but it does not. Loading variables for the Intlayer's config purpose injects these variables into Vite's environment. Additionally, the priority in loading the dotenv files was incorrect: const env = process.env.NODE_ENV
const loadEnvFile = () =>
dotenv.config({ path: ['.env', `.env.${env}`, `.env.${env}.local`] });
// Instead of:
const loadEnvFile = () =>
dotenv.config({ path: [`.env.${env}.local`, `.env.${env}`, '.env'] }); Please let me know if it works as expected 😊 |
I absolutely will do, am just annoyed I didn't spot that ordering issue! Any reason you're using That loading can (does) still mutate the env. The advice that was given to me was to also pass |
Just tried it there @aymericzip and I can confirm the initial issue is resolved, but "hot" reloading of envs isn't working. I'm 99% sure that it's due to the above. Config is mutating env and breaking vite's server's connection to it. Bonkers situation, but that's what it does. If you went this way, you could then also remove the dependency on dotenv from the backend and config packages. |
Indeed, it would be possible to use I just noticed, by the way, that Vite also relies on dotenv for Thank you for raising these questions, as I just realized that And thanks again for reporting the hot reloading issue. I'll take a closer look at it. |
Ahhhh sorry, wasn't thinking properly, that makes sense. Yep, but the issue is doing that multiple times. As per dotenv's docs,
The thing is though, am I right in saying that we shouldn't even be getting in to here?
|
Once again, you're absolutely right. You raise some very valid points. Logically speaking, Additionally, I haven't found a solution to avoid overwriting the environment variables. As such, it seems we should remove the Here’s the link to my latest commit, which includes some minor changes. However, it does not resolve the issue mentioned: |
Just to make sure you're aware, I've raised this issue with vite here - vitejs/vite#19306 as again, I consider this to be an issue with them. I'm lucky that you've opensourced this, and you've been so responsive, but if either of those things weren't true, I may not know the cause of this issue at all (if closed source), or not have any solution at all. But, for the changes, without passing in an obj to This prevents that (and would have prevented the OG, initial issue),
Because this mutation still happens in the plugin
I think (though not sure without testing) the env vars setup with vite will still fall over. |
Amazing! I’ve implemented the change, and it seems to be working well: |
That looks awesome! FYI I just checked and the process.env mutation doesn't break things. |
Are these changes in 4.1.7 @aymericzip ? I saw that that version bump, and push to npm went before 4c38ea2 . I've just installed 4.1.7 and the problem is still present (but that makes sense if your fix isn't in yet). |
Exactly, I just released it with v4.1.8 |
Sample app: https://github.com/SuperStreaming/intlayerapp
Stackblitz: https://stackblitz.com/~/github.com/SuperStreaming/intlayerapp (this unfortunately doesn't work due to a
import { findMatchingCondition } from "../../index.mjs";
ingetEnumerationContent.mjs:1
which you might want to take a look at too)Basically, in this project, there is a
.env.development
and a.env.development.local
env vars. Before usingintlayerPlugin
with vite, usingimport.meta.env.VITE_MY_ENV_VAR
in my code correctly shows the.local
content, "seaders2". After, it incorrectly showsseaders
.It also seems to some very strange things to my environment overall. I had a situation where production vars (from
.env.production
) were "leaking" into a build for dev, as well as if I changed variables in run-time they weren't reflected, sometimes even after restarting the server fully, sometimes I even had to reload VS Code?I know for a fact that
intlayer/packages/vite-intlayer/src/intlayerPlugin.ts
Line 38 in dc758c8
will cause some of these ill effects as, due to running into a similar issue previously, I know that mutating
process.env
directly is a no-no, and causes vite's internal processing of environment variables to fall over in weird ways. I know this because I hit this issue previously with vite -vitejs/vite#18993 (comment)
Unfortunately just removing those lines didn't resolve it. I ran a local copy of the plugin, without that env manipulation, and the error persisted.
I suspect the
loadEnvFile
probably needs changing too, but I can't see where this is actually used https://github.com/aymericzip/intlayer/blob/main/packages/%40intlayer/config/src/envVariables/loadEnvFile.ts I also see where anObject.assign(process.env
is happening here,intlayer/docs/en-GB/packages/@intlayer/config/index.md
Line 107 in dc758c8
The text was updated successfully, but these errors were encountered: