Skip to content

fix(lib-coverage): merge ranges with unknown end columns - #25

Merged
AriPerkkio merged 2 commits into
vitest-dev:mainfrom
stevez:fix/merge-lenient-locations
Nov 19, 2026
Merged

AriPerkkio merged 2 commits into
vitest-dev:mainfrom
stevez:fix/merge-lenient-locations

Conversation

@stevez

@stevez stevez commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When the same file is covered by multiple transforms, FileCoverage#merge() duplicates statements, functions and branches. For example, Vitest projects with jsdom + browser. Items are matched by their exact start|end location, but source maps can leave the end column unresolved on one side:

  • In memory the unresolved end column is Infinity. It becomes null once serialized as JSON, e.g. in coverage-final.json or with --merge-reports.
  • The other transform resolves it, so 1:22->7:Infinity and 1:22->7:10 are treated as different items.

With jsdom + browser this happens because Vite's SSR transform rewrites imported bindings (useThing() → (0, __vite_ssr_import_0__.useThing)()), so the end of the call has no mapping back to the original source, while the browser transform keeps it as-is.

Fix

mergeProp keeps exact matching. When a location key has an unknown end column (Infinity or null) and no exact match, it's matched against the item on the other side with the same start position and end line.

The match is only applied when it's one-to-one. If multiple items share the start position and end line, or the candidate already has an exact match, nothing is collapsed and the existing nearest-container logic applies, so no hits are dropped. The merged item keeps the resolved end column.

This applies to statements, functions (loc) and branches (locations[0]), since they all go through mergeProp. Function body ranges that differ in start column (the SWC case from the Vitest review) are not handled. That only happens when merging output from different transpilers.

Tests

New unit tests are built from hand-made coverage objects using locations captured from real Vitest runs, so there's no third-party plugin dependency:

  • statements with null vs resolved end columns, on either side of the merge
  • statements with Infinity vs resolved end columns
  • functions and branches with null vs resolved end columns
  • ambiguous matches are not collapsed
  • a candidate that already has an exact match is not overwritten
  • ranges with different end lines are not merged

The 5 merge tests fail without the fix. The full suite passes after pnpm build.

Verified end-to-end

I installed the preview build of this PR in https://github.com/stevez/vitest-coverage-merge-bug: constants.ts goes from 6 statements to 3, with hits from both projects added together.

A test case reproducing this with jsdom + browser projects and no third-party plugins is ready for Vitest's test/coverage-test. It fails with the current release and passes with this build.


Note

This PR was written by Claude (AI agent, Claude Code), working with @stevez. @stevez reviewed the approach and the verification results before it was opened.

🤖 Generated with Claude Code

@travzhang

Copy link
Copy Markdown
Contributor

I don't think we should merge coverage data generated by different transforms in the first place.

SWC and Babel have different transformation pipelines and instrumentation implementations. Even when processing the same source code, they can produce different coverage metadata, including statement, function, and branch locations. These differences are not necessarily bugs; they are a consequence of the transforms themselves.

Trying to reconcile these differences through fallback matching introduces heuristics that may incorrectly merge distinct coverage entries or produce misleading coverage results. Matching locations more loosely does not guarantee that the underlying coverage data is semantically equivalent.

Instead, I think coverage merging should require a consistent transform and instrumentation pipeline. If coverage data comes from different transforms, it should be treated as incompatible rather than merged heuristically.

For this reason, I'd prefer not to introduce this fallback matching behavior.

@stevez

stevez commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I agree that fuzzy matching shouldn't merge items that aren't the same thing, and that's why the fallback is limited to unambiguous one-to-one pairs. But the main case here doesn't involve different transforms. In https://github.com/stevez/vitest-coverage-merge-bug both projects share one config and the same plugin (extends: true). The only difference is environment: 'jsdom' vs browser mode, so Vite runs the SSR transform for one and the client transform for the other. The SSR source maps leave end.column as null, while the browser ones resolve it. That's a standard Vitest projects setup, so users can't make the pipeline consistent. Today the merge silently doubles those items (6 statements instead of 3), which gives misleading coverage with no warning.

With coverage-v8 there's also no SWC vs Babel instrumentation involved. Vitest builds the istanbul maps from V8 data and source maps, so the mismatch comes from how precise the source maps are.

If the function fallback (matching on decl when body ranges differ) feels too broad, I'm happy to narrow it to the null end-column case, which is enough for vitest-dev/vitest#9366. @AriPerkkio, which scope would you prefer?

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

In https://github.com/stevez/vitest-coverage-merge-bug both projects share one config and the same plugin (extends: true). The only difference is environment: 'jsdom' vs browser mode, so Vite runs the SSR transform for one and the client transform for the other. The SSR source maps leave end.column as null, while the browser ones resolve it. That's a standard Vitest projects setup, so users can't make the pipeline consistent.

This does not reproduce with Vitest 5 + Vite 8. See vitest-dev/vitest#9366 (comment).

@AriPerkkio AriPerkkio closed this Oct 13, 2026
@AriPerkkio AriPerkkio reopened this Oct 13, 2026
@AriPerkkio

Copy link
Copy Markdown
Member

Does this contain fix for vitest-dev/vitest#9480 (comment) too?

@stevez

stevez commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Yes. Functions are matched by their decl range when the body loc differs. In that case only the body's start column differed between the transforms (1:42 vs 1:40), while decl was identical (1:16->1:20).

I ran the exact setup from that review (math.ts, project one without plugins plus project two with SWC) on Vitest 5.0.3:

@vitest/istanbul-lib-coverage fnMap f
1.0.2 sum, subtract, multiply, remainder, sum_2, subtract_2, multiply_2, remainder_2 [1,0,0,0,2,0,0,0]
this PR sum, subtract, multiply, remainder [2,0,0,0]

The unit test merges functions whose body ranges differ uses the locations captured from that run.

To try it against the repro, the test/istanbuljs-pr-25 branch of https://github.com/stevez/vitest-coverage-merge-bug overrides @vitest/istanbul-lib-coverage with a build of this PR: constants.ts goes from 6 statements to 3.

Coverage of the same file from different transforms (e.g. jsdom vs browser
projects, or different transpilers) can disagree on range columns, which
caused merged reports to contain duplicate statements, functions and branches.

When an exact location match fails, fall back to:
- statements and branches: match ignoring the end column
- functions: match by declaration range, as body ranges can differ in start column

Fallback matches are only applied when unambiguous (one-to-one), so distinct
items are never collapsed. Resolved end columns are preferred over `null`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AriPerkkio
AriPerkkio force-pushed the fix/merge-lenient-locations branch from 3d546e4 to 0a12bfa Compare November 18, 2026 14:03
@pkg-pr-new

pkg-pr-new Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
npm i https://pkg.pr.new/@vitest/istanbul-lib-coverage@a24376f
npm i https://pkg.pr.new/@vitest/istanbul-lib-instrument@a24376f
npm i https://pkg.pr.new/@vitest/istanbul-lib-report@a24376f
npm i https://pkg.pr.new/@vitest/istanbul-lib-source-maps@a24376f

commit: a24376f

@AriPerkkio
AriPerkkio force-pushed the fix/merge-lenient-locations branch from 1fd4fa8 to a24376f Compare November 19, 2026 08:13
AriPerkkio added a commit to AriPerkkio/vitest that referenced this pull request Nov 19, 2026
@AriPerkkio AriPerkkio changed the title fix(lib-coverage): merge ranges that differ between transforms fix(lib-coverage): merge ranges with unknown end columns Nov 19, 2026

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

Thanks @stevez! Sorry it took long time to merge. 🙃

@AriPerkkio
AriPerkkio merged commit 992735d into vitest-dev:main Nov 19, 2026
6 checks passed
stevez added a commit to stevez/vitest-coverage-merge-bug that referenced this pull request Nov 20, 2026
Vitest 5.1.1 installs @vitest/istanbul-lib-coverage 2.1.1, which includes
vitest-dev/istanbuljs#25. src/constants.ts now reports 3 statements
instead of 6. Cases that are still duplicated are reproduced on the
repro/start-column-mismatch branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants