golang/go

runtime/coverage: WriteCounters fails after patching the compiler to allow internal imports

Opened this issue · 1 comments

Go version

go version go1.25.3 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/usr/local/google/home/avagin/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/usr/local/google/home/avagin/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build2846895519=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/usr/local/google/home/avagin/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/usr/local/google/home/avagin/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/google/home/avagin/git/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/usr/local/google/home/avagin/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/google/home/avagin/git/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

avagin@avagin:~/git/gvisor$ git clone https://github.com/google/gvisor -b go
avagin@avagin:~/git/gvisor$ gopkgs=$(../go/bin/go list ./... | paste -sd,)
avagin@avagin:~/git/gvisor$ CGO_ENABLED=0 ../go/bin/go build --tags kcov,opensource -covermode=atomic -cover -o runsc.bin runsc/main.go
avagin@avagin:~/git/gvisor$ ./runsc.bin --rootless --debug --debug-log /tmp/runsc.log --network none --strace --coverage-report /tmp/kcov.data do true
warning: GOCOVERDIR not set, no coverage data emitted
warning: GOCOVERDIR not set, no coverage data emitted
warning: GOCOVERDIR not set, no coverage data emitted
avagin@avagin:~/git/gvisor$ ls -l /tmp/kcov.data
-rw-r----- 1 avagin primarygroup 680898 Nov  7 20:09 /tmp/kcov.data
avagin@avagin:~/git/gvisor$ grep coverage.Write /tmp/runsc.log
avagin@avagin:~/git/gvisor$ CGO_ENABLED=0 ../go/bin/go build --tags kcov,opensource -covermode=atomic -cover -coverpkg="gvisor.dev/gvisor/pkg/sentry/kernel" -o runsc.bin runsc/main.go
avagin@avagin:~/git/gvisor$ unlink /tmp/runsc.log
avagin@avagin:~/git/gvisor$ unlink /tmp/kcov.data
avagin@avagin:~/git/gvisor$ ./runsc.bin --rootless --debug --debug-log /tmp/runsc.log --network none --strace --coverage-report /tmp/kcov.data do true
avagin@avagin:~/git/gvisor$ ls -l /tmp/kcov.data
-rw-r----- 1 avagin primarygroup 0 Nov  7 20:09 /tmp/kcov.data
avagin@avagin:~/git/gvisor$ grep coverage.Write /tmp/runsc.log
W1107 20:09:50.513421       1 coverage_unsafe.go:243] coverage.WriteCounters failed: WriteCounters invoked for program built with -covermode=<invalid> (please use -covermode=atomic)

What did you see happen?

coverage.WriteCounters returns the error: "WriteCounters invoked for program built with -covermode= (please use -covermode=atomic)" when using -coverpkg during compilation. go build is executed with -covermode=atomic.

What did you expect to see?

coverage.WriteCounters reports counters.

The go compiler is built with this patch

diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go
index 1f791546f9..809e5f142b 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -1464,6 +1464,7 @@ func reusePackage(p *Package, stk *ImportStack) *Package {
 // If the import is allowed, disallowInternal returns the original package p.
 // If not, it returns a new package containing just an appropriate error.
 func disallowInternal(ctx context.Context, srcDir string, importer *Package, importerPath string, p *Package, stk *ImportStack) *PackageError {
+       return nil
        // golang.org/s/go14internal:
        // An import of a path containing the element “internal”
        // is disallowed if the importing code is outside the tree

More details why it is required can be found in #76098.