src/cmd/go/pkg.go | 4 ---- src/cmd/go/test.go | 21 +++++++++++++++++++++ diff --git a/src/cmd/go/pkg.go b/src/cmd/go/pkg.go index e40f9420c7e2217f9e126cf5f636c282c532d959..575f187f3bd7609d97233c3cb45ccaa90ff75810 100644 --- a/src/cmd/go/pkg.go +++ b/src/cmd/go/pkg.go @@ -955,10 +955,6 @@ // On ARM with GOARM=5, everything depends on math for the link. if p.Name == "main" && goarch == "arm" { importPaths = append(importPaths, "math") } - // In coverage atomic mode everything depends on sync/atomic. - if testCoverMode == "atomic" && (!p.Standard || (p.ImportPath != "runtime/cgo" && p.ImportPath != "runtime/race" && p.ImportPath != "sync/atomic")) { - importPaths = append(importPaths, "sync/atomic") - } } // Runtime and its internal packages depend on runtime/internal/sys, diff --git a/src/cmd/go/test.go b/src/cmd/go/test.go index 6482f0fd32301a83a0bb9174170705b204587ef7..35250c9f6b379428fad9f19ff9b471fceabfb4c9 100644 --- a/src/cmd/go/test.go +++ b/src/cmd/go/test.go @@ -545,6 +545,10 @@ } // Prepare build + run + print actions for all packages being tested. for _, p := range pkgs { + // sync/atomic import is inserted by the cover tool. See #18486 + if testCover && testCoverMode == "atomic" { + ensureImport(p, "sync/atomic") + } buildTest, runTest, printTest, err := b.test(p) if err != nil { str := err.Error() @@ -634,6 +638,23 @@ fmt.Fprintf(os.Stderr, "installing these packages with 'go test %s-i%s' will speed future tests.\n\n", extraOpts, args) } b.do(root) +} + +// ensures that package p imports the named package. +func ensureImport(p *Package, pkg string) { + for _, d := range p.deps { + if d.Name == pkg { + return + } + } + + a := loadPackage(pkg, &importStack{}) + if a.Error != nil { + fatalf("load %s: %v", pkg, a.Error) + } + computeStale(a) + + p.imports = append(p.imports, a) } func contains(x []string, s string) bool {