diff options
author | Jay Conrod <jayconrod@google.com> | 2020-04-15 12:08:24 -0400 |
---|---|---|
committer | Jay Conrod <jayconrod@google.com> | 2020-08-26 21:12:19 +0000 |
commit | db821b54d1a8dffa85a9a3cf599f83a19184f020 (patch) | |
tree | 96b289a0bf8c4bfe583a8ca155309748b517c021 /src/cmd/go/internal/modload/modfile.go | |
parent | bf869c65d1a96da5db78a891430ea3acd7ddf1ab (diff) | |
download | go-db821b54d1a8dffa85a9a3cf599f83a19184f020.tar.gz go-db821b54d1a8dffa85a9a3cf599f83a19184f020.zip |
cmd/go/internal/modload: refactor version filtering for exclude
Query and other functions now accept an "allowed" function that
returns an error (previously, the function returned a bool). If the
error is equivalent to ErrDisallowed, it indicates the version is
excluded (or, in a future CL, retracted). This provides predicates a
chance to explain why a version is not allowed.
When a query refers to a specific revision (by version, branch, tag,
or commit name), most callers will not use the Allowed predicate. This
allows commands like 'go list -m' and 'go mod download' to handle
disallowed versions when explicitly requested. 'go get' will reject
excluded versions though.
When a query does not refer to a specific revision (for example,
"latest"), disallowed versions will not be considered.
When an "allowed" predicate returns an error not equivalent to
ErrDisallowed, it may be ignored or returned, depending on the
case. This never happens for excluded versions, but it may happen for
retractions (in a future CL). This indicates a list of retractions
could not be loaded. This frequently happens when offline, and it
shouldn't cause a fatal or warning in most cases.
For #24031
Change-Id: I4df6fb6bd60e3e0259e5b3b4bf71a307b4b32298
Reviewed-on: https://go-review.googlesource.com/c/go/+/228379
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/modfile.go')
-rw-r--r-- | src/cmd/go/internal/modload/modfile.go | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/src/cmd/go/internal/modload/modfile.go b/src/cmd/go/internal/modload/modfile.go index c04e2add13..aed1f0a36b 100644 --- a/src/cmd/go/internal/modload/modfile.go +++ b/src/cmd/go/internal/modload/modfile.go @@ -5,15 +5,17 @@ package modload import ( + "context" + "errors" + "fmt" + "path/filepath" + "sync" + "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" @@ -41,11 +43,33 @@ type requireMeta struct { indirect bool } -// Allowed reports whether module m is allowed (not excluded) by the main module's go.mod. -func Allowed(m module.Version) bool { - return index == nil || !index.exclude[m] +// CheckAllowed returns an error equivalent to ErrDisallowed if m is excluded by +// the main module's go.mod. Most version queries use this to filter out +// versions that should not be used. +func CheckAllowed(ctx context.Context, m module.Version) error { + return CheckExclusions(ctx, m) +} + +// ErrDisallowed is returned by version predicates passed to Query and similar +// functions to indicate that a version should not be considered. +var ErrDisallowed = errors.New("disallowed module version") + +// CheckExclusions returns an error equivalent to ErrDisallowed if module m is +// excluded by the main module's go.mod file. +func CheckExclusions(ctx context.Context, m module.Version) error { + if index != nil && index.exclude[m] { + return module.VersionError(m, errExcluded) + } + return nil } +var errExcluded = &excludedError{} + +type excludedError struct{} + +func (e *excludedError) Error() string { return "excluded by go.mod" } +func (e *excludedError) Is(err error) bool { return err == ErrDisallowed } + // Replacement returns the replacement for mod, if any, from go.mod. // If there is no replacement for mod, Replacement returns // a module.Version with Path == "". |