-
Notifications
You must be signed in to change notification settings - Fork 18.4k
cmd/go: invalidate coverage report cache when coverpkg sources change #74773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ryancurrah
wants to merge
1
commit into
golang:master
from
ryancurrah:fix/coverage-report-when-coverpkg-is-used
+180
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
137 changes: 137 additions & 0 deletions
src/cmd/go/testdata/script/test_cache_coverpkg_bug.txt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
env GO111MODULE=on | ||
|
||
# Test for bug where cached coverage profiles with -coverpkg can contain | ||
# outdated line references when source files are modified. | ||
# This reproduces the issue where coverage data from cache may reference | ||
# lines that no longer exist in the updated source files. | ||
|
||
[short] skip | ||
[GODEBUG:gocacheverify=1] skip | ||
|
||
# We're testing cache behavior, so start with a clean GOCACHE. | ||
env GOCACHE=$WORK/cache | ||
|
||
# Create a project structure with multiple packages | ||
# proj/ | ||
# some_func.go | ||
# some_func_test.go | ||
# sub/ | ||
# sub.go | ||
# sub_test.go | ||
# sum/ | ||
# sum.go | ||
|
||
# Switch to the proj directory | ||
cd proj | ||
|
||
# Run tests with -coverpkg to collect coverage for all packages | ||
go test -coverpkg=proj/... -coverprofile=cover1.out ./... | ||
stdout 'coverage:' | ||
|
||
# Verify the first coverage profile exists and has expected content | ||
exists cover1.out | ||
grep -q 'proj/sub/sub.go:' cover1.out | ||
|
||
# Run again to ensure caching works | ||
go test -coverpkg=proj/... -coverprofile=cover1_cached.out ./... | ||
stdout '\(cached\)' | ||
stdout 'coverage:' | ||
|
||
# Note: Due to the bug, cached coverage profiles may have duplicate entries | ||
# This is the issue we're trying to fix | ||
|
||
# Now modify sub.go to change line structure - this will invalidate | ||
# the cache for sub package but not for proj package | ||
cp ../sub_modified.go sub/sub.go | ||
|
||
# The bug manifests as duplicate or invalid line references in the coverage profile | ||
# After modifying sub.go, we should not have both old and new line references | ||
go test -coverpkg=proj/... -coverprofile=cover2.out ./... | ||
stdout 'coverage:' | ||
|
||
# With the bug present, we would see duplicate entries for the same lines | ||
# This demonstrates the bug - there should be duplicates in the coverage profile | ||
grep 'proj/sub/sub.go:' cover2.out | ||
# The fix should ensure that only the new line format exists, not the old one | ||
grep 'proj/sub/sub.go:3.24,4.35' cover2.out | ||
# This should fail if the stale coverage line exists (the bug is present) | ||
! grep 'proj/sub/sub.go:3.24,4.22' cover2.out | ||
|
||
-- proj/go.mod -- | ||
module proj | ||
|
||
go 1.21 | ||
|
||
-- proj/some_func.go -- | ||
package proj | ||
|
||
import "proj/sum" | ||
|
||
func SomeFunc(a, b int) int { | ||
if a == 0 && b == 0 { | ||
return 0 | ||
} | ||
return sum.Sum(a, b) | ||
} | ||
|
||
-- proj/some_func_test.go -- | ||
package proj | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_SomeFunc(t *testing.T) { | ||
t.Run("test1", func(t *testing.T) { | ||
result := SomeFunc(1, 1) | ||
if result != 2 { | ||
t.Errorf("Expected 2, got %d", result) | ||
} | ||
}) | ||
} | ||
|
||
-- proj/sub/sub.go -- | ||
package sub | ||
|
||
func Sub(a, b int) int { | ||
if a == 0 && b == 0 { | ||
return 0 | ||
} | ||
return a - b | ||
} | ||
|
||
-- proj/sub/sub_test.go -- | ||
package sub | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_Sub(t *testing.T) { | ||
t.Run("test_sub1", func(t *testing.T) { | ||
result := Sub(1, 1) | ||
if result != 0 { | ||
t.Errorf("Expected 0, got %d", result) | ||
} | ||
}) | ||
} | ||
|
||
-- proj/sum/sum.go -- | ||
package sum | ||
|
||
func Sum(a, b int) int { | ||
if a == 0 { | ||
return b | ||
} | ||
return a + b | ||
} | ||
|
||
-- sub_modified.go -- | ||
package sub | ||
|
||
func Sub(a, b int) int { | ||
if a == 0 && b == 0 || a == -100 { | ||
return 0 | ||
} | ||
return a - b | ||
} |
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.