cmd/apidiff: store package path with export data
BREAKING CHANGE: If you have old files saved with -w, they will
no longer work.
When writing export data for a package, save the path of the package.
The export data format doesn't store this information. Previously
it didn't matter, because gcexportdata.Reader.Read would return
the package that was written. But since golang.org/cl/198742, that
is no longer true.
This was apidiff's bug, not gcexportdata's: for the path argument of
Read I was passing garbage (the filename) rather than the import path.
Read's doc doesn't specify what happens if you do that.
Also, update golang.org/x/tools to latest.
Fixes golang/go#34849.
Change-Id: I9d590270a9c2bb1fcc6c9071d4a2ca82c85d84d6
Reviewed-on: https://go-review.googlesource.com/c/exp/+/200997
Reviewed-by: Jay Conrod <jayconrod@google.com>
diff --git a/cmd/apidiff/main.go b/cmd/apidiff/main.go
index d29c90f..a5446b7 100644
--- a/cmd/apidiff/main.go
+++ b/cmd/apidiff/main.go
@@ -2,6 +2,7 @@
package main
import (
+ "bufio"
"flag"
"fmt"
"go/token"
@@ -109,7 +110,14 @@
return nil, err
}
defer f.Close()
- return gcexportdata.Read(f, token.NewFileSet(), map[string]*types.Package{}, filename)
+ r := bufio.NewReader(f)
+ m := map[string]*types.Package{}
+ pkgPath, err := r.ReadString('\n')
+ if err != nil {
+ return nil, err
+ }
+ pkgPath = pkgPath[:len(pkgPath)-1] // remove delimiter
+ return gcexportdata.Read(r, token.NewFileSet(), m, pkgPath)
}
func writeExportData(pkg *packages.Package, filename string) error {
@@ -117,6 +125,9 @@
if err != nil {
return err
}
+ // Include the package path in the file. The exportdata format does
+ // not record the path of the package being written.
+ fmt.Fprintln(f, pkg.PkgPath)
err1 := gcexportdata.Write(f, pkg.Fset, pkg.Types)
err2 := f.Close()
if err1 != nil {
diff --git a/go.mod b/go.mod
index 7df64e7..796d190 100644
--- a/go.mod
+++ b/go.mod
@@ -10,5 +10,5 @@
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028
golang.org/x/mod v0.1.0
golang.org/x/sys v0.0.0-20190412213103-97732733099d
- golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e
+ golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a
)
diff --git a/go.sum b/go.sum
index 9edbeec..f4283c5 100644
--- a/go.sum
+++ b/go.sum
@@ -25,4 +25,6 @@
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e h1:1xWUkZQQ9Z9UuZgNaIR6OQOE7rUFglXUUBZlO+dGg6I=
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a h1:TwMENskLwU2NnWBzrJGEWHqSiGUkO/B4rfyhwqDxDYQ=
+golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=