cmd/gobind: fix build-tag, CGO and load trybot failures

* Correctly format build tags to pass into go/packages
* Removes CGO_ENABLED=0 from a packages.Load configuration
* Calls go/packages.Load twice to work around a build cache
* staleness issue

These bugs were introduced by CL 189597.

Updates golang/go#27234.
Updates golang/go#33687.

Change-Id: I3ae6737bf53bbecda0c7e25885b9c6aea5779332
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/190479
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
diff --git a/bind/java/seq_test.go b/bind/java/seq_test.go
index fc1575c..12242aa 100644
--- a/bind/java/seq_test.go
+++ b/bind/java/seq_test.go
@@ -46,12 +46,11 @@
 		if out, err := exec.Command(gocmd, "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
 			log.Fatalf("gobind build failed: %v: %s", err, out)
 		}
-		PATH := os.Getenv("PATH")
-		if PATH != "" {
-			PATH += string(filepath.ListSeparator)
+		path := binDir
+		if oldPath := os.Getenv("PATH"); oldPath != "" {
+			path += string(filepath.ListSeparator) + oldPath
 		}
-		PATH += binDir
-		os.Setenv("PATH", PATH)
+		os.Setenv("PATH", path)
 	}
 	return m.Run()
 }
diff --git a/bind/objc/seq_test.go b/bind/objc/seq_test.go
index 5d88914..8d247d8 100644
--- a/bind/objc/seq_test.go
+++ b/bind/objc/seq_test.go
@@ -9,9 +9,11 @@
 	"fmt"
 	"io"
 	"io/ioutil"
+	"log"
 	"os"
 	"os/exec"
 	"path/filepath"
+	"runtime"
 	"strings"
 	"testing"
 )
@@ -34,6 +36,43 @@
 
 var destination = flag.String("device", "platform=iOS Simulator,name=iPhone 6s Plus", "Specify the -destination flag to xcodebuild")
 
+var gomobileBin string
+
+func TestMain(m *testing.M) {
+	os.Exit(testMain(m))
+}
+
+func testMain(m *testing.M) int {
+	binDir, err := ioutil.TempDir("", "bind-objc-test-")
+	if err != nil {
+		log.Fatal(err)
+	}
+	defer os.RemoveAll(binDir)
+
+	exe := ""
+	if runtime.GOOS == "windows" {
+		exe = ".exe"
+	}
+	if runtime.GOOS != "android" {
+		gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
+		gomobileBin = filepath.Join(binDir, "gomobile"+exe)
+		gobindBin := filepath.Join(binDir, "gobind"+exe)
+		if out, err := exec.Command(gocmd, "build", "-o", gomobileBin, "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil {
+			log.Fatalf("gomobile build failed: %v: %s", err, out)
+		}
+		if out, err := exec.Command(gocmd, "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
+			log.Fatalf("gobind build failed: %v: %s", err, out)
+		}
+		path := binDir
+		if oldPath := os.Getenv("PATH"); oldPath != "" {
+			path += string(filepath.ListSeparator) + oldPath
+		}
+		os.Setenv("PATH", path)
+	}
+
+	return m.Run()
+}
+
 // TestObjcSeqTest runs ObjC test SeqTest.m.
 func TestObjcSeqTest(t *testing.T) {
 	runTest(t, []string{
@@ -62,15 +101,12 @@
 }
 
 func runTest(t *testing.T, pkgNames []string, prefix, testfile, framework string, uitest, dumpOutput bool) {
+	if gomobileBin == "" {
+		t.Skipf("no gomobile on %s", runtime.GOOS)
+	}
 	if _, err := run("which xcodebuild"); err != nil {
 		t.Skip("command xcodebuild not found, skipping")
 	}
-	if _, err := run("which gomobile"); err != nil {
-		t.Log("go install gomobile")
-		if _, err := run("go install golang.org/x/mobile/cmd/gomobile"); err != nil {
-			t.Fatalf("gomobile install failed: %v", err)
-		}
-	}
 
 	tmpdir, err := ioutil.TempDir("", "bind-objc-seq-test-")
 	if err != nil {
@@ -87,7 +123,7 @@
 		t.Fatalf("failed to copy %s: %v", testfile, err)
 	}
 
-	cmd := exec.Command("gomobile", "bind", "-target", "ios", "-tags", "aaa bbb")
+	cmd := exec.Command(gomobileBin, "bind", "-target", "ios", "-tags", "aaa bbb")
 	if prefix != "" {
 		cmd.Args = append(cmd.Args, "-prefix", prefix)
 	}
diff --git a/cmd/gobind/main.go b/cmd/gobind/main.go
index eec3c37..2546f37 100644
--- a/cmd/gobind/main.go
+++ b/cmd/gobind/main.go
@@ -60,16 +60,16 @@
 	}
 
 	cfg := &packages.Config{
-		Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles |
+		Mode: packages.NeedName | packages.NeedFiles |
 			packages.NeedImports | packages.NeedDeps |
 			packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo,
-		BuildFlags: []string{"-tags", *tags},
-
-		// packages.Load invokes `go list` command with `GOOS=android`, but in most cases
-		// go-list cannot find the header files for Android. Suppress this error by
-		// disabling Cgo.
-		Env: append(os.Environ(), "CGO_ENABLED=0"),
+		BuildFlags: []string{"-tags", strings.Join(strings.Split(*tags, ","), " ")},
 	}
+
+	// Call Load twice to warm the cache. There is a known issue that the result of Load
+	// depends on build cache state. See golang/go#33687.
+	packages.Load(cfg, flag.Args()...)
+
 	allPkg, err := packages.Load(cfg, flag.Args()...)
 	if err != nil {
 		log.Fatal(err)
diff --git a/go.mod b/go.mod
index a1a0c61..fc62116 100644
--- a/go.mod
+++ b/go.mod
@@ -5,5 +5,5 @@
 require (
 	golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56
 	golang.org/x/image v0.0.0-20190802002840-cff245a6509b
-	golang.org/x/tools v0.0.0-20190808195139-e713427fea3f
+	golang.org/x/tools v0.0.0-20190816200558-6889da9d5479
 )
diff --git a/go.sum b/go.sum
index 597a296..6e1919b 100644
--- a/go.sum
+++ b/go.sum
@@ -17,6 +17,6 @@
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
-golang.org/x/tools v0.0.0-20190808195139-e713427fea3f h1:lSQQYboXWc71s9tnZRRBiMcc9Uc1BPWj3Bzvdk8UQ0Y=
-golang.org/x/tools v0.0.0-20190808195139-e713427fea3f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20190816200558-6889da9d5479 h1:lfN2PY/jymfnxkNHlbBF5DwPsUvhqUnrdgfK01iH2s0=
+golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
diff --git a/internal/importers/ast.go b/internal/importers/ast.go
index fb80014..b9e36ee 100644
--- a/internal/importers/ast.go
+++ b/internal/importers/ast.go
@@ -106,7 +106,7 @@
 	fset := token.NewFileSet()
 	for _, pkg := range pkgs {
 		files := make(map[string]*ast.File)
-		for i, name := range pkg.CompiledGoFiles {
+		for i, name := range pkg.GoFiles {
 			files[name] = pkg.Syntax[i]
 		}
 		// Ignore errors (from unknown packages)