aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/buildlist.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2021-04-14 17:12:27 -0400
committerBryan C. Mills <bcmills@google.com>2021-04-21 04:23:52 +0000
commit81fcb18df5557943a80d27f248de43968e048aae (patch)
tree196796d5179a38ad4cf87b8d4917bb80a5077e80 /src/cmd/go/internal/modload/buildlist.go
parentc33ced6d8a2bb4db6896ff36cfcaac2bbdf123d1 (diff)
downloadgo-81fcb18df5557943a80d27f248de43968e048aae.tar.gz
go-81fcb18df5557943a80d27f248de43968e048aae.zip
cmd/go: make Tidy an option in PackageOpts rather than a separate call
This eliminates some awkwardly-stateful outside calls to modload.{Disallow,Allow,}WriteGoMod. Perhaps more importantly, it gives the loader the opportunity to reload packages and revise dependencies after the tidied requirements are computed. With lazy loading, dropping an irrelevant requirement from the main module's go.mod file may (rarely) cause other test dependencies for packages outside the main module to become unresolved, which may require the loader to re-resolve those dependencies, which may in turn add new roots and increase the selected versions of modules providing other packages. This refactoring allows the loader to iterate between tidying the build list and reloading packages as needed, making the exact sequencing of loading and tidying an implementation detail of the modload package. For #36460 For #40775 Change-Id: Ib6da3672f32153d5bd7d653d85e3672ab96cbe36 Reviewed-on: https://go-review.googlesource.com/c/go/+/310181 Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/buildlist.go')
-rw-r--r--src/cmd/go/internal/modload/buildlist.go21
1 files changed, 8 insertions, 13 deletions
diff --git a/src/cmd/go/internal/modload/buildlist.go b/src/cmd/go/internal/modload/buildlist.go
index ad138887a0..2eb47d2c9f 100644
--- a/src/cmd/go/internal/modload/buildlist.go
+++ b/src/cmd/go/internal/modload/buildlist.go
@@ -79,8 +79,8 @@ type cachedGraph struct {
//
// It is always non-nil if the main module's go.mod file has been loaded.
//
-// This variable should only be read from the LoadModFile function,
-// and should only be written in the writeGoMod function.
+// This variable should only be read from the loadModFile function, and should
+// only be written in the loadModFile and commitRequirements functions.
// All other functions that need or produce a *Requirements should
// accept and/or return an explicit parameter.
var requirements *Requirements
@@ -538,14 +538,9 @@ type Conflict struct {
Constraint module.Version
}
-// TidyBuildList trims the build list to the minimal requirements needed to
-// retain the same versions of all packages from the preceding call to
-// LoadPackages.
-func TidyBuildList(ctx context.Context) {
- if loaded == nil {
- panic("internal error: TidyBuildList called when no packages have been loaded")
- }
-
+// tidyBuildList trims the build list to the minimal requirements needed to
+// retain the same versions of all packages loaded by ld.
+func tidyBuildList(ctx context.Context, ld *loader, initialRS *Requirements) *Requirements {
if go117LazyTODO {
// Tidy needs to maintain the lazy-loading invariants for lazy modules.
// The implementation for eager modules should be factored out into a function.
@@ -557,7 +552,7 @@ func TidyBuildList(ctx context.Context) {
// changed after loading packages.
}
- tidy, err := updateRoots(ctx, depth, loaded.requirements.direct, loaded.pkgs, nil)
+ tidy, err := updateRoots(ctx, depth, ld.requirements.direct, ld.pkgs, nil)
if err != nil {
base.Fatalf("go: %v", err)
}
@@ -565,7 +560,7 @@ func TidyBuildList(ctx context.Context) {
if cfg.BuildV {
mg, _ := tidy.Graph(ctx)
- for _, m := range LoadModFile(ctx).rootModules {
+ for _, m := range initialRS.rootModules {
if mg.Selected(m.Path) == "none" {
fmt.Fprintf(os.Stderr, "unused %s\n", m.Path)
} else if go117LazyTODO {
@@ -575,7 +570,7 @@ func TidyBuildList(ctx context.Context) {
}
}
- commitRequirements(ctx, tidy)
+ return tidy
}
// updateRoots returns a set of root requirements that includes the selected