question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nx Cache Inputs should include NX_* env vars and some others

See original GitHub issue

I 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

  1. Environment variables prefixed with NX_ should automatically be included as Nx Cache inputs
  2. 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)
  3. Docs should be improved to let users know about this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mikecanncommented, Mar 7, 2021

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.

0reactions
thor84nocommented, Sep 27, 2021

btw this can be achieved already by using something like "runtimeCacheInputs": ["echo $NX_MY_VAR" ]. You can even call your own script with node. Can you try it and see if that solves your case?

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How Caching Works - Nx
Nx sets up hooks to collect stdout/stderr before running the command. All the output is cached and then replayed during a cache hit....
Read more >
Nx 14.4 — Inputs, optional npm scope, project graph cache ...
With this release, we introduce a new NX_PROJECT_GRAPH_CACHE_DIRECTORY environment variable to dictate where Nx (and the Nx Daemon) should store the project ...
Read more >
NX_* Environment Variables not working on prod builds in our ...
NX cache ignores environment variable value changes #4662 · Nx Cache Inputs should include NX_* env vars and some others #4455.
Read more >
Helping the Environment by Saving Two Centuries of ...
Nx comes with a so-called computation caching feature. For every cacheable operation, Nx takes a set of input parameters, computes a hash and ......
Read more >
Getting Started with Monorepo with Nx Nrwl - Rupesh Tiwari
Do you have a lot of JavaScript projects using GitHub repositories? How are you managing dependencies among those projects?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found