Skip to content

Commit

Permalink
mod/modregistry: allow RegistryResolver to return ErrRegistryNotFound
Browse files Browse the repository at this point in the history
When using an explicit `none` registry, we don't want the error
returned by `Resolver.ResolveToRegistry` to count as a hard
error and prevent resolution of other module paths, so
define a new error type and make `modconfig.Resolver.ResolveToRegistry`
return it.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ica0b210628d337118dd11de166428ada1f33b576
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193764
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed Apr 25, 2024
1 parent c8cc462 commit 89ae7d7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
26 changes: 26 additions & 0 deletions cmd/cue/cmd/testdata/script/modtidy_nofallback.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Check that we can tidy a module when using a no-fallback
# "none" registry.

env CUE_REGISTRY=foo.com/bar=$CUE_REGISTRY1,none
exec cue mod tidy
exec cue eval .
cmp stdout want-stdout

-- want-stdout --
x: "hello"
-- cue.mod/module.cue --
module: "main.org@v0"
language: version: "v0.8.0"
-- main.cue --
package main
import "foo.com/bar/baz@v0"

baz

-- _registry1/foo.com_bar_v0.0.1/cue.mod/module.cue --
module: "foo.com/bar@v0"
language: version: "v0.8.0"

-- _registry1/foo.com_bar_v0.0.1/baz/baz.cue --
package baz
x: "hello"
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/registry_nofallback.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ env CUE_REGISTRY=foo.com=$CUE_REGISTRY1,none
cmp stderr expect-stderr

-- expect-stderr --
import failed: cannot find package "example.com@v0": cannot fetch example.com@v0.0.1: cannot resolve example.com (version v0.0.1) to registry:
import failed: cannot find package "example.com@v0": cannot fetch example.com@v0.0.1: module not found:
./main.cue:2:8
-- cue.mod/module.cue --
module: "main.org@v0"
Expand Down
4 changes: 2 additions & 2 deletions mod/modconfig/modconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (r *Resolver) ResolveToLocation(mpath string, version string) (HostLocation
return r.resolver.ResolveToLocation(mpath, version)
}

// Resolve implements modregistry.Resolver.Resolve.
// ResolveToRegistry implements [modregistry.Resolver.ResolveToRegistry].
func (r *Resolver) ResolveToRegistry(mpath string, version string) (modregistry.RegistryLocation, error) {
loc, ok := r.resolver.ResolveToLocation(mpath, version)
if !ok {
Expand All @@ -162,7 +162,7 @@ func (r *Resolver) ResolveToRegistry(mpath string, version string) (modregistry.
//
// It can also happen when the user has explicitly configured a "none"
// registry to avoid falling back to a default registry.
return modregistry.RegistryLocation{}, fmt.Errorf("cannot resolve %s (version %s) to registry", mpath, version)
return modregistry.RegistryLocation{}, fmt.Errorf("cannot resolve %s (version %q) to registry: %w", mpath, version, modregistry.ErrRegistryNotFound)
}
r.mu.Lock()
defer r.mu.Unlock()
Expand Down
17 changes: 17 additions & 0 deletions mod/modregistry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ type Resolver interface {
// will hold the prefix that all versions of the module in its
// repository have. That prefix will be followed by the version
// itself.
//
// If there is no registry configured for the module, it returns
// an [ErrRegistryNotFound] error.
ResolveToRegistry(mpath, vers string) (RegistryLocation, error)
}

// ErrRegistryNotFound is returned by [Resolver.ResolveToRegistry]
// when there is no registry configured for a module.
var ErrRegistryNotFound = fmt.Errorf("no registry configured for module")

// RegistryLocation holds a registry and a location within it
// that a specific module (or set of versions for a module)
// will be stored.
Expand Down Expand Up @@ -104,6 +111,9 @@ func NewClientWithResolver(resolver Resolver) *Client {
func (c *Client) GetModule(ctx context.Context, m module.Version) (*Module, error) {
loc, err := c.resolve(m)
if err != nil {
if errors.Is(err, ErrRegistryNotFound) {
return nil, ErrNotFound
}
return nil, err
}
rd, err := loc.Registry.GetTag(ctx, loc.Repository, loc.Tag)
Expand All @@ -130,6 +140,10 @@ func (c *Client) GetModule(ctx context.Context, m module.Version) (*Module, erro
func (c *Client) GetModuleWithManifest(ctx context.Context, m module.Version, contents []byte, mediaType string) (*Module, error) {
loc, err := c.resolve(m)
if err != nil {
// Note: don't return [ErrNotFound] here because if we've got the
// manifest we should be pretty sure that the module actually
// exists, so it's a harder error than if we're getting the module
// by tag.
return nil, err
}

Expand Down Expand Up @@ -168,6 +182,9 @@ func (c *Client) ModuleVersions(ctx context.Context, m string) ([]string, error)
}
loc, err := c.resolver.ResolveToRegistry(mpath, "")
if err != nil {
if errors.Is(err, ErrRegistryNotFound) {
return nil, nil
}
return nil, err
}
versions := []string{}
Expand Down

0 comments on commit 89ae7d7

Please sign in to comment.