chihab / dotenv-run

Seamlessly load environment variables. Supports cli, esbuild, rollup, vite, webpack, angular. ESM and Monorepos.
238 stars 17 forks source link

Multiple env files log correctly but have a different effect #100

Open atollk opened 3 months ago

atollk commented 3 months ago

Hi. First of all, thanks for creating the project and keeping it active. It's a good addition that Angular is lacking.

I am trying to inject different configurations into my Angular application with @ngx-env/builder. However, it seems that the env files are not loaded correctly. I am running myapp:serve:development. This is the output when building:

---------------------------------
- Root directory:  /home/x/Projects/git/myapp/web
- Working directory:  /home/x/Projects/git/myapp/web/apps/myapp
- Files: .env
- Environment: none
- Environment files: 
 ✔ /home/x/Projects/git/myapp/web/.env
- Environment variables: NG_APP 
 ✔ NG_APP_BACKEND_API_URL
---------------------------------

Seems valid to me. However, NG_APP_BACKEND_API_URL does not actually have the value that is defined in .env, as it is suggested by that console log, but rather the value that is defined in a separate .env.development. If I delete the latter, then the correct value is used.

On the other hand, if I run NODE_ENV=devxx myapp:serve:development, this is the output:

---------------------------------
- Root directory:  /home/x/Projects/git/myapp/web
- Working directory:  /home/x/Projects/git/myapp/web/apps/myapp
- Files: .env
- Environment: devxx
- Environment files: 
 ✔ /home/x/Projects/git/myapp/web/.env.devxx
 ✔ /home/x/Projects/git/myapp/web/.env
- Environment variables: NG_APP 
 ✔ NG_APP_BACKEND_API_URL
---------------------------------

Again, completely reasonable and what I would expect. But again, that's actually not showing the truth: env.devxx is completely ignored and the values from .env are used.

So, apparently NODE_ENV is used only for logging but for the actual values, the angular build configuration is used?

chihab commented 3 months ago

What version of the package are you using please?

atollk commented 3 months ago

I'm using 17.3.0.

chihab commented 3 months ago

The environment follows this rule:

    process.env.NG_APP_ENV || // @deprecated
    process.env.NODE_ENV || // default in @dotenv-run/core
    builderConfiguration // the angular build configuration

Example: if you set NODE_ENV the builder configuration is ignored if NG_APP_ENV and NODE_ENV are not set, then the builder configuration is picked.

Would it be possible to provide a repo reproducing the issue (you can fork it on stackblitz from here: https://stackblitz.com/edit/ngx-env-3ey8js)

Which builder are you using: application, browser-esbuild or browser?

atollk commented 2 months ago

Sorry, I didn't have the time to build a new example from the Stackblitz. I tried to reduce my own project to a small reproducible example.

web.zip

I am running export NODE_ENV=production && npx nx run chess2-web:serve:development. The builder output is what would be intended:

- Files: .env
- Environment: production
- Environment files: 
 ✔ /home/x/Projects/git/chess-2.0/web/.env.production
 ✔ /home/x/Projects/git/chess-2.0/web/.env
- Environment variables: NG_APP 
 ✔ NG_APP_FOO
---------------------------------

But the page is rendered as:

NG_APP_FOO: DEV
NG_APP_ENV:
NODE_ENV: development

So to me it seems like there are two issues here:

  1. At some point, the NODE_ENV variable is just overridden. Since you also said that NG_APP_ENV is deprecated, I wouldn't know how to then manually set my environment.
  2. Even though NODE_ENV is overridden, the builder output seems to print its information before that and "pretends" to use the production environment.