From aeb933d3ffa1cba614baf21849715bed4145dcd4 Mon Sep 17 00:00:00 2001 From: Michael Matloob Date: Wed, 27 Apr 2022 16:04:28 -0400 Subject: [PATCH] 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 Reviewed-by: Michael Matloob TryBot-Result: Gopher Robot Auto-Submit: Michael Matloob Reviewed-by: Bryan Mills --- src/cmd/go/internal/list/list.go | 3 ++- src/cmd/go/internal/load/pkg.go | 8 ++++++-- .../go/testdata/script/list_json_fields.txt | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/cmd/go/internal/list/list.go b/src/cmd/go/internal/list/list.go index e9e0910f32a..4d7c727048c 100644 --- a/src/cmd/go/internal/list/list.go +++ b/src/cmd/go/internal/list/list.go @@ -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 { diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 7f2ce324d24..511bdc1734f 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -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 diff --git a/src/cmd/go/testdata/script/list_json_fields.txt b/src/cmd/go/testdata/script/list_json_fields.txt index 9b8edc6d7f6..5ddbb7385e0 100644 --- a/src/cmd/go/testdata/script/list_json_fields.txt +++ b/src/cmd/go/testdata/script/list_json_fields.txt @@ -26,6 +26,18 @@ go list -json=Deps stdout '"Deps": \[' stdout '"errors",' +[!exec:git] skip + +# Test -json= 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() {}