Skip to content

Commit 0f91f92

Browse files
committed
internal/coverage/pods: sort counter files first by origin, then name
This patch fixes a problem with the way pods (clumps of related coverage meta+counter data files) are collected, which was causing problems for "go tool covdata subtract". A subtract operation such as "go tool covdata subtract -i=dir1,dir2 -o=out" works by loading in all the counter data files from "dir1" before any of the data files from "dir2" are loaded. The sorting function in the pods code was sorting counter files for a given pod based purely on name, which meant that differences in process ID assignment could result in some files from "dir2" being presented before "dir1". The fix is to change the sorting compare function to prefer origin directory over filename. Fixes #60526. Change-Id: I2226ea675fc99666a9a28e6550d823bcdf2d6977 Reviewed-on: https://go-review.googlesource.com/c/go/+/499317 Run-TryBot: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Michael Pratt <[email protected]>
1 parent 094c752 commit 0f91f92

File tree

2 files changed

+19
-16
lines changed

2 files changed

+19
-16
lines changed

src/internal/coverage/pods/pods.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ func collectPodsImpl(files []string, dirIndices []int, warn bool) []Pod {
166166
pods := make([]Pod, 0, len(mm))
167167
for _, p := range mm {
168168
sort.Slice(p.elements, func(i, j int) bool {
169+
if p.elements[i].origin != p.elements[j].origin {
170+
return p.elements[i].origin < p.elements[j].origin
171+
}
169172
return p.elements[i].file < p.elements[j].file
170173
})
171174
pod := Pod{

src/internal/coverage/pods/pods_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ func TestPodCollection(t *testing.T) {
4040
return mkfile(dir, fn)
4141
}
4242

43-
mkcounter := func(dir string, tag string, nt int) string {
43+
mkcounter := func(dir string, tag string, nt int, pid int) string {
4444
hash := md5.Sum([]byte(tag))
45-
dummyPid := int(42)
46-
fn := fmt.Sprintf(coverage.CounterFileTempl, coverage.CounterFilePref, hash, dummyPid, nt)
45+
fn := fmt.Sprintf(coverage.CounterFileTempl, coverage.CounterFilePref, hash, pid, nt)
4746
return mkfile(dir, fn)
4847
}
4948

@@ -76,26 +75,26 @@ func TestPodCollection(t *testing.T) {
7675

7776
// Add a meta-data file with two counter files to first dir.
7877
mkmeta(o1, "m1")
79-
mkcounter(o1, "m1", 1)
80-
mkcounter(o1, "m1", 2)
81-
mkcounter(o1, "m1", 2)
78+
mkcounter(o1, "m1", 1, 42)
79+
mkcounter(o1, "m1", 2, 41)
80+
mkcounter(o1, "m1", 2, 40)
8281

8382
// Add a counter file with no associated meta file.
84-
mkcounter(o1, "orphan", 9)
83+
mkcounter(o1, "orphan", 9, 39)
8584

8685
// Add a meta-data file with three counter files to second dir.
8786
mkmeta(o2, "m2")
88-
mkcounter(o2, "m2", 1)
89-
mkcounter(o2, "m2", 2)
90-
mkcounter(o2, "m2", 3)
87+
mkcounter(o2, "m2", 1, 38)
88+
mkcounter(o2, "m2", 2, 37)
89+
mkcounter(o2, "m2", 3, 36)
9190

9291
// Add a duplicate of the first meta-file and a corresponding
9392
// counter file to the second dir. This is intended to capture
9493
// the scenario where we have two different runs of the same
9594
// coverage-instrumented binary, but with the output files
9695
// sent to separate directories.
9796
mkmeta(o2, "m1")
98-
mkcounter(o2, "m1", 11)
97+
mkcounter(o2, "m1", 11, 35)
9998

10099
// Collect pods.
101100
podlist, err := pods.CollectPods([]string{o1, o2}, true)
@@ -114,14 +113,15 @@ func TestPodCollection(t *testing.T) {
114113

115114
expected := []string{
116115
`o1/covmeta.ae7be26cdaa742ca148068d5ac90eaca [
116+
o1/covcounters.ae7be26cdaa742ca148068d5ac90eaca.40.2 o:0
117+
o1/covcounters.ae7be26cdaa742ca148068d5ac90eaca.41.2 o:0
117118
o1/covcounters.ae7be26cdaa742ca148068d5ac90eaca.42.1 o:0
118-
o1/covcounters.ae7be26cdaa742ca148068d5ac90eaca.42.2 o:0
119-
o2/covcounters.ae7be26cdaa742ca148068d5ac90eaca.42.11 o:1
119+
o2/covcounters.ae7be26cdaa742ca148068d5ac90eaca.35.11 o:1
120120
]`,
121121
`o2/covmeta.aaf2f89992379705dac844c0a2a1d45f [
122-
o2/covcounters.aaf2f89992379705dac844c0a2a1d45f.42.1 o:1
123-
o2/covcounters.aaf2f89992379705dac844c0a2a1d45f.42.2 o:1
124-
o2/covcounters.aaf2f89992379705dac844c0a2a1d45f.42.3 o:1
122+
o2/covcounters.aaf2f89992379705dac844c0a2a1d45f.36.3 o:1
123+
o2/covcounters.aaf2f89992379705dac844c0a2a1d45f.37.2 o:1
124+
o2/covcounters.aaf2f89992379705dac844c0a2a1d45f.38.1 o:1
125125
]`,
126126
}
127127
for k, exp := range expected {

0 commit comments

Comments
 (0)