aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Matloob <matloob@golang.org>2022-03-14 13:00:03 -0400
committerGopher Robot <gobot@golang.org>2022-04-20 20:25:02 +0000
commitc75befeec2a8ef2fea3c41da589ca0ffddda506f (patch)
tree24acceefa80e39ef124408c2b655ae75a7654566 /src
parent58340240576a9ad254d90da4570e5e6afb913959 (diff)
downloadgo-c75befeec2a8ef2fea3c41da589ca0ffddda506f.tar.xz
cmd/go: don't compute Deps fields if they're not needed
If the user provides the -json flag to explicitly specify fields, but doesn't specify the Deps or DepsErrors fields, skip computing the deps fields. For #29666 Change-Id: I15596c374aba1af13bdf5808d11d54abdc838667 Reviewed-on: https://go-review.googlesource.com/c/go/+/392495 Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Michael Matloob <matloob@golang.org> Auto-Submit: Michael Matloob <matloob@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/go/internal/list/list.go7
-rw-r--r--src/cmd/go/internal/load/pkg.go10
-rw-r--r--src/cmd/go/testdata/script/list_json_fields.txt5
3 files changed, 21 insertions, 1 deletions
diff --git a/src/cmd/go/internal/list/list.go b/src/cmd/go/internal/list/list.go
index 17864e1da7..e9e0910f32 100644
--- a/src/cmd/go/internal/list/list.go
+++ b/src/cmd/go/internal/list/list.go
@@ -568,6 +568,13 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
IgnoreImports: *listFind,
ModResolveTests: *listTest,
LoadVCS: true,
+ // SuppressDeps is set if the user opts to explicitly ask for the json fields they
+ // need, don't ask for Deps or DepsErrors. It's not set when using a template string,
+ // even if *listFmt doesn't contain .Deps because Deps are used to find import cycles
+ // 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"),
}
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 e43117f3d3..51bf7176d1 100644
--- a/src/cmd/go/internal/load/pkg.go
+++ b/src/cmd/go/internal/load/pkg.go
@@ -1938,7 +1938,9 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
}
}
p.Internal.Imports = imports
- p.collectDeps()
+ if !opts.SuppressDeps {
+ p.collectDeps()
+ }
if p.Error == nil && p.Name == "main" && !p.Internal.ForceLibrary && len(p.DepsErrors) == 0 {
// TODO(bcmills): loading VCS metadata can be fairly slow.
// Consider starting this as a background goroutine and retrieving the result
@@ -2679,6 +2681,12 @@ 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
+ // 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
}
// 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 58c9efa162..9b8edc6d7f 100644
--- a/src/cmd/go/testdata/script/list_json_fields.txt
+++ b/src/cmd/go/testdata/script/list_json_fields.txt
@@ -21,6 +21,11 @@ cmp stdout want-json-name.txt
go list -json=ImportPath,Name,GoFiles,Imports
cmp stdout want-json-multiple.txt
+# Test -json=<field> with Deps outputs the Deps field.
+go list -json=Deps
+stdout '"Deps": \['
+stdout '"errors",'
+
-- go.mod --
module example.com/a