fix: stale cached coverage of changed files survives the Tia coverage merge as phantom uncovered lines - #1792
Open
AlessioGiacobbe wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
When Tia piggybacks coverage (
--tia --coverage), the merged report contains phantom "executable but uncovered" lines for every changed file, at the file's old line numbers, alongside the live data at the new line numbers. Coverage for changed files is deflated on every filtered run, and lines that are now comments/blank can show up as uncovered executable lines.Root cause
Two behaviours compose into the bug:
CoverageMerger::stripCurrentTestsFromCached()removes the re-run test IDs from the cached baseline's line entries, but keeps the line keys — an entry like[42 => ['testA']]becomes[42 => []], which inProcessedCodeCoverageDatasemantics means executable but uncovered.ProcessedCodeCoverageData::merge()(phpunit/php-code-coverage) never removes lines that exist only in the destination side — any line missing from$newDatais skipped viacontinue.For a changed file, every test that covered it is in the affected set and has just been re-run, so the live report already carries the file's entire coverage at the new line numbers. The cached entries at the old line numbers are pure staleness — but nothing invalidates them, so the stripped-to-empty entries survive the merge as phantom uncovered lines. Cached
functionCoverageof changed files (e.g. renamed functions) survives the same way, and so does the data of deleted files.The fix
Invalidate the cached data of changed files before merging:
Tia::enterReplayMode()persists the changed-files list (the same one fed toGraph::affected(), as absolute paths) under a new state key,coverage.changed.json. The key is cleared alongside the marker write inhandleParent()so a list from an aborted run can never leak into a later one.CoverageMerger::applyIfMarked()consumes that key and drops those files from the cached baseline's line and function coverage before the strip + merge. Since the affected set re-runs every test that covered a changed file, the live report fully re-measures it — the merge then simply copies the live data over.Unchanged files keep the existing behaviour: rerun test IDs are stripped from their cached entries and re-attributed from the live data, while tests outside the affected set keep their cached attribution.
Tests
tests/Unit/Plugins/Tia/CoverageMerger.php(new):functionCoverage(fails on 5.x with10 => [], 12 => []surviving the merge);