1
0
mirror of https://github.com/golang/go synced 2024-11-17 07:54:41 -07:00

cmd/go: skip computing BuildInfo in go list unless it's needed

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>
This commit is contained in:
Michael Matloob 2022-04-27 16:04:28 -04:00 committed by Gopher Robot
parent 2f23364340
commit aeb933d3ff
3 changed files with 26 additions and 3 deletions

View File

@ -575,6 +575,7 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
// might not expect those errors to stop showing up.
// See issue #52443.
SuppressDeps: !listJsonFields.needAny("Deps", "DepsErrors"),
SuppressBuildInfo: !listJsonFields.needAny("Stale", "StaleReason"),
}
pkgs := load.PackagesAndErrors(ctx, pkgOpts, args)
if !*listE {

View 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

View 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() {}