Skip to content

fix(core): enable node's native v8 compile cache support#35415

Merged
FrozenPandaz merged 2 commits into
masterfrom
scintillating-mandolin
May 7, 2026
Merged

fix(core): enable node's native v8 compile cache support#35415
FrozenPandaz merged 2 commits into
masterfrom
scintillating-mandolin

Conversation

@AgentEnder
Copy link
Copy Markdown
Member

@AgentEnder AgentEnder commented Apr 23, 2026

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) 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 #

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 23, 2026

Deploy Preview for nx-dev ready!

Name Link
🔨 Latest commit 5c7614e
🔍 Latest deploy log https://app.netlify.com/projects/nx-dev/deploys/69fcbf1d9c33b60008c4f352
😎 Deploy Preview https://deploy-preview-35415--nx-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 23, 2026

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit 5c7614e
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/69fcbf1dd62cfb0008f0f302
😎 Deploy Preview https://deploy-preview-35415--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link
Copy Markdown
Contributor

nx-cloud Bot commented Apr 23, 2026

View your CI Pipeline Execution ↗ for commit 5c7614e

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 36m 22s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 3s View ↗
nx-cloud record -- pnpm nx-cloud conformance:check ✅ Succeeded 16s View ↗
nx build workspace-plugin ✅ Succeeded <1s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 24s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 6s View ↗

☁️ Nx Cloud last updated this comment at 2026-05-07 17:14:32 UTC

@AgentEnder AgentEnder changed the title feat(core): worker_threads transport + V8 compile cache for plugins wip Apr 23, 2026
nx-cloud[bot]

This comment was marked as outdated.

nx-cloud[bot]

This comment was marked as outdated.

nx-cloud[bot]

This comment was marked as outdated.

@AgentEnder AgentEnder force-pushed the scintillating-mandolin branch from 4da7d91 to ed8affc Compare May 7, 2026 14:25
nx-cloud[bot]

This comment was marked as outdated.

@AgentEnder AgentEnder changed the title wip fix(core): enable node's native v8 compile cache support May 7, 2026
nx-cloud[bot]

This comment was marked as outdated.

@AgentEnder AgentEnder force-pushed the scintillating-mandolin branch from 1b7d55d to 685e8d5 Compare May 7, 2026 16:24
Copy link
Copy Markdown
Contributor

@nx-cloud nx-cloud Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.                                                                                                                                                                                                       |

Revert fix via Nx Cloud  

View interactive diff ↗

🎓 Learn more about Self-Healing CI on nx.dev

Co-authored-by: AgentEnder <AgentEnder@users.noreply.github.com>
@AgentEnder AgentEnder marked this pull request as ready for review May 7, 2026 19:01
@AgentEnder AgentEnder requested a review from a team as a code owner May 7, 2026 19:01
@AgentEnder AgentEnder requested a review from FrozenPandaz May 7, 2026 19:01
@FrozenPandaz FrozenPandaz merged commit 996228b into master May 7, 2026
25 checks passed
@FrozenPandaz FrozenPandaz deleted the scintillating-mandolin branch May 7, 2026 20:41
FrozenPandaz pushed a commit that referenced this pull request May 12, 2026
## 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>
@github-actions
Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants