aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/modfile.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2021-04-07 14:10:46 -0400
committerBryan C. Mills <bcmills@google.com>2021-04-08 19:30:34 +0000
commitecca94a7d1fb4f00101af9831fcb395ed08b6948 (patch)
tree3f6eaa6eabf6a0f024f852ec10fdd479767e8a6c /src/cmd/go/internal/modload/modfile.go
parent96a67450887314cedbd9e8634ad92b76a0443779 (diff)
downloadgo-ecca94a7d1fb4f00101af9831fcb395ed08b6948.tar.gz
go-ecca94a7d1fb4f00101af9831fcb395ed08b6948.zip
cmd/go/internal/modload: add a dormant depth type
This change adds the depth constants 'lazy' and 'eager', but leaves the go117EnableLazyLoading constant set to false so that the depth in effect is still always 'eager'. The go117EnableLazyLoading constant can be toggled to true once the module loader has been updated to maintain the lazy-loading invariants in the go.mod file. In the meantime, this will allow me to progressively replace uses of go117LazyTODO with real conditions and locally toggle lazy-mode on to see which tests are still failing (or which behaviors are missing test coverage). For #36460 Change-Id: Ifd358265a3903a5000003c2072f28171f336e15c Reviewed-on: https://go-review.googlesource.com/c/go/+/308515 Trust: Bryan C. Mills <bcmills@google.com> Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com> Reviewed-by: Michael Matloob <matloob@golang.org>
Diffstat (limited to 'src/cmd/go/internal/modload/modfile.go')
-rw-r--r--src/cmd/go/internal/modload/modfile.go64
1 files changed, 56 insertions, 8 deletions
diff --git a/src/cmd/go/internal/modload/modfile.go b/src/cmd/go/internal/modload/modfile.go
index 3e4772f217..3b01afa13f 100644
--- a/src/cmd/go/internal/modload/modfile.go
+++ b/src/cmd/go/internal/modload/modfile.go
@@ -25,15 +25,29 @@ import (
"golang.org/x/mod/semver"
)
-// narrowAllVersionV is the Go version (plus leading "v") at which the
-// module-module "all" pattern no longer closes over the dependencies of
-// tests outside of the main module.
-const narrowAllVersionV = "v1.16"
+const (
+ // narrowAllVersionV is the Go version (plus leading "v") at which the
+ // module-module "all" pattern no longer closes over the dependencies of
+ // tests outside of the main module.
+ narrowAllVersionV = "v1.16"
+
+ // lazyLoadingVersionV is the Go version (plus leading "v") at which a
+ // module's go.mod file is expected to list explicit requirements on every
+ // module that provides any package transitively imported by that module.
+ lazyLoadingVersionV = "v1.17"
+)
+
+const (
+ // go117EnableLazyLoading toggles whether lazy-loading code paths should be
+ // active. It will be removed once the lazy loading implementation is stable
+ // and well-tested.
+ go117EnableLazyLoading = false
-// go1117LazyTODO is a constant that exists only until lazy loading is
-// implemented. Its use indicates a condition that will need to change if the
-// main module is lazy.
-const go117LazyTODO = false
+ // go1117LazyTODO is a constant that exists only until lazy loading is
+ // implemented. Its use indicates a condition that will need to change if the
+ // main module is lazy.
+ go117LazyTODO = false
+)
var modFile *modfile.File
@@ -57,6 +71,14 @@ type requireMeta struct {
indirect bool
}
+// A modDepth indicates which dependencies should be loaded for a go.mod file.
+type modDepth uint8
+
+const (
+ lazy modDepth = iota // load dependencies only as needed
+ eager // load all transitive dependencies eagerly
+)
+
// CheckAllowed returns an error equivalent to ErrDisallowed if m is excluded by
// the main module's go.mod or retracted by its author. Most version queries use
// this to filter out versions that should not be used.
@@ -300,6 +322,18 @@ func (i *modFileIndex) allPatternClosesOverTests() bool {
return false
}
+// depth reports the modDepth indicated by the indexed go.mod file,
+// or lazy if the go.mod file has not been indexed.
+func (i *modFileIndex) depth() modDepth {
+ if !go117EnableLazyLoading {
+ return eager
+ }
+ if i != nil && semver.Compare(i.goVersionV, lazyLoadingVersionV) < 0 {
+ return eager
+ }
+ return lazy
+}
+
// modFileIsDirty reports whether the go.mod file differs meaningfully
// from what was indexed.
// If modFile has been changed (even cosmetically) since it was first read,
@@ -394,6 +428,20 @@ type retraction struct {
Rationale string
}
+func (s *modFileSummary) depth() modDepth {
+ if !go117EnableLazyLoading {
+ return eager
+ }
+ // The 'go' command fills in the 'go' directive automatically, so an empty
+ // goVersionV in a dependency implies either Go 1.11 (eager loading) or no
+ // explicit go.mod file at all (no difference between eager and lazy because
+ // the module doesn't specify any requirements at all).
+ if s.goVersionV == "" || semver.Compare(s.goVersionV, lazyLoadingVersionV) < 0 {
+ return eager
+ }
+ return lazy
+}
+
// goModSummary returns a summary of the go.mod file for module m,
// taking into account any replacements for m, exclusions of its dependencies,
// and/or vendoring.