fix(core): enable node's native v8 compile cache support#35415
Conversation
✅ Deploy Preview for nx-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for nx-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
View your CI Pipeline Execution ↗ for commit 5c7614e
☁️ Nx Cloud last updated this comment at |
4da7d91 to
ed8affc
Compare
1b7d55d to
685e8d5
Compare
There was a problem hiding this comment.
✅ The fix from Nx Cloud was applied automatically
We added the missing NX_COMPILE_CACHE entry to the environment variables reference doc to satisfy the env-vars-documented conformance rule. The PR introduced this env var in packages/nx/src/utils/compile-cache.ts but did not document it, causing the conformance check to fail. This fix adds it to the Advanced section with a description consistent with the PR's intent.
Tip
✅ We verified this fix by re-running nx-cloud record -- pnpm nx-cloud conformance:check.
Suggested Fix changes
diff --git a/astro-docs/src/content/docs/reference/environment-variables.mdoc b/astro-docs/src/content/docs/reference/environment-variables.mdoc
index 81ea04f8..8c239f1b 100644
--- a/astro-docs/src/content/docs/reference/environment-variables.mdoc
+++ b/astro-docs/src/content/docs/reference/environment-variables.mdoc
@@ -48,6 +48,7 @@ The following environment variables are ones that you can set to change the beha
| ------------------------------------------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `NX_ADD_PLUGINS` | boolean | If set to `false`, Nx will not add plugins to infer tasks. This is `true` by default. |
| `NX_CACHE_PROJECT_GRAPH` | boolean | If set to `false`, disables the project graph cache. Most useful when developing a plugin that modifies the project graph. |
+| `NX_COMPILE_CACHE` | boolean | If set to `false`, disables Node's built-in V8 bytecode compile cache for Nx processes (CLI, daemon, plugin workers). The cache is enabled by default on Node 22.8+ and speeds up repeated invocations by reusing compiled bytecode. Has no effect on older Node versions. |
| `NX_DAEMON_SOCKET_DIR` | string | Alias of `NX_SOCKET_DIR`, used only when `NX_SOCKET_DIR` is not set. Despite the name, it controls all Nx socket placements, not just the daemon socket. Prefer `NX_SOCKET_DIR` in new setups. |
| `NX_FORCE_REUSE_CACHED_GRAPH` | boolean | If set to `true`, Nx will reuse an existing cached project graph when available and skip recomputing it. Useful in short-lived CI steps that run immediately after a step which already computed the graph. |
| `NX_FORMAT_SORT_TSCONFIG_PATHS` | boolean | If set to `true`, generators will sort the TypeScript path mappings in the root tsconfig file. |
🎓 Learn more about Self-Healing CI on nx.dev
Co-authored-by: AgentEnder <AgentEnder@users.noreply.github.com>
## Current Behavior
Every `nx` invocation re-parses and re-compiles the same JS modules from
disk. Node 22.8+ ships an opt-in V8 cache for compiled bytecode
(`require('module').enableCompileCache()`), but bin/nx.ts doesn't enable
it.
> ⚠️ This is **not the same** as the `v8-compile-cache` npm package that
was previously used in nx and removed in #20454 due to ESM
incompatibility (`Invalid host options` error). The npm package was a
userspace `Module.prototype._compile` monkey-patch and famously broke
when ESM modules were loaded. The Node 22.8 built-in is implemented
inside Node's loader and was designed specifically to support both CJS
and ESM cleanly. It does not have the bug that motivated #20454.
## Expected Behavior
Call `enableCompileCache()` at the top of bin/nx.ts so the cached
bytecode is reused on subsequent runs. The optional chaining (`?.`) plus
try/catch make it a no-op on older Node versions, and the cache itself
is a no-op on the first run — every run after that pays only the
cached-bytecode load instead of full parse+compile.
Cache files live in Node's default location
([`os.tmpdir()/node-compile-cache`](https://nodejs.org/api/module.html#moduleenablecompilecachecachedir))
and Node manages them automatically. Cache entries are keyed on source
mtime+size and Node version, so they invalidate automatically when
source changes or the user upgrades Node.
This also enables the cache in the daemon and plugin workers, which has
shown to be promising for speeding up plugin load times.
## Related Issue(s)
Fixes #
---------
Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>
Co-authored-by: AgentEnder <AgentEnder@users.noreply.github.com>
|
This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request. |
Current Behavior
Every
nxinvocation re-parses and re-compiles the same JS modules from disk. Node 22.8+ ships an opt-in V8 cache for compiled bytecode (require('module').enableCompileCache()), but bin/nx.ts doesn't enable it.Expected Behavior
Call
enableCompileCache()at the top of bin/nx.ts so the cached bytecode is reused on subsequent runs. The optional chaining (?.) plus try/catch make it a no-op on older Node versions, and the cache itself is a no-op on the first run — every run after that pays only the cached-bytecode load instead of full parse+compile.Cache files live in Node's default location (
os.tmpdir()/node-compile-cache) and Node manages them automatically. Cache entries are keyed on source mtime+size and Node version, so they invalidate automatically when source changes or the user upgrades Node.This also enables the cache in the daemon and plugin workers, which has shown to be promising for speeding up plugin load times.
Related Issue(s)
Fixes #