Skip to content

perf: send source maps to workers as a string - #11750

Merged
sheremet-va merged 9 commits into
vitest-dev:mainfrom
maschwenk:feat/source-map-as-string
Nov 9, 2026
Merged

sheremet-va merged 9 commits into
vitest-dev:mainfrom
maschwenk:feat/source-map-as-string

Conversation

@maschwenk

@maschwenk maschwenk commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #11717

The fetch result now has the source map as a JSON string in sourceMap instead of a base64 comment at the end of code, so v8 only compiles the code. The map is stringified once per transform and parsed only when a stack trace needs it.

  • The fs module cache and forks pool tmp files store the map after a \n//# vitestSourceMap= separator, and the cache version is bumped. On a cache hit the server parses it into transformResult.map.
  • getModuleSourceMapById in vitest's EvaluatedModules reads meta.sourceMap and falls back to the comment, so stack traces and inline snapshots map as before, in vm pools and watch mode too.
  • The custom environment loader uses vite's own evaluator, so it puts the comment back for its few modules.

It's the default, not an experimental.stripSourceMapComments option, because a debugger is the only thing I found that reads the comment from the compiled script. For vm scripts node's --enable-source-maps and NODE_V8_COVERAGE ignore it (node 22.22, 24.22 and 26.5: no findSourceMap result and no source-map-cache entry in the coverage file, with or without the comment). coverage-v8 reads transformResult on the server and istanbul instruments during transform.

So the worker adds the comment back when an inspector is open in the worker or in the main process. A debugger on the main process can attach to worker threads, where inspector.url() is undefined, so that state goes to workers as config.inspector.openInMainProcess.

Open questions:

  • Is sourceMap ok as the name? I avoided map in case vite takes the map object from Module runner: send source maps separately from module code vitejs/vite#23890.
  • Is passing the main process inspector state in the serialized config ok, or should only --inspect* get the comment?
  • The fs cache comment (//# vitestCache=) still gets compiled, about 1M chars in test/unit. Drop it in a follow up?

An AI coding agent (Claude) did the implementation, tests and measurements under my direction. I'm responsible for the change.

@netlify

netlify Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 34d8215
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/6aee02761b510d915ac264da
😎 Deploy Preview https://deploy-preview-11750--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

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

@maschwenk
maschwenk force-pushed the feat/source-map-as-string branch from dd64489 to 55b74a4 Compare October 26, 2026 17:07
Workers get the module code without the inline source map comment and the map as a JSON string next to it. Stack traces read the map from the string. The comment is added back only when an inspector is open in the worker or the main process, since debuggers read it from the compiled script.
koriyoshi2041

This comment was marked as spam.

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

wrong status last time*

@sheremet-va

Copy link
Copy Markdown
Member

Is sourceMap ok as the name? I avoided map in case vite takes the map object from Module runner: send source maps separately from module code vitejs/vite#23890.

Not sure. Would be nice to have "stringified" in there or something to signal it's not an object. If vite adds map, sourceMap and map would be very confusing

Is passing the main process inspector state in the serialized config ok, or should only --inspect* get the comment?

Not ok, it should be calculated in resolveConfig. It's not rational to start vitest and enable the inspector afterwards, you would usually have inspect ready by the time we resolve the config

The fs cache comment (//# vitestCache=) still gets compiled, about 1M chars in test/unit. Drop it in a follow up?

Makes sense to drop it in a follow up

@maschwenk

Copy link
Copy Markdown
Contributor Author

thanks for the quick turnaround! pushed fixes for all three

one more thing i noticed while doing this: @vitest/web-worker builds its own VitestModuleEvaluator and never passes inlineSourceMap, so modules it runs wont get the source map comment even when a debugger is attached. its a one liner (inlineSourceMap: state.config.inspector.openInMainProcess in packages/web-worker/src/runner.ts), happy to add it here or do it in a follow up, whichever you prefer

@sheremet-va

Copy link
Copy Markdown
Member

thanks for the quick turnaround! pushed fixes for all three

one more thing i noticed while doing this: @vitest/web-worker builds its own VitestModuleEvaluator and never passes inlineSourceMap, so modules it runs wont get the source map comment even when a debugger is attached. its a one liner (inlineSourceMap: state.config.inspector.openInMainProcess in packages/web-worker/src/runner.ts), happy to add it here or do it in a follow up, whichever you prefer

Better to do in a follow up, I think


const sourceMap = (module.meta as VitestFetchResult | undefined)?.sourceMap
const compiledCode =
sourceMap && this.options.inlineSourceMap ? inlineSourceMap(code, sourceMap) : code

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.

inlineSourceMap spread around the codebase - in environment loader it's used directly, here it's inside the evaluator. Can we do it one way only to avoid confusion? I suggest keeping it in the transport implementation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

moved it into the transport in 60e13aa. VitestTransport takes an inlineSourceMap option now and adds the comment to the fetched code, the env loader sets it to true and the worker sets it when an inspector is open. the server runner has its own transport so it calls the same helper. the evaluator doesnt know about source maps anymore

this also covers @vitest/web-worker since it goes through the same transport, so the follow up is just a test now

@maschwenk

Copy link
Copy Markdown
Contributor Author

fyi the e2e failures are signal.test.ts > cancelling test run aborts the signal after a concurrent test finished, which is flaky on main too, e.g. https://github.com/vitest-dev/vitest/actions/runs/40356375484 and https://github.com/vitest-dev/vitest/actions/runs/40053221439

sheremet-va
sheremet-va previously approved these changes Oct 30, 2026
@sheremet-va

Copy link
Copy Markdown
Member

Do you have any benchmarks to compare it? You can use https://github.com/vitest-dev/benchmarks

# Conflicts:
#	packages/vitest/src/node/cache/fsModuleCache.ts
@maschwenk

Copy link
Copy Markdown
Contributor Author

Numbers from vitest-dev/benchmarks@c2a8bc1 (default matrix, 140 cells including browser). The PR at c2d9e54, before the merge below, vs its base 54a2bdb, alternating on the same dedicated AWS boxes (m8g.4xlarge, Graviton4, 16 vCPU), 10 pairs per cell. "Faster / slower" counts cells whose 95% interval excludes zero.

app CPU, median cell cells faster / slower instructions
barrel-hell −2.2% 16 / 0 −7.7%
enterprise-monolith −2.2% 10 / 0 −5.4%
node-library −0.7% 5 / 0 −1.8%
react-spa +0.5% 0 / 5 +0.2%
vue-spa +0.4% 0 / 4 +0.3%
micro-utils +0.5% 0 / 2 +0.4%
deps-heavy +0.1% 0 / 3 +0.1%
long-haul, design-system, cpu-bound, node-backend within ±0.3% 3 / 1 within ±0.2%

Big module graphs use about 2% less CPU. The 15 slower cells are 0.3-1.2% slower, mostly react-spa and vue-spa under jsdom, plus deps-heavy. I haven't found the cause yet.

On a larger private backend test suite (182 test files, 8 workers), keeping the map out of the compiled code cut CPU by 12.5% and wall time by 12.7% with warm caches, and by 4.0% / 2.3% cold. The inline maps were about two thirds of what V8 compiled there. That run used a patched vitest 5.0.5 that strips the comment right before compile, not this branch, so it measures the same effect another way.

I also merged main into the branch. The one conflict was in fsModuleCache: I kept the cache version bump and took main's fsCacheRoots type.

I wrote this with an AI agent and reviewed it.

Cache files no longer inline the source map, so the `//# sourceMappingSource` marker this test sliced at is gone.
@maschwenk

Copy link
Copy Markdown
Contributor Author

@sheremet-va 🙏🏼 we cool to merge?

@sheremet-va
sheremet-va merged commit 485cd62 into vitest-dev:main Nov 9, 2026
28 of 29 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 24, 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.

Compile modules without the inline source map comment (opt-in)

3 participants