Nx Cache Inputs should include NX_* env vars and some others
See original GitHub issueI brought this up on slack and it was suggested that I make an issue for it so here goes.
Description
I just ran into a bit of a gotcha… It appears that Nx caching does NOT take into account environment variables when building web projects.
So for example say I have NX_MY_VAR=foo nx build myap
then it will build and cache, then if I do NX_MY_VAR=bar nx build myap
it wont re-build and instead will use the cache…
this is a bit of a gotcha IMO seeings as the docs say:
which works against the current state of the codebase and the environment, https://nx.dev/latest/react/core-concepts/computation-caching
I would expect standard environment variables such as those documented here would automatically be part of the computation cache hash: https://nx.dev/latest/react/guides/environment-variables (edited)
Motivation
Because caching is hard and it can lead to some nasty issues that are hard to track down.
Suggested Implementation
- Environment variables prefixed with
NX_
should automatically be included as Nx Cache inputs - Some other choice environment variables such as
NODE_ENV
and others that NX includes while building (https://nx.dev/latest/react/guides/environment-variables#how-to-use) - Docs should be improved to let users know about this.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (6 by maintainers)
Actually the more I think about this the more I think this is the wrong solution.
I have been reading about Immutable Web Apps (https://immutablewebapps.org/) and they advocate runtime variables being sepparate from the build process.
So IMO environment variables should not be supplied at build time and instead should be supplied AFTER build either injected into index.html or supplied as a sepparate file that can be loaded at runtime.
The problem is the way the Nx Cloud Caching works, it doesnt allow you to run anything AFTER a build has been run so the env vars cant be injected. I think thats the real problem / solution here.
This is all well and good for normal environment variables, but nx doesn’t run
runtimeCacheInputs
including all environment variables it uses for itself. E.g.nx-cloud.env
.