diff --git a/cmd/cue/cmd/mod.go b/cmd/cue/cmd/mod.go index 49544ecc4..93645f46b 100644 --- a/cmd/cue/cmd/mod.go +++ b/cmd/cue/cmd/mod.go @@ -23,6 +23,7 @@ import ( "github.com/spf13/cobra" + "cuelang.org/go/internal/cueexperiment" "cuelang.org/go/mod/modfile" "cuelang.org/go/mod/module" gomodule "golang.org/x/mod/module" @@ -99,8 +100,10 @@ func runModInit(cmd *Command, args []string) (err error) { } return fmt.Errorf("invalid module name %q: %v", modulePath, err1) } - // Default major version to v0. - modulePath += "@v0" + // Default major version to v0 if the modules experiment is enabled. + if cueexperiment.Flags.Modules { + modulePath += "@v0" + } } } diff --git a/cmd/cue/cmd/testdata/script/modinit_majorversion.txtar b/cmd/cue/cmd/testdata/script/modinit_majorversion.txtar index 0e8d79bf2..8e2759198 100644 --- a/cmd/cue/cmd/testdata/script/modinit_majorversion.txtar +++ b/cmd/cue/cmd/testdata/script/modinit_majorversion.txtar @@ -1,11 +1,28 @@ env CUE_VERSION_OVERRIDE=v0.1.2 + +# Without the experiment, the major version is allowed, +# even though it's not particularly useful. +# It feels unnecessary to ask the user to re-run with the experiment. exec cue mod init foo.com/bar@v1 cmp cue.mod/module.cue want-module.cue exists cue.mod/usr exists cue.mod/pkg +# With the experiment, it works as expected. +env CUE_EXPERIMENT=modules +rm cue.mod +exec cue mod init foo.com/bar@v1 +cmp cue.mod/module.cue want-module-experiment.cue +exists cue.mod/usr +exists cue.mod/pkg + -- want-module.cue -- module: "foo.com/bar@v1" language: { version: "v0.1.2" } +-- want-module-experiment.cue -- +module: "foo.com/bar@v1" +language: { + version: "v0.1.2" +} diff --git a/cmd/cue/cmd/testdata/script/modinit_nomajorversion.txtar b/cmd/cue/cmd/testdata/script/modinit_nomajorversion.txtar index d370988dd..25c459743 100644 --- a/cmd/cue/cmd/testdata/script/modinit_nomajorversion.txtar +++ b/cmd/cue/cmd/testdata/script/modinit_nomajorversion.txtar @@ -1,10 +1,25 @@ env CUE_VERSION_OVERRIDE=v0.1.2 + +# Without the experiment, we use the module path as-is. exec cue mod init foo.com/bar cmp cue.mod/module.cue want-module.cue exists cue.mod/usr exists cue.mod/pkg +# With the experiment, we add the major version v0 by default. +env CUE_EXPERIMENT=modules +rm cue.mod +exec cue mod init foo.com/bar +cmp cue.mod/module.cue want-module-experiment.cue +exists cue.mod/usr +exists cue.mod/pkg + -- want-module.cue -- +module: "foo.com/bar" +language: { + version: "v0.1.2" +} +-- want-module-experiment.cue -- module: "foo.com/bar@v0" language: { version: "v0.1.2" diff --git a/cmd/cue/cmd/testdata/script/modinit_without_version.txtar b/cmd/cue/cmd/testdata/script/modinit_without_version.txtar index 228f15736..36fe508e2 100644 --- a/cmd/cue/cmd/testdata/script/modinit_without_version.txtar +++ b/cmd/cue/cmd/testdata/script/modinit_without_version.txtar @@ -7,6 +7,7 @@ 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 +env CUE_CACHE_DIR=$WORK/.tmp/cache exec cue mod tidy cmp cue.mod/module.cue want-module