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

x/tools/go/buildutil: TestContainingPackage fails if tools repo is in non-first GOPATH directory. #19400

dmitshur opened this issue Mar 4, 2017 · 5 comments


Copy link

@dmitshur dmitshur commented Mar 4, 2017

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

$ go version
go version go1.8 darwin/amd64

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

$ go env
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/tw/kgz4v2kn4n7d7ryg5k_z3dk40000gn/T/go-build206861156=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

$ go get -u
$ go test

What did you expect to see?

$ go test
ok	0.077s

What did you see instead?

$ go test
--- FAIL: TestContainingPackage (0.00s)
	util_test.go:67: ContainingPackage("/Users/Dmitri/Dropbox/Work/2013/GoLanding/src/") = (not found), want
	util_test.go:67: ContainingPackage("/var/folders/tw/kgz4v2kn4n7d7ryg5k_z3dk40000gn/T/go607519547/src/") = (not found), want
	util_test.go:67: ContainingPackage("/Users/Dmitri/Dropbox/Work/2013/GoLanding/src/") = (not found), want
	util_test.go:67: ContainingPackage("/var/folders/tw/kgz4v2kn4n7d7ryg5k_z3dk40000gn/T/go607519547/src/") = (not found), want
FAIL	0.078s
Copy link
Member Author

@dmitshur dmitshur commented Mar 4, 2017

I know the exact cause of this issue, the above report is for tracking purposes.

It happens because TestContainingPackage expects/assumes the tools repository to be inside the first GOPATH directory.

In my environment, it happened to be inside the second GOPATH directory rather than first.

The following ad-hoc patch for my particular environment fixes the TestContainingPackage test. Of course, this is not a general fix, just a demonstration of the problem.

$ git diff
diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go
index 72db317..a244a4a 100644
--- a/go/buildutil/util_test.go
+++ b/go/buildutil/util_test.go
@@ -18,7 +18,7 @@ import (
 func TestContainingPackage(t *testing.T) {
        // unvirtualized:
        goroot := runtime.GOROOT()
-       gopath := filepath.SplitList(os.Getenv("GOPATH"))[0]
+       gopath := filepath.SplitList(os.Getenv("GOPATH"))[1]
        type Test struct {
                gopath, filename, wantPkg string
$ go test
ok	0.071s
@gopherbot gopherbot added this to the Unreleased milestone Mar 21, 2017
Copy link
Member Author

@dmitshur dmitshur commented Mar 28, 2017

A related issue is in x/tools/cmd/heapview:

func toolsDir() string {
	gopath := os.Getenv("GOPATH")
	if gopath == "" {
		log.Println("error: GOPATH not set. Can't find client files")
	return filepath.Join(filepath.SplitList(gopath)[0], "src/")


That assumes one's repository is in the first GOPATH workspace, and would fail to work if it's in another.

The issue can be resolved by using go/build package to find the import path directory in any of GOPATH workspaces:

func toolsDir() string {
	p, err := build.Import("", "", build.FindOnly)
	if err != nil {
		log.Println("error: can't find client files:", err)
	return p.Dir

Edit: I've sent CL 38778 that fixes this.

Copy link

@gopherbot gopherbot commented Mar 28, 2017

CL mentions this issue.

gopherbot pushed a commit to golang/tools that referenced this issue Apr 10, 2017
The GOPATH environment variable is defined at as:

> The GOPATH environment variable lists places to look for Go code. On
> Unix, the value is a colon-separated string. On Windows, the value is
> a semicolon-separated string. On Plan 9, the value is a list.

It is legal for Go packages to be in any of those places, not only
the first entry. Look in all places for repository.

Cache the directory that is found. It's slightly more expensive
to look for it, since potentially multiple directories must be
checked for existence.

Updates golang/go#19400.

Change-Id: I16661b7149d52ea6168fffc605c842d7a4da009b
Reviewed-by: Michael Matloob <>
@dmitshur dmitshur self-assigned this Feb 17, 2018
Copy link

@gopherbot gopherbot commented Feb 17, 2018

Change mentions this issue: go/buildutil: use build.Import to find GOPATH containing x/tools

Copy link
Member Author

@dmitshur dmitshur commented Feb 17, 2018

I've sent CL 94900 that resolves this issue.

@dmitshur dmitshur removed their assignment Feb 17, 2018
@golang golang locked and limited conversation to collaborators Mar 15, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.