aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/init.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/init.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/init.go')
-rw-r--r--src/cmd/go/internal/modload/init.go36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index 35bbcc795e..953419a718 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -365,8 +365,8 @@ var errGoModDirty error = goModDirtyError{}
// build list from its go.mod file.
//
// LoadModFile may make changes in memory, like adding a go directive and
-// ensuring requirements are consistent. WriteGoMod should be called later to
-// write changes out to disk or report errors in readonly mode.
+// ensuring requirements are consistent, and will write those changes back to
+// disk unless DisallowWriteGoMod is in effect.
//
// As a side-effect, LoadModFile may change cfg.BuildMod to "vendor" if
// -mod wasn't set explicitly and automatic vendoring should be enabled.
@@ -379,8 +379,22 @@ var errGoModDirty error = goModDirtyError{}
// it for global consistency. Most callers outside of the modload package should
// use LoadModGraph instead.
func LoadModFile(ctx context.Context) *Requirements {
+ rs, needCommit := loadModFile(ctx)
+ if needCommit {
+ commitRequirements(ctx, rs)
+ }
+ return rs
+}
+
+// loadModFile is like LoadModFile, but does not implicitly commit the
+// requirements back to disk after fixing inconsistencies.
+//
+// If needCommit is true, after the caller makes any other needed changes to the
+// returned requirements they should invoke commitRequirements to fix any
+// inconsistencies that may be present in the on-disk go.mod file.
+func loadModFile(ctx context.Context) (rs *Requirements, needCommit bool) {
if requirements != nil {
- return requirements
+ return requirements, false
}
Init()
@@ -388,8 +402,8 @@ func LoadModFile(ctx context.Context) *Requirements {
Target = module.Version{Path: "command-line-arguments"}
targetPrefix = "command-line-arguments"
rawGoVersion.Store(Target, latestGoVersion())
- commitRequirements(ctx, newRequirements(index.depth(), nil, nil))
- return requirements
+ requirements = newRequirements(index.depth(), nil, nil)
+ return requirements, false
}
gomod := ModFilePath()
@@ -418,7 +432,7 @@ func LoadModFile(ctx context.Context) *Requirements {
}
setDefaultBuildMod() // possibly enable automatic vendoring
- rs := requirementsFromModFile(ctx, f)
+ rs = requirementsFromModFile(ctx, f)
if cfg.BuildMod == "vendor" {
readVendorList()
@@ -450,9 +464,8 @@ func LoadModFile(ctx context.Context) *Requirements {
}
}
- // Fix up roots if inconsistent.
- commitRequirements(ctx, rs)
- return requirements
+ requirements = rs
+ return requirements, true
}
// CreateModFile initializes a new module by creating a go.mod file.
@@ -1136,8 +1149,3 @@ const (
func modkey(m module.Version) module.Version {
return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
}
-
-func TrimGoSum(ctx context.Context) {
- rs := LoadModFile(ctx)
- modfetch.TrimGoSum(keepSums(ctx, loaded, rs, loadedZipSumsOnly))
-}