- Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
Go version
go version go1.22.0 linux/amd64
Output of go env in your module/workspace:
GO111MODULE='' GOARCH='amd64' GOBIN='' GOCACHE='/home/mjh/.cache/go-build' GOENV='/home/mjh/.config/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='amd64' GOHOSTOS='linux' GOINSECURE='' GOMODCACHE='/home/mjh/.local/share/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='linux' GOPATH='/home/mjh/.local/share/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/home/mjh/sdk/go1.22.0' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/home/mjh/sdk/go1.22.0/pkg/tool/linux_amd64' GOVCS='' GOVERSION='go1.22.0' GCCGO='gccgo' GOAMD64='v1' AR='ar' CC='gcc' CXX='g++' CGO_ENABLED='1' GOMOD='/home/mjh/src/personal/go-cov/go.mod' GOWORK='' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3982892531=/tmp/go-build -gno-record-gcc-switches'What did you do?
Ran Go tests with -coverpkg=./... in a directory which included GOMODCACHE/GOCACHE in a subdirectory starting with "." (the use case is for caching in GitLab, which requires directories stored in caches exist in the working directory, see their Go example https://docs.gitlab.com/ee/ci/caching/#cache-go-dependencies)
Here's a bash script to reproduce:
#!/usr/bin/env bash set -o errexit -o pipefail -o nounset mkdir proj cd proj go mod init example-proj # create a trivial lib with 100% test coverage cat <<EOF > lib.go package lib import ( // add an unused dependency, just so something is stored in the cache directory _ "golang.org/x/tools/cover" ) func Sum(a int, b int) int { return a + b } EOF cat <<EOF > lib_test.go package lib import ( "testing" ) func TestSum(t *testing.T) { if Sum(1, 2) != 3 { t.Error("fail") } } EOF # .go contains directories we wish to cache mkdir .go export GOMODCACHE="$PWD/.go/mod_cache" export GOCACHE="$PWD/.go/cache" go mod tidy go test -coverprofile=coverage.out -coverpkg=./... ./...What did you see happen?
The modules under .go were included in test coverage, output of the above script (note the coverage % in the last line):
go: creating new go.mod: module example-proj go: finding module for package golang.org/x/tools/cover go: downloading golang.org/x/tools v0.18.0 go: found golang.org/x/tools/cover in golang.org/x/tools v0.18.0 ok example-proj 0.002s coverage: 0.8% of statements in ./... inspecting the coverage profile:
$ grep --invert-match "$(go list -m)" coverage.out | head mode: set golang.org/x/tools/cover/profile.go:37.41,37.58 1 0 golang.org/x/tools/cover/profile.go:38.41,38.81 1 0 golang.org/x/tools/cover/profile.go:39.41,39.68 1 0 golang.org/x/tools/cover/profile.go:43.57,45.16 2 0 golang.org/x/tools/cover/profile.go:45.16,47.3 1 0 golang.org/x/tools/cover/profile.go:48.2,49.36 2 0 golang.org/x/tools/cover/profile.go:54.64,62.15 4 0 golang.org/x/tools/cover/profile.go:62.15,64.17 2 0 golang.org/x/tools/cover/profile.go:64.17,66.48 2 0What did you expect to see?
Modules under the .go directory to be ignored, per https://pkg.go.dev/cmd/go#hdr-Package_lists_and_patterns
Directory and file names that begin with "." or "_" are ignored by the go tool, as are directories named "testdata".
EDIT: for anyone looking for a workaround, I just filtered out lines not belonging to my module in the coverage file:
awk -v go_mod="$(go list -m)" 'NR==1 || $0 ~ "^"go_mod' "coverage.in" > coverage.out