Skip to content

Commit

Permalink
cmd/cue: mod init should only add @v0 with the experiment
Browse files Browse the repository at this point in the history
Otherwise, existing users of `cue mod init` who haven't opted into
CUE_EXPERIMENT=modules would get different and likely broken behavior,
made worse by the known breakage in #2945.

We add test cases with and without the experiment for both
a module path argument with and without a major version, for clarity.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I32afc0e669fd5bfd5634ec382111d2e2846dcd9e
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1183394
Reviewed-by: Paul Jolly <paul@myitcv.io>
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
mvdan committed Mar 14, 2024
1 parent 77741ff commit 24f93c9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd/cue/cmd/mod.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions 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"
}
15 changes: 15 additions & 0 deletions 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"
Expand Down

0 comments on commit 24f93c9

Please sign in to comment.