Skip to content

Commit c269767

Browse files
committed
mod/modfile: make it an error to have an empty module path
We are relying on this behavior in the cmd/cue tests but had no test case for it in mod/modfile. There doesn't seem any good rationale for allowing an empty module path, so return an error in that case. Signed-off-by: Roger Peppe <[email protected]> Change-Id: If078c3716bc3300f1f1fc94f7931bbdd5f2c7eb1 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207984 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 24cf1f8 commit c269767

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

cmd/cue/cmd/testdata/script/modget_bad_modulepath.txtar

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
# path includes the path to the offending file (issue 3259)
33

44
! exec cue mod get example.com/foo
5-
cmp stderr want-stderr
6-
-- want-stderr --
7-
cue.mod/module.cue: invalid module path: path "" has no major version
5+
stderr 'empty module path in.*module.cue'
86
-- cue.mod/module.cue --
97
module: ""
108
language: version: "v0.9.2"

cmd/cue/cmd/testdata/script/registry_with_empty_module_path.txtar

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
exec cue export x.cue
2-
cmp stdout want-stdout
3-
-- want-stdout --
4-
{
5-
"a": 1
6-
}
1+
! exec cue export x.cue
2+
stderr 'empty module path in .*module\.cue'
73
-- cue.mod/module.cue --
84
module: ""
95
language: version: "v0.8.0"

mod/modfile/modfile.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ func parse(modfile []byte, filename string, strict bool) (*File, error) {
451451
}
452452
// There's no main module major version: default to v0.
453453
mainMajor = "v0"
454+
} else {
455+
return nil, fmt.Errorf("empty module path in %q", filename)
454456
}
455457
if mf.Language != nil {
456458
vers := mf.Language.Version
@@ -462,9 +464,10 @@ func parse(modfile []byte, filename string, strict bool) (*File, error) {
462464
}
463465
}
464466
var versions []module.Version
465-
// The main module is always the default for its own major version.
466-
defaultMajorVersions := map[string]string{
467-
mainPath: mainMajor,
467+
defaultMajorVersions := make(map[string]string)
468+
if mainPath != "" {
469+
// The main module is always the default for its own major version.
470+
defaultMajorVersions[mainPath] = mainMajor
468471
}
469472
// Check that major versions match dependency versions.
470473
for m, dep := range mf.Deps {

mod/modfile/modfile_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ _foo: "blah.example"
300300
parse: ParseLegacy,
301301
data: `module: ""`,
302302
want: &File{},
303+
}, {
304+
testName: "NonLegacyEmptyModule",
305+
parse: Parse,
306+
data: `module: "", language: version: "v0.8.0"`,
307+
wantError: `empty module path in "module.cue"`,
303308
}, {
304309
testName: "ReferencesNotAllowed#1",
305310
parse: Parse,

0 commit comments

Comments
 (0)