Skip to content

Commit 0bf507e

Browse files
committed
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>
1 parent 0709e58 commit 0bf507e

File tree

6 files changed

+17
-0
lines changed

6 files changed

+17
-0
lines changed

src/cmd/go/alldocs.go

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/go_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,18 @@ func TestGoListExport(t *testing.T) {
12371237
if _, err := os.Stat(file); err != nil {
12381238
t.Fatalf("cannot find .Export result %s: %v", file, err)
12391239
}
1240+
1241+
tg.run("list", "-export", "-f", "{{.BuildID}}", "strings")
1242+
buildID := strings.TrimSpace(tg.stdout.String())
1243+
if buildID == "" {
1244+
t.Fatalf(".BuildID with -export was empty")
1245+
}
1246+
1247+
tg.run("tool", "buildid", file)
1248+
toolBuildID := strings.TrimSpace(tg.stdout.String())
1249+
if buildID != toolBuildID {
1250+
t.Fatalf(".BuildID with -export %q disagrees with 'go tool buildid' %q", buildID, toolBuildID)
1251+
}
12401252
}
12411253

12421254
// Issue 4096. Validate the output of unsuccessful go install foo/quxx.

src/cmd/go/internal/list/list.go

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

src/cmd/go/internal/load/pkg.go

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

src/cmd/go/internal/work/buildid.go

+1
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ func (b *Builder) updateBuildID(a *Action, target string, rewrite bool) error {
713713
return err
714714
}
715715
a.Package.Export = c.OutputFile(outputID)
716+
a.Package.BuildID = a.buildID
716717
}
717718
}
718719
}

src/cmd/go/internal/work/exec.go

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ func (b *Builder) build(ctx context.Context, a *Action) (err error) {
433433
need &^= needBuild
434434
if b.NeedExport {
435435
p.Export = a.built
436+
p.BuildID = a.buildID
436437
}
437438
if need&needCompiledGoFiles != 0 {
438439
if err := b.loadCachedSrcFiles(a); err == nil {

0 commit comments

Comments
 (0)