diff options
author | Bryan C. Mills <bcmills@google.com> | 2020-09-15 15:14:55 -0400 |
---|---|---|
committer | Bryan C. Mills <bcmills@google.com> | 2020-09-17 21:05:27 +0000 |
commit | 7a095c32366593f637a95bc927c63454125e3015 (patch) | |
tree | 1a71ee0f17ad4b236307e8d4c6c0b7e1bd383d5d /src/cmd/go/internal/modload/query.go | |
parent | 9a702fd427645e4bcd42a68f9676bc1ab2adb6e4 (diff) | |
download | go-7a095c32366593f637a95bc927c63454125e3015.tar.gz go-7a095c32366593f637a95bc927c63454125e3015.zip |
cmd/go/internal/modload: avoid a network fetch when querying a valid semantic version
Test this behavior incidentally in a test for ambiguous import errors.
(I rediscovered the error when writing the new test.)
For #32567
Updates #28806
Change-Id: I323f05145734e5cf99818b9f04d65075f7c0f787
Reviewed-on: https://go-review.googlesource.com/c/go/+/255046
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/query.go')
-rw-r--r-- | src/cmd/go/internal/modload/query.go | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/cmd/go/internal/modload/query.go b/src/cmd/go/internal/modload/query.go index f67a738677..5ddb4e6565 100644 --- a/src/cmd/go/internal/modload/query.go +++ b/src/cmd/go/internal/modload/query.go @@ -212,7 +212,20 @@ func queryProxy(ctx context.Context, proxy, path, query, current string, allowed default: // Direct lookup of semantic version or commit identifier. - // + + // If the query is a valid semantic version and that version is replaced, + // use the replacement module without searching the proxy. + canonicalQuery := module.CanonicalVersion(query) + if canonicalQuery != "" { + m := module.Version{Path: path, Version: query} + if r := Replacement(m); r.Path != "" { + if err := allowed(ctx, m); errors.Is(err, ErrDisallowed) { + return nil, err + } + return &modfetch.RevInfo{Version: query}, nil + } + } + // If the identifier is not a canonical semver tag — including if it's a // semver tag with a +metadata suffix — then modfetch.Stat will populate // info.Version with a suitable pseudo-version. @@ -222,9 +235,9 @@ func queryProxy(ctx context.Context, proxy, path, query, current string, allowed // The full query doesn't correspond to a tag. If it is a semantic version // with a +metadata suffix, see if there is a tag without that suffix: // semantic versioning defines them to be equivalent. - if vers := module.CanonicalVersion(query); vers != "" && vers != query { - info, err = modfetch.Stat(proxy, path, vers) - if !errors.Is(err, os.ErrNotExist) { + if canonicalQuery != "" && query != canonicalQuery { + info, err = modfetch.Stat(proxy, path, canonicalQuery) + if err != nil && !errors.Is(err, os.ErrNotExist) { return info, err } } |