Skip to content

Commit

Permalink
mod/module: allow versionless module path in CheckPath
Browse files Browse the repository at this point in the history
In general, a canonical module path can omit the major
version (implying `@v0`), but this was not reflected
in the `module.CheckPath` function.

Changing this behavior fixes `cue mod edit` to allow
such module paths. It also changes some error messages,
as reflected in the tests.

Fixes #3262.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Ic1d64bf13b9afc81da5281d320e61fa704a1a947
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197629
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
rogpeppe committed Jul 12, 2024
1 parent 56d6987 commit 3a379b7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 27 deletions.
9 changes: 1 addition & 8 deletions cmd/cue/cmd/modinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -56,13 +55,7 @@ func runModInit(cmd *Command, args []string) (err error) {
}
modulePath = args[0]
if err := module.CheckPath(modulePath); err != nil {
// It might just be lacking a major version.
if err1 := module.CheckPathWithoutVersion(modulePath); err1 != nil {
if strings.Contains(modulePath, "@") {
err1 = err
}
return fmt.Errorf("invalid module name %q: %v", modulePath, err1)
}
return err
}
}

Expand Down
13 changes: 7 additions & 6 deletions cmd/cue/cmd/testdata/script/modedit_nomajorversion.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

exec cue mod init test.example
cmp cue.mod/module.cue want-module

# TODO this should not fail.
! exec cue mod edit --module other.example
cmp stderr want-stderr
exec cue mod edit --module other.example
cmp cue.mod/module.cue want-module-2

-- want-module --
module: "test.example"
language: {
version: "v0.10.0"
}
-- want-stderr --
invalid argument "other.example" for "--module" flag: malformed module path "other.example": no major version found in module path
-- want-module-2 --
module: "other.example"
language: {
version: "v0.10.0"
}
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/modinit_badmodule.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
! exec cue mod init bad
cmp stderr want-stderr
-- want-stderr --
invalid module name "bad": missing dot in first path element
malformed module path "bad": missing dot in first path element
4 changes: 1 addition & 3 deletions cmd/cue/cmd/testdata/script/modinit_badmoduleversion.txtar
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# TODO reduce error message redundancy

! exec cue mod init bad.com@v2.2
cmp stderr want-stderr
-- want-stderr --
invalid module name "bad.com@v2.2": malformed module path "bad.com@v2.2": path can contain major version only
malformed module path "bad.com@v2.2": path can contain major version only
21 changes: 12 additions & 9 deletions mod/module/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ func CheckPathWithoutVersion(basePath string) (err error) {
// ASCII digits, dots (U+002E), and dashes (U+002D);
// it must contain at least one dot and cannot start with a dash.
//
// Second, there must be a final major version of the form
// Second, there may be a final major version of the form
// @vN where N looks numeric
// (ASCII digits) and must not begin with a leading zero.
// Without such a major version, the major version is assumed
// to be v0.
//
// Third, no path element may begin with a dot.
func CheckPath(mpath string) (err error) {
Expand All @@ -164,18 +166,19 @@ func CheckPath(mpath string) (err error) {
}()

basePath, vers, ok := SplitPathVersion(mpath)
if !ok {
return fmt.Errorf("no major version found in module path")
}
if semver.Major(vers) != vers {
return fmt.Errorf("path can contain major version only")
if ok {
if semver.Major(vers) != vers {
return fmt.Errorf("path can contain major version only")
}
if !tagPat.MatchString(vers) {
return fmt.Errorf("non-conforming version %q", vers)
}
} else {
basePath = mpath
}
if err := CheckPathWithoutVersion(basePath); err != nil {
return err
}
if !tagPat.MatchString(vers) {
return fmt.Errorf("non-conforming version %q", vers)
}
return nil
}

Expand Down

0 comments on commit 3a379b7

Please sign in to comment.