Commit 8d88c9ae authored by Russ Cox's avatar Russ Cox

cmd/go: fix -coverpkg=all with dot imports

If you use -coverpkg=all you get coverage for all packages in the build.
Go 1.9 used a global counter for all the GoCover variables, so that they
were distinct for the entire build. The global counter caused problems
with caching, so we switched to a per-package counter. But now the
GoCover_0 in one package may be dot-imported into another and
conflict with the GoCover_0 in that other package.

Reestablish (overwhelmingly likely) global uniqueness of GoCover
variables by appending an _xxxxxxxxxxxx suffix, where the x's are
the prefix of the SHA256 hash of the import path. The point is only
to avoid accidents, not to defeat people determined to break the tools.

Fixes #23432.

Change-Id: I3088eceebbe35174f2eefe8d558b7c8b59d3eeac
Reviewed-on: https://go-review.googlesource.com/89135Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 236abdb4
...@@ -2437,6 +2437,16 @@ func TestCoverageRuns(t *testing.T) { ...@@ -2437,6 +2437,16 @@ func TestCoverageRuns(t *testing.T) {
checkCoverage(tg, data) checkCoverage(tg, data)
} }
func TestCoverageDotImport(t *testing.T) {
tg := testgo(t)
defer tg.cleanup()
tg.parallel()
tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
tg.run("test", "-coverpkg=coverdot1,coverdot2", "coverdot2")
data := tg.getStdout() + tg.getStderr()
checkCoverage(tg, data)
}
// Check that coverage analysis uses set mode. // Check that coverage analysis uses set mode.
// Also check that coverage profiles merge correctly. // Also check that coverage profiles merge correctly.
func TestCoverageUsesSetMode(t *testing.T) { func TestCoverageUsesSetMode(t *testing.T) {
......
...@@ -6,6 +6,7 @@ package test ...@@ -6,6 +6,7 @@ package test
import ( import (
"bytes" "bytes"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"go/ast" "go/ast"
...@@ -1091,13 +1092,21 @@ func isTestFile(file string) bool { ...@@ -1091,13 +1092,21 @@ func isTestFile(file string) bool {
func declareCoverVars(importPath string, files ...string) map[string]*load.CoverVar { func declareCoverVars(importPath string, files ...string) map[string]*load.CoverVar {
coverVars := make(map[string]*load.CoverVar) coverVars := make(map[string]*load.CoverVar)
coverIndex := 0 coverIndex := 0
// We create the cover counters as new top-level variables in the package.
// We need to avoid collisions with user variables (GoCover_0 is unlikely but still)
// and more importantly with dot imports of other covered packages,
// so we append 12 hex digits from the SHA-256 of the import path.
// The point is only to avoid accidents, not to defeat users determined to
// break things.
sum := sha256.Sum256([]byte(importPath))
h := fmt.Sprintf("%x", sum[:6])
for _, file := range files { for _, file := range files {
if isTestFile(file) { if isTestFile(file) {
continue continue
} }
coverVars[file] = &load.CoverVar{ coverVars[file] = &load.CoverVar{
File: filepath.Join(importPath, file), File: filepath.Join(importPath, file),
Var: fmt.Sprintf("GoCover_%d", coverIndex), Var: fmt.Sprintf("GoCover_%d_%x", coverIndex, h),
} }
coverIndex++ coverIndex++
} }
......
package coverdot1
func F() {}
package coverdot2
import . "coverdot1"
func G() { F() }
package coverdot2
import "testing"
func TestG(t *testing.T) {
G()
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment