Skip to content

Commit 3a379b7

Browse files
committed
mod/module: allow versionless module path in CheckPath
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 <[email protected]> Change-Id: Ic1d64bf13b9afc81da5281d320e61fa704a1a947 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197629 Reviewed-by: Paul Jolly <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 56d6987 commit 3a379b7

File tree

5 files changed

+22
-27
lines changed

5 files changed

+22
-27
lines changed

cmd/cue/cmd/modinit.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"fmt"
1919
"os"
2020
"path/filepath"
21-
"strings"
2221

2322
"github.com/spf13/cobra"
2423

@@ -56,13 +55,7 @@ func runModInit(cmd *Command, args []string) (err error) {
5655
}
5756
modulePath = args[0]
5857
if err := module.CheckPath(modulePath); err != nil {
59-
// It might just be lacking a major version.
60-
if err1 := module.CheckPathWithoutVersion(modulePath); err1 != nil {
61-
if strings.Contains(modulePath, "@") {
62-
err1 = err
63-
}
64-
return fmt.Errorf("invalid module name %q: %v", modulePath, err1)
65-
}
58+
return err
6659
}
6760
}
6861

cmd/cue/cmd/testdata/script/modedit_nomajorversion.txtar

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
exec cue mod init test.example
66
cmp cue.mod/module.cue want-module
7-
8-
# TODO this should not fail.
9-
! exec cue mod edit --module other.example
10-
cmp stderr want-stderr
7+
exec cue mod edit --module other.example
8+
cmp cue.mod/module.cue want-module-2
119

1210
-- want-module --
1311
module: "test.example"
1412
language: {
1513
version: "v0.10.0"
1614
}
17-
-- want-stderr --
18-
invalid argument "other.example" for "--module" flag: malformed module path "other.example": no major version found in module path
15+
-- want-module-2 --
16+
module: "other.example"
17+
language: {
18+
version: "v0.10.0"
19+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
! exec cue mod init bad
22
cmp stderr want-stderr
33
-- want-stderr --
4-
invalid module name "bad": missing dot in first path element
4+
malformed module path "bad": missing dot in first path element
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# TODO reduce error message redundancy
2-
31
! exec cue mod init [email protected]
42
cmp stderr want-stderr
53
-- want-stderr --
6-
invalid module name "[email protected]": malformed module path "[email protected]": path can contain major version only
4+
malformed module path "[email protected]": path can contain major version only

mod/module/path.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,11 @@ func CheckPathWithoutVersion(basePath string) (err error) {
148148
// ASCII digits, dots (U+002E), and dashes (U+002D);
149149
// it must contain at least one dot and cannot start with a dash.
150150
//
151-
// Second, there must be a final major version of the form
151+
// Second, there may be a final major version of the form
152152
// @vN where N looks numeric
153153
// (ASCII digits) and must not begin with a leading zero.
154+
// Without such a major version, the major version is assumed
155+
// to be v0.
154156
//
155157
// Third, no path element may begin with a dot.
156158
func CheckPath(mpath string) (err error) {
@@ -164,18 +166,19 @@ func CheckPath(mpath string) (err error) {
164166
}()
165167

166168
basePath, vers, ok := SplitPathVersion(mpath)
167-
if !ok {
168-
return fmt.Errorf("no major version found in module path")
169-
}
170-
if semver.Major(vers) != vers {
171-
return fmt.Errorf("path can contain major version only")
169+
if ok {
170+
if semver.Major(vers) != vers {
171+
return fmt.Errorf("path can contain major version only")
172+
}
173+
if !tagPat.MatchString(vers) {
174+
return fmt.Errorf("non-conforming version %q", vers)
175+
}
176+
} else {
177+
basePath = mpath
172178
}
173179
if err := CheckPathWithoutVersion(basePath); err != nil {
174180
return err
175181
}
176-
if !tagPat.MatchString(vers) {
177-
return fmt.Errorf("non-conforming version %q", vers)
178-
}
179182
return nil
180183
}
181184

0 commit comments

Comments
 (0)