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

Nx serve not loading .env variable changes with monorepo format (Vite/Vue) #23090

Closed
2 of 4 tasks
NitroG42 opened this issue Apr 30, 2024 · 3 comments · Fixed by #23231
Closed
2 of 4 tasks

Nx serve not loading .env variable changes with monorepo format (Vite/Vue) #23090

NitroG42 opened this issue Apr 30, 2024 · 3 comments · Fixed by #23231
Assignees
Labels
scope: core core nx functionality type: bug

Comments

@NitroG42
Copy link

Current Behavior

Using a vite/vue scaffold, running the project through a monorepo architecture and using nx serve myapp with a .env.local file containing variables

when we change a variable from .env, the page will reload, but the variable will match the previous value. We have to restart the nx serve to make it use the new value.

Tested directly with vite serve, it does work : [vite] .env.local changed, restarting server...; but not through nx.
I wonder if it's caused by the daemon ? (#22786)

Expected Behavior

Modifying the .env.local restart the nx/vite server.

GitHub Repo

https://github.com/NitroG42/nxenv

Steps to Reproduce

  1. start the server npm run app
  2. Test should be displayed
  3. Change "Test" string in .env.local
  4. The page should change after reload, it doesn't.

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.11.1
OS     : darwin-arm64
npm    : 10.2.4

nx (global)        : 18.3.3
nx                 : 18.3.4
@nx/js             : 18.3.4
@nx/linter         : 18.3.4
@nx/eslint         : 18.3.4
@nx/workspace      : 18.3.4
@nx/devkit         : 18.3.4
@nx/eslint-plugin  : 18.3.4
@nrwl/tao          : 18.3.4
@nx/vite           : 18.3.4
@nx/vue            : 18.3.4
@nx/web            : 18.3.4
typescript         : 5.4.5
---------------------------------------
Registered Plugins:
@nx/eslint/plugin
@nx/vite/plugin

Failure Logs

No response

Package Manager Version

No response

Operating System

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

Additional Information

No response

@creotip
Copy link

creotip commented Apr 30, 2024

It's absolutely correct behaviour and not related to NX.
Vite(or any other build tool) hot reload doesn't watch on .env file changes.

@NitroG42
Copy link
Author

NitroG42 commented Apr 30, 2024

It's absolutely correct behaviour and not related to NX. Vite(or any other build tool) hot reload doesn't watch on .env file changes.

I can make another sample where you'll see that Vite definitelly does that ! (though with vue 2 and vue-cli it didn't).
With a non-monorepo scaffold + NX, it also works

also, as i put in the first post, modifying the .env cause the following logs : (on non-monorepo setup)

[vite] .env.local changed, restarting server...

@AgentEnder AgentEnder added the scope: bundlers Issues related to webpack, rollup label May 3, 2024
@jaysoo
Copy link
Member

jaysoo commented May 7, 2024

The issue here is that Nx is loading the .env.local from the project folder, which is intentional. When Vite reloads itself, it is still inheriting the environment variable set by Nx since Vite process is spawned from Nx.

The workaround for now is to use npx vite dev from the app folder, which circumvents the Nx process.

To skip or unload from .env.local file requires changes to the Nx core. Stay tuned!

@jaysoo jaysoo added scope: core core nx functionality and removed scope: bundlers Issues related to webpack, rollup labels May 7, 2024
@FrozenPandaz FrozenPandaz assigned xiongemi and unassigned jaysoo and FrozenPandaz May 7, 2024
FrozenPandaz pushed a commit that referenced this issue May 14, 2024
…3231)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #23090
FrozenPandaz pushed a commit that referenced this issue May 14, 2024
…3231)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #23090

(cherry picked from commit 9122b85)
Coly010 pushed a commit that referenced this issue May 15, 2024
…3231)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #23090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope: core core nx functionality type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants