cmd/go: add BuildID to list -json -export

That is, the following two pieces of shell code are now equivalent:

	$ go tool buildid $(go list -export -f {{.Export}} strings)
	v_0VqA6yzwuMg2dn4u57/PXcIR2Pb8Mi9yRdcdkwe

	$ go list -export -f {{.BuildID}} strings
	v_0VqA6yzwuMg2dn4u57/PXcIR2Pb8Mi9yRdcdkwe

This does not expose any information that wasn't available before, but
makes this workflow simpler and faster. In the first example, we have to
execute two programs, and 'go tool buildid' has to re-open the export
data file to read the build ID.

With the new mechanism, 'go list -export' already has the build ID
ready, so we can simply print it out. Moreover, when listing lots of
related packages like './...', we can now obtain all their build IDs at
once.

Fixes #37281.

Change-Id: I8e2f65a08391b3df1a628c6e06e708b8c8cb7865
Reviewed-on: https://go-review.googlesource.com/c/go/+/263542
Trust: Daniel Martí <mvdan@mvdan.cc>
Trust: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Daniel Martí 2020-10-19 15:06:02 +01:00
parent 0709e58bee
commit 0bf507efe9
6 changed files with 17 additions and 0 deletions

View File

@ -796,6 +796,7 @@
// BinaryOnly bool // binary-only package (no longer supported)
// ForTest string // package is only for use in named test
// Export string // file containing export data (when using -export)
// BuildID string // build ID of the export data (when using -export)
// Module *Module // info about package's containing module, if any (can be nil)
// Match []string // command-line patterns matching this package
// DepOnly bool // package is only a dependency, not explicitly listed

View File

@ -1237,6 +1237,18 @@ func TestGoListExport(t *testing.T) {
if _, err := os.Stat(file); err != nil {
t.Fatalf("cannot find .Export result %s: %v", file, err)
}
tg.run("list", "-export", "-f", "{{.BuildID}}", "strings")
buildID := strings.TrimSpace(tg.stdout.String())
if buildID == "" {
t.Fatalf(".BuildID with -export was empty")
}
tg.run("tool", "buildid", file)
toolBuildID := strings.TrimSpace(tg.stdout.String())
if buildID != toolBuildID {
t.Fatalf(".BuildID with -export %q disagrees with 'go tool buildid' %q", buildID, toolBuildID)
}
}
// Issue 4096. Validate the output of unsuccessful go install foo/quxx.

View File

@ -66,6 +66,7 @@ to -f '{{.ImportPath}}'. The struct being passed to the template is:
BinaryOnly bool // binary-only package (no longer supported)
ForTest string // package is only for use in named test
Export string // file containing export data (when using -export)
BuildID string // build ID of the export data (when using -export)
Module *Module // info about package's containing module, if any (can be nil)
Match []string // command-line patterns matching this package
DepOnly bool // package is only a dependency, not explicitly listed

View File

@ -60,6 +60,7 @@ type PackagePublic struct {
ConflictDir string `json:",omitempty"` // Dir is hidden by this other directory
ForTest string `json:",omitempty"` // package is only for use in named test
Export string `json:",omitempty"` // file containing export data (set by go list -export)
BuildID string `json:",omitempty"` // build ID of the export data (set by go list -export)
Module *modinfo.ModulePublic `json:",omitempty"` // info about package's module, if any
Match []string `json:",omitempty"` // command-line patterns matching this package
Goroot bool `json:",omitempty"` // is this package found in the Go root?

View File

@ -713,6 +713,7 @@ func (b *Builder) updateBuildID(a *Action, target string, rewrite bool) error {
return err
}
a.Package.Export = c.OutputFile(outputID)
a.Package.BuildID = a.buildID
}
}
}

View File

@ -433,6 +433,7 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
need &^= needBuild
if b.NeedExport {
p.Export = a.built
p.BuildID = a.buildID
}
if need&needCompiledGoFiles != 0 {
if err := b.loadCachedSrcFiles(a); err == nil {