Skip to content

Commit

Permalink
cmd/go: skip computing BuildInfo in go list unless it's needed
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
matloob authored and gopherbot committed May 4, 2022
1 parent 2f23364 commit aeb933d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/cmd/go/internal/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 6 additions & 2 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/cmd/go/testdata/script/list_json_fields.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -55,3 +67,9 @@ example.com/a
"fmt"
]
}
-- repo/go.mod --
module example.com/repo
-- repo/main.go --
package main

func main() {}

0 comments on commit aeb933d

Please sign in to comment.