Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: coverpkg broken behaviour in version 1.10.1 #25093

Closed
apremalal opened this issue Apr 26, 2018 · 7 comments
Closed

cmd/go: coverpkg broken behaviour in version 1.10.1 #25093

apremalal opened this issue Apr 26, 2018 · 7 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@apremalal
Copy link

apremalal commented Apr 26, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.10.1 darwin/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ag/Library/Caches/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/ag/workspace"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/2t/8v5wp6zn36n2fgxf5qvd75zw0000gq/T/go-build821224783=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

My repo structure is like below, and I'm executing commands indie folder

<go-test-cov>
 |
  -<models>
 |
  -<models-test>

I have hosted the code in github for easy referencing.

models/math.go

package models

func Add(x,y int) int{
  return x+y
}

models_test/math_test.go

package models_test

import (
   "testing"
  "github.com/apremalal/go-test-cov/models"
)

func TestAdd(t *testing.T) {
    total := models.Add(5, 5)
    if total != 10 {
       t.Errorf("Sum was incorrect, got: %d, want: %d.", total, 10)
    }
}

I tried the following command which worked in golang 1.9.X versions. And gave me correct coverage results

go test -v ./models_test -coverpkg=./models -coverprofile=c.out && go tool cover -func=c.out

But with version 1.10.1 it throws a warning saying

no packages being tested depend on matches for pattern ./models

And the coverage is always 0% with the above command

What did you expect to see?

100% test coverage

What did you see instead?

0% test coverage

@agnivade agnivade changed the title coverpkg broken behaviour in version 1.10.1 cmd/go: coverpkg broken behaviour in version 1.10.1 Apr 26, 2018
@agnivade agnivade added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 26, 2018
@agnivade agnivade added this to the Go1.11 milestone Apr 26, 2018
@majewsky
Copy link

I can reproduce the issue with package github.com/majewsky/schwift and Go 1.10.1. Downgrading to Go 1.9.4 fixes the problem for me.

@majewsky
Copy link

majewsky commented May 2, 2018

Still reproducible with Go 1.10.2. Furthermore, I noticed that the warning that @apremalal mentions also exists in 1.9.4 (where coverage is reported correctly), but is worded slightly differently. With Go 1.9.4:

$ go test -covermode count -coverpkg github.com/majewsky/schwift,github.com/majewsky/schwift/gopherschwift  -coverprofile cover.out github.com/majewsky/schwift/tests
warning: no packages being tested depend on github.com/majewsky/schwift
warning: no packages being tested depend on github.com/majewsky/schwift/gopherschwift
ok      github.com/majewsky/schwift/tests       14.399s coverage: 80.3% of statements in github.com/majewsky/schwift, github.com/majewsky/schwift/gopherschwift

Same command with Go 1.10.2:

warning: no packages being tested depend on matches for pattern github.com/majewsky/schwift
warning: no packages being tested depend on matches for pattern github.com/majewsky/schwift/gopherschwift
ok      github.com/majewsky/schwift/tests       12.036s coverage: 0.0% of statements in github.com/majewsky/schwift, github.com/majewsky/schwift/gopherschwift

This makes me think that e33794f is where the error was introduced. I did not have the time to do a bisect, though.

@gopherbot
Copy link

Change https://golang.org/cl/122555 mentions this issue: cmd/go: search test imports when matching -coverpkg

@vaishali-ntnx
Copy link

In which release this fix is available?

@davecheney
Copy link
Contributor

Go 1.11

@vaishali-ntnx
Copy link

Thanks @davecheney

@TACIXAT
Copy link

TACIXAT commented Sep 23, 2018

Sorry to bump this, but for the search indexers:

If you're really confused about how to get code coverage for an external package or imported package in golang, you might be running into this bug. Update to 1.11+, then -coverpkg will work with the package name.

@golang golang locked and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

7 participants