Navigation Menu

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
Open

cmd/cover: cannot find package in local directory outside GOPATH #17269

gertcuykens opened this issue Sep 28, 2016 · 6 comments
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@gertcuykens
Copy link
Contributor

gertcuykens 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
Copy link
Contributor

vdobler 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
Copy link
Contributor Author

gertcuykens 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 the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Oct 3, 2016
@quentinmit quentinmit added this to the Go1.8Maybe milestone Oct 3, 2016
@rsc
Copy link
Contributor

rsc 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 The path to resolution is known, but the work has not been done. and removed NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Oct 20, 2016
@rsc rsc modified the milestones: Go1.9, Go1.8Maybe Nov 2, 2016
@mattn
Copy link
Member

mattn 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
`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
`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
`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
`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
`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
`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
`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
`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
`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
`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
`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
`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
`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
Copy link
Contributor

rsc 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
Copy link

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
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

8 participants