]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: skip computing BuildInfo in go list unless it's needed
authorMichael Matloob <matloob@golang.org>
Wed, 27 Apr 2022 20:04:28 +0000 (16:04 -0400)
committerGopher Robot <gobot@golang.org>
Wed, 4 May 2022 21:35:22 +0000 (21:35 +0000)
The only fields of the go list output that require BuildInfo to be
computed are the Stale and StaleReason fields. If a user explicitly
requests JSON fields and does not ask for Stale or StaleReason, skip
the computation of BuildInfo.

For #29666

Change-Id: Ie77581c44babedcb5cb7f3dc7d6ed1078b56eee4
Reviewed-on: https://go-review.googlesource.com/c/go/+/402736
Run-TryBot: Michael Matloob <matloob@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/cmd/go/internal/list/list.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/testdata/script/list_json_fields.txt

index e9e0910f32a50dccecf269e8412920285bfbee7d..4d7c727048c76b72546468bd308ebfba91538a9a 100644 (file)
@@ -574,7 +574,8 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
                // for test variants of packages and users who have been providing format strings
                // might not expect those errors to stop showing up.
                // See issue #52443.
-               SuppressDeps: !listJsonFields.needAny("Deps", "DepsErrors"),
+               SuppressDeps:      !listJsonFields.needAny("Deps", "DepsErrors"),
+               SuppressBuildInfo: !listJsonFields.needAny("Stale", "StaleReason"),
        }
        pkgs := load.PackagesAndErrors(ctx, pkgOpts, args)
        if !*listE {
index 7f2ce324d2447a8469fe1bb3fb0ad08c7e80e4a9..511bdc1734f5d2a34c17058d60ffc74f06da7337 100644 (file)
@@ -1947,7 +1947,7 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
        if !opts.SuppressDeps {
                p.collectDeps()
        }
-       if p.Error == nil && p.Name == "main" && !p.Internal.ForceLibrary && len(p.DepsErrors) == 0 {
+       if p.Error == nil && p.Name == "main" && !p.Internal.ForceLibrary && len(p.DepsErrors) == 0 && !opts.SuppressBuildInfo {
                // TODO(bcmills): loading VCS metadata can be fairly slow.
                // Consider starting this as a background goroutine and retrieving the result
                // asynchronously when we're actually ready to build the package, or when we
@@ -2688,11 +2688,15 @@ type PackageOpts struct {
        // LoadVCS controls whether we also load version-control metadata for main packages.
        LoadVCS bool
 
-       // NeedDepsFields is true if the caller does not need Deps and DepsErrors to be populated
+       // SuppressDeps is true if the caller does not need Deps and DepsErrors to be populated
        // on the package. TestPackagesAndErrors examines the  Deps field to determine if the test
        // variant has an import cycle, so SuppressDeps should not be set if TestPackagesAndErrors
        // will be called on the package.
        SuppressDeps bool
+
+       // SuppressBuildInfo is true if the caller does not need p.Stale, p.StaleReason, or p.Internal.BuildInfo
+       // to be populated on the package.
+       SuppressBuildInfo bool
 }
 
 // PackagesAndErrors returns the packages named by the command line arguments
index 9b8edc6d7f6029e8297eb315c732cdb9337b3ba8..5ddbb7385e0d9793875d787ff4e9bce80710e917 100644 (file)
@@ -26,6 +26,18 @@ go list -json=Deps
 stdout '"Deps": \['
 stdout '"errors",'
 
+[!exec:git] skip
+
+# Test -json=<field> without Stale skips computing buildinfo
+cd repo
+exec git init
+# Control case: with -json=Stale cmd/go executes git status to compute buildinfo
+go list -json=Stale -x
+stderr 'git status'
+# Test case: without -json=Stale cmd/go skips git status
+go list -json=Name -x
+! stderr 'git status'
+
 -- go.mod --
 module example.com/a
 
@@ -55,3 +67,9 @@ example.com/a
                "fmt"
        ]
 }
+-- repo/go.mod --
+module example.com/repo
+-- repo/main.go --
+package main
+
+func main() {}