go/buildutil: use build.Import to find GOPATH containing x/tools

Previously, the test assumed that golang.org/x/tools was always in the
first GOPATH workspace. It may not be. Find it dynamically instead.

Fixes golang/go#19400.

Change-Id: I9c75d5ff1409aebd403d7ad4314b496fe1a04140
Reviewed-on: https://go-review.googlesource.com/94900
Run-TryBot: Dmitri Shuralyov <dmitri@shuralyov.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go
index 72db317..f7c26dd 100644
--- a/go/buildutil/util_test.go
+++ b/go/buildutil/util_test.go
@@ -8,7 +8,6 @@
 	"go/build"
 	"io/ioutil"
 	"os"
-	"path/filepath"
 	"runtime"
 	"testing"
 
@@ -18,7 +17,7 @@
 func TestContainingPackage(t *testing.T) {
 	// unvirtualized:
 	goroot := runtime.GOROOT()
-	gopath := filepath.SplitList(os.Getenv("GOPATH"))[0]
+	gopath := gopathContainingTools(t)
 
 	type Test struct {
 		gopath, filename, wantPkg string
@@ -70,3 +69,13 @@
 
 	// TODO(adonovan): test on virtualized GOPATH too.
 }
+
+// gopathContainingTools returns the path of the GOPATH workspace
+// with golang.org/x/tools, or fails the test if it can't locate it.
+func gopathContainingTools(t *testing.T) string {
+	p, err := build.Import("golang.org/x/tools", "", build.FindOnly)
+	if err != nil {
+		t.Fatal(err)
+	}
+	return p.Root
+}