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.

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
Dispatch-Trailer: {"type":"trybot","CL":1183394,"patchset":2,"ref":"refs/changes/94/1183394/2","targetBranch":"master"}
  • Loading branch information
mvdan authored and cueckoo committed Mar 14, 2024
1 parent 0b04ed8 commit d03058a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cmd/cue/cmd/mod.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions cmd/cue/cmd/testdata/script/modinit_without_version.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d03058a

Please sign in to comment.