Skip to content

Commit

Permalink
cmd/cue: better error message for mismatched publish version
Browse files Browse the repository at this point in the history
The error messages did not make it that clear what the issue was
when there's a version mismatch between the version argument
passed to `cue mod publish` and the major version in the
module file.

Example of old error message:

    cannot form module version: mismatched major version suffix in "mod.example/blah" (version v1.0.0)

Example of new message:

   publish version "v1.0.0" does not match implied major version "v0" in "cue.mod/module.cue"; must be v0.N.N

Also improve a couple of other error messages in this area, making clear that a canonical semver
is required.

Fixes #3225

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: Id84166be9e5c97216d170a8968ec44354a56cb82
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1197924
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
rogpeppe committed Jul 19, 2024
1 parent 9a88d06 commit b513fc2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
15 changes: 15 additions & 0 deletions cmd/cue/cmd/modpublish.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ func runModUpload(cmd *Command, args []string) error {
if err != nil {
return err
}
if !semver.IsValid(args[0]) {
return fmt.Errorf("invalid publish version %q; must be valid semantic version (see http://semver.org)", args[0])
}
if semver.Canonical(args[0]) != args[0] {
return fmt.Errorf("publish version %q is not in canonical form", args[0])
}

if major := mf.MajorVersion(); semver.Major(args[0]) != major {
if _, _, ok := module.SplitPathVersion(mf.Module); ok {
return fmt.Errorf("publish version %q does not match the major version %q declared in %q; must be %s.N.N", args[0], major, modPath, major)
} else {
return fmt.Errorf("publish version %q does not match implied major version %q in %q; must be %s.N.N", args[0], major, modPath, major)
}
}

mv, err := module.NewVersion(mf.QualifiedModule(), args[0])
if err != nil {
return fmt.Errorf("cannot form module version: %v", err)
Expand Down
24 changes: 24 additions & 0 deletions cmd/cue/cmd/testdata/script/modpublish_invalid_version.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
memregistry MEMREGISTRY
env CUE_REGISTRY=$MEMREGISTRY+insecure

! exec cue mod publish 1.2.3
cmp stderr want-stderr-1

! exec cue mod publish v1.2
cmp stderr want-stderr-2

! exec cue mod publish v1.2.3+build
cmp stderr want-stderr-3

-- want-stderr-1 --
invalid publish version "1.2.3"; must be valid semantic version (see http://semver.org)
-- want-stderr-2 --
publish version "v1.2" is not in canonical form
-- want-stderr-3 --
publish version "v1.2.3+build" is not in canonical form
-- cue.mod/module.cue --
module: "test.example"
language: version: "v0.9.2"
-- main.cue --
package main

8 changes: 4 additions & 4 deletions cmd/cue/cmd/testdata/script/modpublish_no_major_suffix.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ exec cue mod publish v0.0.1

# The inferred version is v0. Using any other major version will fail.
! exec cue mod publish v1.0.0
stderr 'mismatched major version suffix'
stderr 'publish version "v1.0.0" does not match implied major version "v0" in ".*module.cue"; must be v0.N.N'
! exec cue mod publish v2.0.0
stderr 'mismatched major version suffix'
stderr 'publish version "v2.0.0" does not match implied major version "v0" in ".*module.cue"; must be v0.N.N'

# `cue mod fix` will not add add a major version suffix when missing.
exec cue mod fix
Expand All @@ -28,9 +28,9 @@ cmp cue.mod/module.cue cue.mod/module.cue.edited-with-v0

# Trying to publish with the wrong major version will fail.
! exec cue mod publish v1.0.0
stderr 'mismatched major version suffix'
stderr 'publish version "v1.0.0" does not match the major version "v0" declared in ".*module.cue"; must be v0.N.N'
! exec cue mod publish v2.0.0
stderr 'mismatched major version suffix'
stderr 'publish version "v2.0.0" does not match the major version "v0" declared in ".*module.cue"; must be v0.N.N'

# Publishing with the right major version works.
exec cue mod publish v0.0.2
Expand Down

0 comments on commit b513fc2

Please sign in to comment.