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/cover: cannot find package in local directory outside GOPATH #17269

Open
gertcuykens opened this issue Sep 28, 2016 · 6 comments

Comments

@gertcuykens
Copy link

commented Sep 28, 2016

version go1.7.1 darwin/amd64

When I try the following

go test -coverprofile=coverage.out

I get this coverage.out:

mode: set
_/Users/gert/Desktop/httx/auth.go:10.66,11.54 1 0
_/Users/gert/Desktop/httx/auth.go:11.54,13.89 2 0
_/Users/gert/Desktop/httx/auth.go:17.3,17.11 1 0
_/Users/gert/Desktop/httx/auth.go:13.89,16.4 2 0
_/Users/gert/Desktop/httx/auth.go:22.42,25.2 2 0
...

But when I then do

go tool cover -func=coverage.out

the cover tool doesn't understand "_/Users/gert/Desktop/httx/"

cover: can't find "auth.go": cannot find package "_/Users/gert/Desktop/httx/" in any of:                                     
        /usr/local/Cellar/go/1.7.1/libexec/src/_/Users/gert/Desktop/httx (from $GOROOT)                                      
        /Users/gert/go/src/_/Users/gert/Desktop/httx (from $GOPATH)

Note that go test -cover works.

PASS                                                                                                                         
coverage: 29.7% of statements                                                                                                
ok      _/Users/gert/Desktop/httx       0.015s

I tried to be sure if I wasn't doing something wrong and asked on slack and stackoverflow

Can the cover tool first check for "_/Users/gert/Desktop/httx/auth.go" in the directory the go tool cover is launched in like go test -cover does?

For me it's counter intuitive to be forced to put it in your GOPATH for it to work.

@vdobler

This comment has been minimized.

Copy link
Contributor

commented Sep 29, 2016

As basically everything the go tool works on has to be under GOPATH I think the "fix" should be the other way around: The original go test command should fail.

@gertcuykens

This comment has been minimized.

Copy link
Author

commented Sep 29, 2016

If thats the case then I agree go test -coverand go test -coverprofile=coverage.out should be consistent and also give errors.

@quentinmit quentinmit added this to the Go1.8Maybe milestone Oct 3, 2016

@rsc

This comment has been minimized.

Copy link
Contributor

commented Oct 20, 2016

We could have cover run 'go list -json .' or just build.Import(".") and if it succeeds and produces the import path we're looking for, use that.

@rsc rsc changed the title cmd/cover: coverprofile cmd/cover: cannot find package in local directory outside GOPATH Oct 20, 2016

@rsc rsc added NeedsFix and removed NeedsDecision labels Oct 20, 2016

@rsc rsc modified the milestones: Go1.9, Go1.8Maybe Nov 2, 2016

@mattn

This comment has been minimized.

Copy link
Member

commented Dec 27, 2016

How about de-normalize prefixes?

diff --git a/src/cmd/cover/func.go b/src/cmd/cover/func.go
index 66ec242..feeb799 100644
--- a/src/cmd/cover/func.go
+++ b/src/cmd/cover/func.go
@@ -15,6 +15,9 @@ import (
 	"go/token"
 	"os"
 	"path/filepath"
+	"regexp"
+	"runtime"
+	"strings"
 	"text/tabwriter"
 )
 
@@ -54,7 +57,7 @@ func funcOutput(profile, outputFile string) error {
 
 	var total, covered int64
 	for _, profile := range profiles {
-		fn := profile.FileName
+		fn := denormalize(profile.FileName)
 		file, err := findFile(fn)
 		if err != nil {
 			return err
@@ -153,12 +156,31 @@ func (f *FuncExtent) coverage(profile *Profile) (num, den int64) {
 	return covered, total
 }
 
+func denormalize(file string) string {
+	if runtime.GOOS == "windows" {
+		re := regexp.MustCompile(`^_\\([A-Z])_\\`)
+		if re.MatchString(file) {
+			m := re.FindStringSubmatch(file)
+			if len(m) == 2 {
+				file = m[1] + ":" + file[4:]
+			}
+		}
+	} else {
+		if strings.HasPrefix(file, "_/") {
+			file = file[2:]
+		}
+	}
+	return file
+}
+
 // findFile finds the location of the named file in GOROOT, GOPATH etc.
 func findFile(file string) (string, error) {
 	dir, file := filepath.Split(file)
 	pkg, err := build.Import(dir, ".", build.FindOnly)
 	if err != nil {
-		return "", fmt.Errorf("can't find %q: %v", file, err)
+		if _, err2 := os.Stat(file); err2 != nil {
+			return "", fmt.Errorf("can't find %q: %v", file, err)
+		}
 	}
 	return filepath.Join(pkg.Dir, file), nil
 }

I'm not sure denormalize is right word or not. unescape ?

chriscasola added a commit to chriscasola/nlp that referenced this issue Mar 31, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Mar 31, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Mar 31, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269
chriscasola added a commit to chriscasola/nlp that referenced this issue Apr 1, 2017
chore: attempt build fix
`go tool cover` and `go test` create package paths differently
when code is not inside a "src" directory, and circleci doesn't clone
code into a src directory.

golang/go#17269

@bradfitz bradfitz modified the milestones: Go1.10, Go1.9 Jun 15, 2017

@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 29, 2017

Moving to Go 1.11. Will need to revisit with package management anyway.

@rsc rsc modified the milestones: Go1.10, Go1.11 Nov 29, 2017

@gopherbot gopherbot removed this from the Go1.11 milestone May 23, 2018

@gopherbot gopherbot added this to the Unplanned milestone May 23, 2018

@Lewiscowles1986

This comment has been minimized.

Copy link

commented Aug 19, 2018

I've just run into this. I suppose I'll have to edit my GOPATH, but it would be nice to shed the importance of that ENV variable, instead establish expectations of folder structures or as mentioned above a json file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
8 participants
You can’t perform that action at this time.