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

env: environment is not loaded in the same order as it described in the docs #18712

Closed
1 of 4 tasks
nullifiedtsk opened this issue Aug 18, 2023 · 1 comment · Fixed by #18746
Closed
1 of 4 tasks

env: environment is not loaded in the same order as it described in the docs #18712

nullifiedtsk opened this issue Aug 18, 2023 · 1 comment · Fixed by #18746
Labels

Comments

@nullifiedtsk
Copy link

nullifiedtsk commented Aug 18, 2023

Current Behavior

After upgrading from 15.7.2 (or 15.5.2) to 16.7.1 using nx migrate latest noticed that application started to fail during environment variables checks.

In my project I have a couple of env files:

apps/app/.env.production # not used at all
apps/app/.env.local # local overrides for app
.env.local # local overrides for all apps
.env # a file that contains list of all the variables required to start all the apps at once

--

So AAA_VARIABLE_NAME is resolved as JAMAL when vars are set in following order (which is expected):

  • AAA_VARIABLE_NAME=JAMAL in apps/app/.env.local
  • AAA_VARIABLE_NAME=ALICE in .env.local
  • AAA_VARIABLE_NAME=BOB in .env

--

AAA_VARIABLE_NAME is resolved as "" (empty string) when vars are set in following order (which is expected):

  • AAA_VARIABLE_NAME= in apps/app/.env.local
  • AAA_VARIABLE_NAME=ALICE in .env.local
  • AAA_VARIABLE_NAME=BOB in .env

--

AAA_VARIABLE_NAME is resolved as "ALICE" when vars are set in following order (which is expected):

  • # AAA_VARIABLE_NAME=JAMAL in apps/app/.env.local (disabled)
  • AAA_VARIABLE_NAME=ALICE in .env.local
  • # AAA_VARIABLE_NAME=BOB in .env (disabled)

--

But in next case value resolves to "BOB" which is NOT (!) expected here:

  • NO RECORD in apps/app/.env.local
  • AAA_VARIABLE_NAME=ALICE in .env.local
  • AAA_VARIABLE_NAME=BOB in .env

According to https://nx.dev/recipes/tips-n-tricks/define-environment-variables value from .env should be ignored if value is already picked up from .env.local or apps/app/.env.local.

Expected Behavior

AAA_VARIABLE_NAME should be resolved to ALICE when vars are set in following order:

  • NO RECORD in apps/app/.env.local
  • AAA_VARIABLE_NAME=ALICE in .env.local
  • AAA_VARIABLE_NAME=BOB in .env

GitHub Repo

https://github.com/nullifiedtsk/nx-env-order-issue-demo-repo

Steps to Reproduce

Repo contains three branches (no master):

https://github.com/nullifiedtsk/nx-env-order-issue-demo-repo/branches/all

  • nestjs-plus-nx-15.5.2 -> NestJS + NX 15.5.2
  • nestjs-plus-nx-16.7.1 -> NestJS + NX 16.7.1
  • nx-raw-16.7.1 -> NX with typescript 16.7.1 (to ensure bug is not caused by NestJS)

To reproduce the issue without this repo you will need to:

  1. Create new NX app
  2. Create .env file in root directory and put AAA_VARIABLE_NAME=BOB inside.
  3. Create .env.local file in root directory and put AAA_VARIABLE_NAME=ALICE inside.
  4. Optionally create apps/<appname>/.env.local and put commented #AAA_VARIABLE_NAME=JAMAL inside.
  5. Read var from process.env as process.env['AAA_VARIABLE_NAME']

Result:
16.7.1 and 15.5.2 produces different results:

  • 16.7.1 -> BOB (should be ALICE)
  • 15.5.2 -> ALICE

Nx Report

Node   : 18.16.1
OS     : win32-x64
npm    : 9.5.1

nx                 : 16.7.1
@nx/js             : 16.7.1
@nx/jest           : 16.7.1
@nx/linter         : 16.7.1
@nx/workspace      : 16.7.1
@nx/devkit         : 16.7.1
@nx/eslint-plugin  : 16.7.1
@nx/nest           : 16.7.1
@nx/node           : 16.7.1
@nrwl/tao          : 16.7.1
@nx/webpack        : 16.7.1
typescript         : 4.8.4

Failure Logs

No failure logs available.

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants