go/gcexportdata: use IExportData when writing gcexportdata

Updates golang/go#28260

Change-Id: Id0ef98e1599fbef4dd67ba23c20ddb8bc4d33228
Reviewed-on: https://go-review.googlesource.com/c/tools/+/198742
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
diff --git a/go/gcexportdata/gcexportdata.go b/go/gcexportdata/gcexportdata.go
index 98b3987..f8363d8 100644
--- a/go/gcexportdata/gcexportdata.go
+++ b/go/gcexportdata/gcexportdata.go
@@ -100,7 +100,7 @@
 // Write writes encoded type information for the specified package to out.
 // The FileSet provides file position information for named objects.
 func Write(out io.Writer, fset *token.FileSet, pkg *types.Package) error {
-	b, err := gcimporter.BExportData(fset, pkg)
+	b, err := gcimporter.IExportData(fset, pkg)
 	if err != nil {
 		return err
 	}
diff --git a/go/internal/gcimporter/iimport.go b/go/internal/gcimporter/iimport.go
index a9c5733..3af95f4 100644
--- a/go/internal/gcimporter/iimport.go
+++ b/go/internal/gcimporter/iimport.go
@@ -143,12 +143,20 @@
 		p.pkgIndex[pkg] = nameIndex
 		pkgList[i] = pkg
 	}
+	if len(pkgList) == 0 {
+		errorf("no packages found for %s", path)
+		panic("unreachable")
+	}
 	var localpkg *types.Package
 	for _, pkg := range pkgList {
 		if pkg.Path() == path {
 			localpkg = pkg
+			break
 		}
 	}
+	if localpkg == nil {
+		localpkg = pkgList[0]
+	}
 
 	names := make([]string, 0, len(p.pkgIndex[localpkg]))
 	for name := range p.pkgIndex[localpkg] {