]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modload: avoid using the global build list in QueryPattern
authorBryan C. Mills <bcmills@google.com>
Mon, 12 Oct 2020 19:32:49 +0000 (15:32 -0400)
committerBryan C. Mills <bcmills@google.com>
Fri, 16 Oct 2020 19:13:40 +0000 (19:13 +0000)
The Query function allows the caller to specify the current version of
the requested module, but the QueryPattern function is missing that
parameter: instead, it always assumes that the current version is the
one selected from the global build list.

This change removes that assumption, instead adding a callback
function to determine the current version. (The callback is currently
invoked once per candidate module, regardless of whether that module
exists, but in a future change we can refactor it to invoke the
callback only when needed.)

For #36460
For #40775

Change-Id: I001a4a8ab24f5b4fcc66a670d9bd305b47e948ba
Reviewed-on: https://go-review.googlesource.com/c/go/+/261640
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>
src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modload/buildlist.go
src/cmd/go/internal/modload/import.go
src/cmd/go/internal/modload/modfile.go
src/cmd/go/internal/modload/query.go
src/cmd/go/internal/work/build.go

index 9e2fb8e408817dc254790b4f04c9df6c09f14e66..171c070ab342df2357e2fca2fb446bc51f2f0c79 100644 (file)
@@ -912,7 +912,7 @@ func getQuery(ctx context.Context, path, vers string, prevM module.Version, forc
        // If it turns out to only exist as a module, we can detect the resulting
        // PackageNotInModuleError and avoid a second round-trip through (potentially)
        // all of the configured proxies.
-       results, err := modload.QueryPattern(ctx, path, vers, allowed)
+       results, err := modload.QueryPattern(ctx, path, vers, modload.Selected, allowed)
        if err != nil {
                // If the path doesn't contain a wildcard, check whether it was actually a
                // module path instead. If so, return that.
index 059b0204206d5db47d3b37dc47b0484a0f3dc6c2..95a68637c665cad6d02257d0bac66667e92a05b2 100644 (file)
@@ -52,6 +52,21 @@ func LoadedModules() []module.Version {
        return buildList
 }
 
+// Selected returns the selected version of the module with the given path, or
+// the empty string if the given module has no selected version
+// (either because it is not required or because it is the Target module).
+func Selected(path string) (version string) {
+       if path == Target.Path {
+               return ""
+       }
+       for _, m := range buildList {
+               if m.Path == path {
+                       return m.Version
+               }
+       }
+       return ""
+}
+
 // SetBuildList sets the module build list.
 // The caller is responsible for ensuring that the list is valid.
 // SetBuildList does not retain a reference to the original list.
index 1c572d5d6debf60bc2cf687afabe13b1ac449136..6d0d8de94459403ac1e18910c4b65a8611f2dbb0 100644 (file)
@@ -345,7 +345,7 @@ func queryImport(ctx context.Context, path string) (module.Version, error) {
        // and return m, dir, ImpportMissingError.
        fmt.Fprintf(os.Stderr, "go: finding module for package %s\n", path)
 
-       candidates, err := QueryPattern(ctx, path, "latest", CheckAllowed)
+       candidates, err := QueryPattern(ctx, path, "latest", Selected, CheckAllowed)
        if err != nil {
                if errors.Is(err, os.ErrNotExist) {
                        // Return "cannot find module providing package […]" instead of whatever
index d15da892e6db48ba7c4b30c53cc25e5d6be49fc4..006db4f169ebe5308b9dd89c6ed19dfd5b2bb7bb 100644 (file)
@@ -116,7 +116,7 @@ func checkRetractions(ctx context.Context, m module.Version) error {
                // Ignore exclusions from the main module's go.mod.
                // We may need to account for the current version: for example,
                // v2.0.0+incompatible is not "latest" if v1.0.0 is current.
-               rev, err := Query(ctx, path, "latest", findCurrentVersion(path), nil)
+               rev, err := Query(ctx, path, "latest", Selected(path), nil)
                if err != nil {
                        return &entry{nil, err}
                }
index 6a3fd103fcdea03918dd39d75ca056612700fae3..d16a247f727e518f09140eaa70e2b15b3e4bc572 100644 (file)
@@ -516,7 +516,7 @@ type QueryResult struct {
 // If any matching package is in the main module, QueryPattern considers only
 // the main module and only the version "latest", without checking for other
 // possible modules.
-func QueryPattern(ctx context.Context, pattern, query string, allowed AllowedFunc) ([]QueryResult, error) {
+func QueryPattern(ctx context.Context, pattern, query string, current func(string) string, allowed AllowedFunc) ([]QueryResult, error) {
        ctx, span := trace.StartSpan(ctx, "modload.QueryPattern "+pattern+" "+query)
        defer span.Done()
 
@@ -591,9 +591,9 @@ func QueryPattern(ctx context.Context, pattern, query string, allowed AllowedFun
                        ctx, span := trace.StartSpan(ctx, "modload.QueryPattern.queryModule ["+proxy+"] "+path)
                        defer span.Done()
 
-                       current := findCurrentVersion(path)
+                       pathCurrent := current(path)
                        r.Mod.Path = path
-                       r.Rev, err = queryProxy(ctx, proxy, path, query, current, allowed)
+                       r.Rev, err = queryProxy(ctx, proxy, path, query, pathCurrent, allowed)
                        if err != nil {
                                return r, err
                        }
@@ -649,15 +649,6 @@ func modulePrefixesExcludingTarget(path string) []string {
        return prefixes
 }
 
-func findCurrentVersion(path string) string {
-       for _, m := range buildList {
-               if m.Path == path {
-                       return m.Version
-               }
-       }
-       return ""
-}
-
 type prefixResult struct {
        QueryResult
        err error
index 21342ac8ba00c1184194340bad05a9ae157c85f5..3531612dc6115850df336f09183f2023532403ac 100644 (file)
@@ -741,7 +741,7 @@ func installOutsideModule(ctx context.Context, args []string) {
                // Don't check for retractions if a specific revision is requested.
                allowed = nil
        }
-       qrs, err := modload.QueryPattern(ctx, patterns[0], version, allowed)
+       qrs, err := modload.QueryPattern(ctx, patterns[0], version, modload.Selected, allowed)
        if err != nil {
                base.Fatalf("go install %s: %v", args[0], err)
        }