Skip to content

Commit

Permalink
mod/modregistry: return empty list for Versions with invalid name
Browse files Browse the repository at this point in the history
This fixes the module logic so that when there's a request
for a module that has a name that isn't OCI-compatible,
it will not even make the request to the module,
instead returning a "not found" error.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ie144e72181c05fa2f8d74a48210eb49ca9157b44
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199212
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Aug 13, 2024
1 parent 9a9dd17 commit f00a023
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
13 changes: 4 additions & 9 deletions cmd/cue/cmd/testdata/script/modtidy_badpath.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
# might not return a 404 error, but some other kind of error.
# The OCI proxy is one such registry.

# TODO fix this so it works OK.
! exec cue mod tidy
cmp stderr want-stderr
exec cue mod tidy

# TODO enable these assertions.
# cmp cue.mod/module.cue want-module
# exec cue export
# cmp stdout want-stdout
cmp cue.mod/module.cue want-module
exec cue export
cmp stdout want-stdout

-- want-stderr --
failed to resolve "test.example/foo/BarBaz": module test.example/foo/BarBaz: invalid OCI request: name invalid: invalid repository name
-- want-module --
module: "cue.example@v0"
language: {
Expand Down
2 changes: 1 addition & 1 deletion internal/mod/modload/testdata/tidy/badimportpath.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- tidy-check-error --
module is not tidy: missing dependency providing package x.com/Foo--bar@v0
-- want --
error: failed to resolve "x.com/Foo--bar@v0": cannot obtain versions for module "x.com/Foo--bar@v0": module x.com/Foo--bar@v0: invalid OCI request: name invalid: invalid repository name
error: failed to resolve "x.com/Foo--bar@v0": cannot find module providing package x.com/Foo--bar@v0
-- cue.mod/module.cue --
language: version: "v0.8.0"
module: "main.org@v0"
Expand Down
9 changes: 8 additions & 1 deletion mod/modregistry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"

"cuelabs.dev/go/oci/ociregistry"
"cuelabs.dev/go/oci/ociregistry/ociref"
"cuelang.org/go/internal/mod/semver"
digest "github.com/opencontainers/go-digest"
specs "github.com/opencontainers/image-spec/specs-go"
Expand Down Expand Up @@ -173,7 +174,7 @@ func (c *Client) GetModuleWithManifest(m module.Version, contents []byte, mediaT
// sorted in semver order.
// If m has a major version suffix, only versions with that major version will
// be returned.
func (c *Client) ModuleVersions(ctx context.Context, m string) ([]string, error) {
func (c *Client) ModuleVersions(ctx context.Context, m string) (_req []string, _err0 error) {
mpath, major, hasMajor := module.SplitPathVersion(m)
if !hasMajor {
mpath = m
Expand All @@ -186,6 +187,12 @@ func (c *Client) ModuleVersions(ctx context.Context, m string) ([]string, error)
return nil, err
}
versions := []string{}
if !ociref.IsValidRepository(loc.Repository) {
// If it's not a valid repository, it can't be used in an OCI
// request, so return an empty slice rather than the
// "invalid OCI request" error that a registry can return.
return nil, nil
}
// Note: do not use c.repoName because that always expects
// a module path with a major version.
iter := loc.Registry.Tags(ctx, loc.Repository, "")
Expand Down

0 comments on commit f00a023

Please sign in to comment.