Skip to content

Commit c75befe

Browse files
matloobgopherbot
authored andcommitted
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>
1 parent 5834024 commit c75befe

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/cmd/go/internal/list/list.go

+7
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,13 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
568568
IgnoreImports: *listFind,
569569
ModResolveTests: *listTest,
570570
LoadVCS: true,
571+
// SuppressDeps is set if the user opts to explicitly ask for the json fields they
572+
// need, don't ask for Deps or DepsErrors. It's not set when using a template string,
573+
// even if *listFmt doesn't contain .Deps because Deps are used to find import cycles
574+
// for test variants of packages and users who have been providing format strings
575+
// might not expect those errors to stop showing up.
576+
// See issue #52443.
577+
SuppressDeps: !listJsonFields.needAny("Deps", "DepsErrors"),
571578
}
572579
pkgs := load.PackagesAndErrors(ctx, pkgOpts, args)
573580
if !*listE {

src/cmd/go/internal/load/pkg.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,9 @@ func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *
19381938
}
19391939
}
19401940
p.Internal.Imports = imports
1941-
p.collectDeps()
1941+
if !opts.SuppressDeps {
1942+
p.collectDeps()
1943+
}
19421944
if p.Error == nil && p.Name == "main" && !p.Internal.ForceLibrary && len(p.DepsErrors) == 0 {
19431945
// TODO(bcmills): loading VCS metadata can be fairly slow.
19441946
// Consider starting this as a background goroutine and retrieving the result
@@ -2679,6 +2681,12 @@ type PackageOpts struct {
26792681

26802682
// LoadVCS controls whether we also load version-control metadata for main packages.
26812683
LoadVCS bool
2684+
2685+
// NeedDepsFields is true if the caller does not need Deps and DepsErrors to be populated
2686+
// on the package. TestPackagesAndErrors examines the Deps field to determine if the test
2687+
// variant has an import cycle, so SuppressDeps should not be set if TestPackagesAndErrors
2688+
// will be called on the package.
2689+
SuppressDeps bool
26822690
}
26832691

26842692
// PackagesAndErrors returns the packages named by the command line arguments

src/cmd/go/testdata/script/list_json_fields.txt

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ cmp stdout want-json-name.txt
2121
go list -json=ImportPath,Name,GoFiles,Imports
2222
cmp stdout want-json-multiple.txt
2323

24+
# Test -json=<field> with Deps outputs the Deps field.
25+
go list -json=Deps
26+
stdout '"Deps": \['
27+
stdout '"errors",'
28+
2429
-- go.mod --
2530
module example.com/a
2631

0 commit comments

Comments
 (0)