aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modload/modfile.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2020-07-23 00:45:27 -0400
committerBryan C. Mills <bcmills@google.com>2020-08-24 20:45:05 +0000
commita9146a49d0db666a7efd5f5d4555cf6117405cf5 (patch)
treef4767ced3fd89434280a93cbc26b15d17d4a1e5b /src/cmd/go/internal/modload/modfile.go
parent2a9636dc2bdbb2865dde686352de528c6953c7bf (diff)
downloadgo-a9146a49d0db666a7efd5f5d4555cf6117405cf5.tar.gz
go-a9146a49d0db666a7efd5f5d4555cf6117405cf5.zip
cmd/go/internal/modload: cache parsed go.mod files globally
Previously they were cached per mvsReqs instance. However, the contents of the go.mod file of a given dependency version can only vary if the 'replace' directives that apply to that version have changed, and the only time we change 'replace' directives is in 'go mod edit' (which does not care about the build list or MVS). This not only simplifies the mvsReqs implementation, but also makes more of the underlying logic independent of mvsReqs. For #36460 Change-Id: Ieac20c2fcd56f64d847ac8a1b40f9361ece78663 Reviewed-on: https://go-review.googlesource.com/c/go/+/244774 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/modfile.go')
-rw-r--r--src/cmd/go/internal/modload/modfile.go171
1 files changed, 171 insertions, 0 deletions
diff --git a/src/cmd/go/internal/modload/modfile.go b/src/cmd/go/internal/modload/modfile.go
index 9ff00e9b5c..c04e2add13 100644
--- a/src/cmd/go/internal/modload/modfile.go
+++ b/src/cmd/go/internal/modload/modfile.go
@@ -7,10 +7,17 @@ package modload
import (
"cmd/go/internal/base"
"cmd/go/internal/cfg"
+ "cmd/go/internal/lockedfile"
+ "cmd/go/internal/modfetch"
+ "cmd/go/internal/par"
+ "errors"
+ "fmt"
+ "path/filepath"
"sync"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
+ "golang.org/x/mod/semver"
)
var modFile *modfile.File
@@ -171,3 +178,167 @@ func (i *modFileIndex) modFileIsDirty(modFile *modfile.File) bool {
// If a module is replaced, the version of the replacement is keyed by the
// replacement module.Version, not the version being replaced.
var rawGoVersion sync.Map // map[module.Version]string
+
+// A modFileSummary is a summary of a go.mod file for which we do not need to
+// retain complete information — for example, the go.mod file of a dependency
+// module.
+type modFileSummary struct {
+ module module.Version
+ goVersionV string // GoVersion with "v" prefix
+ require []module.Version
+}
+
+// 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.
+//
+// goModSummary cannot be used on the Target module, as its requirements
+// may change.
+//
+// The caller must not modify the returned summary.
+func goModSummary(m module.Version) (*modFileSummary, error) {
+ if m == Target {
+ panic("internal error: goModSummary called on the Target module")
+ }
+
+ type cached struct {
+ summary *modFileSummary
+ err error
+ }
+ c := goModSummaryCache.Do(m, func() interface{} {
+ if cfg.BuildMod == "vendor" {
+ summary := &modFileSummary{
+ module: module.Version{Path: m.Path},
+ }
+ if vendorVersion[m.Path] != m.Version {
+ // This module is not vendored, so packages cannot be loaded from it and
+ // it cannot be relevant to the build.
+ return cached{summary, nil}
+ }
+
+ // For every module other than the target,
+ // return the full list of modules from modules.txt.
+ readVendorList()
+
+ // TODO(#36876): Load the "go" version from vendor/modules.txt and store it
+ // in rawGoVersion with the appropriate key.
+
+ // We don't know what versions the vendored module actually relies on,
+ // so assume that it requires everything.
+ summary.require = vendorList
+ return cached{summary, nil}
+ }
+
+ actual := Replacement(m)
+ if actual.Path == "" {
+ actual = m
+ }
+ summary, err := rawGoModSummary(actual)
+ if err != nil {
+ return cached{nil, err}
+ }
+
+ if actual.Version == "" {
+ // The actual module is a filesystem-local replacement, for which we have
+ // unfortunately not enforced any sort of invariants about module lines or
+ // matching module paths. Anything goes.
+ //
+ // TODO(bcmills): Remove this special-case, update tests, and add a
+ // release note.
+ } else {
+ if summary.module.Path == "" {
+ return cached{nil, module.VersionError(actual, errors.New("parsing go.mod: missing module line"))}
+ }
+
+ // In theory we should only allow mpath to be unequal to mod.Path here if the
+ // version that we fetched lacks an explicit go.mod file: if the go.mod file
+ // is explicit, then it should match exactly (to ensure that imports of other
+ // packages within the module are interpreted correctly). Unfortunately, we
+ // can't determine that information from the module proxy protocol: we'll have
+ // to leave that validation for when we load actual packages from within the
+ // module.
+ if mpath := summary.module.Path; mpath != m.Path && mpath != actual.Path {
+ return cached{nil, module.VersionError(actual, fmt.Errorf(`parsing go.mod:
+ module declares its path as: %s
+ but was required as: %s`, mpath, m.Path))}
+ }
+ }
+
+ if index != nil && len(index.exclude) > 0 {
+ // Drop any requirements on excluded versions.
+ nonExcluded := summary.require[:0]
+ for _, r := range summary.require {
+ if !index.exclude[r] {
+ nonExcluded = append(nonExcluded, r)
+ }
+ }
+ summary.require = nonExcluded
+ }
+ return cached{summary, nil}
+ }).(cached)
+
+ return c.summary, c.err
+}
+
+var goModSummaryCache par.Cache // module.Version → goModSummary result
+
+// rawGoModSummary returns a new summary of the go.mod file for module m,
+// ignoring all replacements that may apply to m and excludes that may apply to
+// its dependencies.
+//
+// rawGoModSummary cannot be used on the Target module.
+func rawGoModSummary(m module.Version) (*modFileSummary, error) {
+ if m == Target {
+ panic("internal error: rawGoModSummary called on the Target module")
+ }
+
+ summary := new(modFileSummary)
+ var f *modfile.File
+ if m.Version == "" {
+ // m is a replacement module with only a file path.
+ dir := m.Path
+ if !filepath.IsAbs(dir) {
+ dir = filepath.Join(ModRoot(), dir)
+ }
+ gomod := filepath.Join(dir, "go.mod")
+
+ data, err := lockedfile.Read(gomod)
+ if err != nil {
+ return nil, module.VersionError(m, fmt.Errorf("reading %s: %v", base.ShortPath(gomod), err))
+ }
+ f, err = modfile.ParseLax(gomod, data, nil)
+ if err != nil {
+ return nil, module.VersionError(m, fmt.Errorf("parsing %s: %v", base.ShortPath(gomod), err))
+ }
+ } else {
+ if !semver.IsValid(m.Version) {
+ // Disallow the broader queries supported by fetch.Lookup.
+ base.Fatalf("go: internal error: %s@%s: unexpected invalid semantic version", m.Path, m.Version)
+ }
+
+ data, err := modfetch.GoMod(m.Path, m.Version)
+ if err != nil {
+ return nil, err
+ }
+ f, err = modfile.ParseLax("go.mod", data, nil)
+ if err != nil {
+ return nil, module.VersionError(m, fmt.Errorf("parsing go.mod: %v", err))
+ }
+ }
+
+ if f.Module != nil {
+ summary.module = f.Module.Mod
+ }
+ if f.Go != nil && f.Go.Version != "" {
+ rawGoVersion.LoadOrStore(m, f.Go.Version)
+ summary.goVersionV = "v" + f.Go.Version
+ }
+ if len(f.Require) > 0 {
+ summary.require = make([]module.Version, 0, len(f.Require))
+ for _, req := range f.Require {
+ summary.require = append(summary.require, req.Mod)
+ }
+ }
+
+ return summary, nil
+}