Skip to content

fix(vm): fall back to compiling from source when a module's code cache is rejected - #11031

Merged
sheremet-va merged 3 commits into
vitest-dev:mainfrom
MarshallOfSound:fix/vm-esm-cached-data-rejected
Aug 22, 2026
Merged

sheremet-va merged 3 commits into
vitest-dev:mainfrom
MarshallOfSound:fix/vm-esm-cached-data-rejected

Conversation

@MarshallOfSound

@MarshallOfSound MarshallOfSound commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor

Note

This PR was made with assistance from Claude in both identifying the issue and generating the patch

Description

The vm ESM executor's V8 code cache for externalized ES modules (#10744) broke every later file on a worker once any test changed V8 flags at runtime (e.g. v8.setFlagsFromString('--expose-gc')), because SourceTextModule throws ERR_VM_MODULE_CACHED_DATA_REJECTED on a flag-hash mismatch rather than just setting cachedDataRejected like vm.Script... Now we catch it and compile from source

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.
  • Please check Allow edits by maintainers to make review process faster. Note that this option is not available for repositories that are owned by Github organizations.

Tests

  • [ x Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

…e is rejected

The vm ESM executor reuses V8 code cache for externalized ES modules across
contexts (vitest-dev#10744). Unlike vm.Script, which only sets cachedDataRejected, a
SourceTextModule throws ERR_VM_MODULE_CACHED_DATA_REJECTED when V8 rejects
the cache - and V8 rejects it for the rest of a worker's life once any test
changes V8 flags at runtime (v8.setFlagsFromString('--expose-gc') is a common
way to get gc() in a test), because the flag hash the cache is checked against
changes. Every later file on that worker then failed to load its ESM
externals. Catch the rejection, drop the cache entry and compile from source.
@github-actions github-actions Bot added the maybe automated User is likely an AI agent, or the content was generated by an AI assistant without user control label Aug 22, 2026
@github-actions

Copy link
Copy Markdown

Hello @MarshallOfSound. Your PR has been labeled maybe automated because it appears to have been fully generated by AI with no human involvement.

To keep your PR open, please follow these steps:

  • Confirm that you are a real human. If you are an automated agent, disclose that
  • Make sure you've read, reviewed and stand behind its content
  • Make sure you've read the full issue along with all of its comments, as well as any linked issues and their comments
  • Make sure it follows our contribution guidelines and uses the correct GitHub template
  • Disclose any AI tools you used (e.g. Claude, Copilot, Codex)

Please, do not generate or format the response with AI. If you do not speak English, reply in your native language or use translation software like Google Translate or Deepl. If the response is generated, the PR will be closed automatically.

These measures help us reduce maintenance burden and keep the team's work efficient. See our AI contributions policy for more context.

@MarshallOfSound

Copy link
Copy Markdown
Contributor Author

Sorry about #11030 got hit with the PR limit and the github UI led me to "make it a draft anyway" which then reset my PR body πŸ™ƒ

Apparently the auotmated check still fires for definitely hand crafted PRs in the github UI though? Once again, reviewed this one carefully, purely a behavior fix

@sheremet-va sheremet-va removed the maybe automated User is likely an AI agent, or the content was generated by an AI assistant without user control label Aug 22, 2026

@sheremet-va sheremet-va left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if it’s better to return a patched v8 module where setFlagsFromString also clears esm’s cached data? We already return our own β€œnode:module” for example.

Then no error is ever thrown.

@MarshallOfSound

Copy link
Copy Markdown
Contributor Author

There are a bunch of ways to set v8 flags unfortunately, process.env.NODE_OPTIONS, setFlagsFromString, --v8-options etc. which could take effect in new workers but not older workers. etc.

We could probably make it better for some of them but node loves adding ways to set these flags so it'll be a game of whackamole.

Happy to play whackamole but the generic "if one of these slips through we shouldn't hard error" fix I think still applies? So maybe we land this and I send another PR after with a more optimized happy path for usage of setFlagsFromString?

@sheremet-va

Copy link
Copy Markdown
Member

Are you setting them outside the worker? I don’t think NODE_OPTIONS take affect after the worker spawns, and β€”v8-options you need to pass down to the worker from the outside (I assume via execArgv)

@MarshallOfSound
MarshallOfSound marked this pull request as ready for review August 22, 2026 07:27
@MarshallOfSound

Copy link
Copy Markdown
Contributor Author

Not outside, it's inside the worker, at runtime. The case we hit is a test calling v8.setFlagsFromString('--expose-gc') mid-run.

We can patch the node:v8 we hand to the vm context so setFlagsFromString clears the cache, happy to do that as the fast path. But it can't be the only line of defence: the flag flip can come from places we don't control:

  • a native addon
  • code running in the worker realm rather than the vm context (setup/environment code, anything externalized that grabs the real node:v8)
  • process.binding('v8')-style access

And once the process's flag hash changes, every later new SourceTextModule(..., { cachedData }) throws. So the try/catch β†’ recompile is the "whatever slipped through, don't hard-fail the file" backstop, and the patched v8 is an optimisation on top. Happy to add the latter in this PR or a follow-up, whichever you prefer.

@sheremet-va

sheremet-va commented Aug 22, 2026 •

Copy link
Copy Markdown
Member
  • code running in the worker realm rather than the vm context (setup/environment code, anything externalized that grabs the real node:v8)

I don't think this would matter? Externalized code runs via vm mechanism from this PR. Setup/environment runs before any of the cache is populated, and it doesn't have access to VM cache.

  • a native addon
  • process.binding('v8')-style access

This could break it, I agree.

I think the patch you propose should go anyway (what's in this PR). But patching v8.setFlagsFromString would cover most use cases and should be implemented alongside it, I believe. Only throwing an error for every external module by default seems like a big performance hit.

…de:v8

The vm context now gets a node:v8 whose setFlagsFromString empties the ESM
code cache right after the real call, so in the common case (a test or helper
calling v8.setFlagsFromString('--expose-gc')) no later module is even offered
stale cached data. The fallback in createSourceTextModule stays for flag
changes that do not go through that module (native addons, the worker realm's
own binding); the e2e test covers both paths.
@MarshallOfSound

MarshallOfSound commented Aug 22, 2026 •

Copy link
Copy Markdown
Contributor Author

@sheremet-va Done + tested πŸ‘

@sheremet-va
sheremet-va merged commit c3ba16b into vitest-dev:main Aug 22, 2026
28 of 29 checks passed
@sheremet-va

Copy link
Copy Markdown
Member

LGMT! Thanks for testing the rc!

@MarshallOfSound
MarshallOfSound deleted the fix/vm-esm-cached-data-rejected branch August 22, 2026 19:08
@github-actions github-actions Bot locked and limited conversation to collaborators Sep 6, 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