]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: add BuildID to list -json -export
authorDaniel Martí <mvdan@mvdan.cc>
Mon, 19 Oct 2020 14:06:02 +0000 (15:06 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 20 Oct 2020 15:17:15 +0000 (15:17 +0000)
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>
src/cmd/go/alldocs.go
src/cmd/go/go_test.go
src/cmd/go/internal/list/list.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/work/buildid.go
src/cmd/go/internal/work/exec.go

index ebd786d4e2f66874b50c73722914d98a00fa38e5..0827f0c609b798285af3f57a23d0a817efd4098f 100644 (file)
 //         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
index 2c11d169596ba8dfcfb697700887f4f2b4fbc5d7..e9c26d161a978b53ed6a6bf0e2fe0d291e251940 100644 (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.
index 732cebc8cb9323f65c62cb6669a503a1d9ddf7b1..9fd9d7446dfb5c0772f74e748dca5ead083133fd 100644 (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
index 066ff6c98150aa3f41a3a18b1883e0da9a7e3858..913b3b94d76920fc95e2b07435c3711da4e1fe30 100644 (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?
index a3c9b1a2c1cfa295b96e3053535c05a1c51751ff..5cd3124e54aa3a455fb89dde40022ad226dfa74e 100644 (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
                        }
                }
        }
index 717b0cc3afece71bda0c27469dbece5b8fa12f7d..3ffdca571826923ea6f6ac8510f1d636ffe1537e 100644 (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 {