Repository navigation
fix(lib-coverage): merge ranges with unknown end columns - #25
Conversation
|
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. |
|
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 ( With If the function fallback (matching on |
There was a problem hiding this comment.
In https://github.com/stevez/vitest-coverage-merge-bug both projects share one config and the same plugin (
extends: true). The only difference isenvironment: 'jsdom'vs browser mode, so Vite runs the SSR transform for one and the client transform for the other. The SSR source maps leaveend.columnasnull, while the browser ones resolve it. That's a standard Vitestprojectssetup, so users can't make the pipeline consistent.
This does not reproduce with Vitest 5 + Vite 8. See vitest-dev/vitest#9366 (comment).
|
Does this contain fix for vitest-dev/vitest#9480 (comment) too? |
|
Yes. Functions are matched by their I ran the exact setup from that review (
The unit test To try it against the repro, the |
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>
3d546e4 to
0a12bfa
Compare
commit: |
1fd4fa8 to
a24376f
Compare
AriPerkkio
left a comment
There was a problem hiding this comment.
Thanks @stevez! Sorry it took long time to merge. 🙃
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>
Problem
When the same file is covered by multiple transforms,
FileCoverage#merge()duplicates statements, functions and branches. For example, Vitestprojectswith jsdom + browser. Items are matched by their exactstart|endlocation, but source maps can leave the end column unresolved on one side:Infinity. It becomesnullonce serialized as JSON, e.g. incoverage-final.jsonor with--merge-reports.1:22->7:Infinityand1:22->7:10are 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
mergePropkeeps exact matching. When a location key has an unknown end column (Infinityornull) 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 throughmergeProp. 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:
nullvs resolved end columns, on either side of the mergeInfinityvs resolved end columnsnullvs resolved end columnsThe 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.tsgoes 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