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

Closed
dmitshur opened this issue Mar 4, 2017 · 5 comments

Comments

@dmitshur
Copy link
Contributor

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
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/Dmitri/Dropbox/Work/2013/GoLanding:/Users/Dmitri/Dropbox/Work/2013/GoLand"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
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"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

$ go get -u golang.org/x/tools/go/buildutil
$ go test golang.org/x/tools/go/buildutil

What did you expect to see?

$ go test golang.org/x/tools/go/buildutil
ok  	golang.org/x/tools/go/buildutil	0.077s

What did you see instead?

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

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 golang.org/x/tools/go/buildutil
ok  	golang.org/x/tools/go/buildutil	0.071s

@gopherbot gopherbot added this to the Unreleased milestone Mar 21, 2017
@dmitshur
Copy link
Contributor Author

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")
		os.Exit(1)
	}
	return filepath.Join(filepath.SplitList(gopath)[0], "src/golang.org/x/tools")
}

Source: https://github.com/golang/tools/blob/d63e2b22b05a9682de336cd4802bba367ed429e7/cmd/heapview/main.go#L36-L43.

That assumes one's golang.org/x/tools 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("golang.org/x/tools", "", build.FindOnly)
	if err != nil {
		log.Println("error: can't find client files:", err)
		os.Exit(1)
	}
	return p.Dir
}

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

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/38778 mentions this issue.

gopherbot pushed a commit to golang/tools that referenced this issue Apr 10, 2017
The GOPATH environment variable is defined at
https://golang.org/cmd/go/#hdr-GOPATH_environment_variable 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 golang.org/x/tools 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-on: https://go-review.googlesource.com/38778
Reviewed-by: Michael Matloob <matloob@golang.org>
@dmitshur dmitshur self-assigned this Feb 17, 2018
@gopherbot
Copy link
Contributor

Change https://golang.org/cl/94900 mentions this issue: go/buildutil: use build.Import to find GOPATH containing x/tools

@dmitshur
Copy link
Contributor Author

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.
Projects
None yet
Development

No branches or pull requests

2 participants