Skip to content

Commit

Permalink
cmd/cue: add language version in "mod init"
Browse files Browse the repository at this point in the history
We want the language version to be reflected in the module file.

Also check that `cue mod tidy` is a no-op after running `cue mod init`.

Fixes #2808.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: I31339c34f6f26175a6b04fb0431238c88d413725
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1177110
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
  • Loading branch information
rogpeppe committed Feb 20, 2024
1 parent d6ecc3d commit eb5dc6a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
21 changes: 20 additions & 1 deletion cmd/cue/cmd/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"cuelang.org/go/internal/mod/modfile"
"cuelang.org/go/internal/mod/module"
gomodule "golang.org/x/mod/module"
)

func newModCmd(c *Command) *cobra.Command {
Expand Down Expand Up @@ -125,7 +126,11 @@ func runModInit(cmd *Command, args []string) (err error) {
}
mf := &modfile.File{
Module: modulePath,
// TODO Language
}
if vers := versionForModFile(); vers != "" {
mf.Language = &modfile.Language{
Version: vers,
}
}

err = os.Mkdir(mod, 0755)
Expand Down Expand Up @@ -184,3 +189,17 @@ func backport(mod, cwd string) error {

return nil
}

func versionForModFile() string {
bi, _ := readBuildInfo()
version := cueVersion(bi)
if gomodule.IsPseudoVersion(version) {
// If we have a version like v0.7.1-0.20240130142347-7855e15cb701
// we want it to turn into the base version (v0.7.0 in that example).
// If there's no base version (e.g. v0.0.0-...) then PseudoVersionBase
// will return the empty string, which is exactly what we want
// because we don't want to put v0.0.0 in a module.cue file.
version, _ = gomodule.PseudoVersionBase(version)
}
return version
}
13 changes: 1 addition & 12 deletions cmd/cue/cmd/modtidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"path/filepath"

"github.com/spf13/cobra"
gomodule "golang.org/x/mod/module"

"cuelang.org/go/internal/mod/modload"
)
Expand Down Expand Up @@ -69,17 +68,7 @@ func runModTidy(cmd *Command, args []string) error {
if err != nil {
return err
}
bi, _ := readBuildInfo()
version := cueVersion(bi)
if gomodule.IsPseudoVersion(version) {
// If we have a version like v0.7.1-0.20240130142347-7855e15cb701
// we want it to turn into the base version (v0.7.0 in that example).
// If there's no base version (e.g. v0.0.0-...) then PseudoVersionBase
// will return the empty string, which is exactly what we want
// because we don't want to put v0.0.0 in a module.cue file.
version, _ = gomodule.PseudoVersionBase(version)
}
mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg, version)
mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg, versionForModFile())
if err != nil {
return err
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/cue/cmd/testdata/script/modinit_with_version.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Check that cue mod init adds the language version.
env CUE_VERSION_OVERRIDE=v0.1.2
exec cue mod init foo.example
cmp cue.mod/module.cue want-module

# cue mod tidy should be a no-op after cue mod init
exec cue mod tidy
cmp cue.mod/module.cue want-module

-- want-module --
module: "foo.example@v0"
language: {
version: "v0.1.2"
}
-- _registry/example.com_e_v0.0.1/cue.mod/module.cue --
module: "example.com/e@v0"
-- _registry/example.com_e_v0.0.1/main.cue --
package e

0 comments on commit eb5dc6a

Please sign in to comment.