Skip to content

Commit 2f90f54

Browse files
committed
internal/cueversion: split language and module versions
This means that the CUE logic will not predicate language semantics on a relatively volatile build-info-derived version. Instead the semantics are predicated on a simple compiled-in string and the original logic is used for version information that's displayed as part of the `cue version` command and forms part of the User-Agent HTTP header. We also fix the module version logic so that it works when CUE is used as a dependency as well as a main module. Note that writing a local regression test for this is hard, as dependency modules only have versions as provided by a proxy, and deps replaced by a directory don't have module versions. Fixes #3061 Signed-off-by: Roger Peppe <[email protected]> Change-Id: I55bc931590974a5a50b0d82d78cba0a64efbd0f6 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194044 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent d696e44 commit 2f90f54

15 files changed

+88
-91
lines changed

cmd/cue/cmd/modinit.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"golang.org/x/mod/semver"
2626

2727
"cuelang.org/go/internal/cueexperiment"
28+
"cuelang.org/go/internal/cueversion"
2829
"cuelang.org/go/mod/modfile"
2930
"cuelang.org/go/mod/module"
3031
)
@@ -95,14 +96,8 @@ func runModInit(cmd *Command, args []string) (err error) {
9596
return err
9697
}
9798
}
98-
vers := versionForModFile()
99-
if vers == "" {
100-
// Shouldn't happen because we should use the
101-
// fallback version if we can't the version otherwise.
102-
return fmt.Errorf("cannot determine language version for module")
103-
}
10499
mf.Language = &modfile.Language{
105-
Version: vers,
100+
Version: cueversion.LanguageVersion(),
106101
}
107102

108103
err = os.Mkdir(mod, 0755)
@@ -129,7 +124,7 @@ func runModInit(cmd *Command, args []string) (err error) {
129124
}
130125

131126
func versionForModFile() string {
132-
version := cueVersion()
127+
version := cueversion.LanguageVersion()
133128
earliestPossibleVersion := modfile.EarliestClosedSchemaVersion()
134129
if semver.Compare(version, earliestPossibleVersion) < 0 {
135130
// The reported version is earlier than it should be,

cmd/cue/cmd/modtidy.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/spf13/cobra"
2424

25+
"cuelang.org/go/internal/cueversion"
2526
"cuelang.org/go/internal/mod/modload"
2627
)
2728

@@ -71,7 +72,7 @@ func runModTidy(cmd *Command, args []string) error {
7172
if flagCheck.Bool(cmd) {
7273
return modload.CheckTidy(ctx, os.DirFS(modRoot), ".", reg)
7374
}
74-
mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg, versionForModFile())
75+
mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg, cueversion.LanguageVersion())
7576
if err != nil {
7677
return err
7778
}

cmd/cue/cmd/script_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"cuelang.org/go/cue/errors"
4747
"cuelang.org/go/cue/parser"
4848
"cuelang.org/go/internal/cuetest"
49+
"cuelang.org/go/internal/cueversion"
4950
"cuelang.org/go/internal/registrytest"
5051
)
5152

@@ -232,6 +233,7 @@ func TestScript(t *testing.T) {
232233
e.Vars = append(e.Vars,
233234
"GOPROXY="+srv.URL,
234235
"GONOSUMDB=*", // GOPROXY is a private proxy
236+
"CUE_LANGUAGE_VERSION="+cueversion.LanguageVersion(),
235237
)
236238
entries, err := os.ReadDir(e.WorkDir)
237239
if err != nil {

cmd/cue/cmd/testdata/script/modinit_majorversion.txtar

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
env CUE_VERSION_OVERRIDE=v0.8.100
1+
env-fill want-module.cue
2+
env-fill want-module-experiment.cue
23

34
# Without the experiment, the major version is allowed,
45
# even though it's not particularly useful.
@@ -19,10 +20,10 @@ exists cue.mod/pkg
1920
-- want-module.cue --
2021
module: "foo.com/bar@v1"
2122
language: {
22-
version: "v0.8.100"
23+
version: "$CUE_LANGUAGE_VERSION"
2324
}
2425
-- want-module-experiment.cue --
2526
module: "foo.com/bar@v1"
2627
language: {
27-
version: "v0.8.100"
28+
version: "$CUE_LANGUAGE_VERSION"
2829
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# TODO we might want to deprecate or remove the ability to create
22
# module file with an empty module directive.
3-
env CUE_VERSION_OVERRIDE=v0.8.100
43
exec cue mod init
4+
env-fill want-module.cue
55
cmp cue.mod/module.cue want-module.cue
66
exists cue.mod/usr
77
exists cue.mod/pkg
88

99
-- want-module.cue --
1010
module: ""
1111
language: {
12-
version: "v0.8.100"
12+
version: "$CUE_LANGUAGE_VERSION"
1313
}

cmd/cue/cmd/testdata/script/modinit_nomajorversion.txtar

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
env CUE_VERSION_OVERRIDE=v0.8.0
1+
env-fill want-module.cue
2+
env-fill want-module-experiment.cue
23

34
# Without the experiment, we use the module path as-is.
45
exec cue mod init foo.com/bar
@@ -17,10 +18,10 @@ exists cue.mod/pkg
1718
-- want-module.cue --
1819
module: "foo.com/bar"
1920
language: {
20-
version: "v0.8.0"
21+
version: "$CUE_LANGUAGE_VERSION"
2122
}
2223
-- want-module-experiment.cue --
2324
module: "foo.com/bar@v0"
2425
language: {
25-
version: "v0.8.0"
26+
version: "$CUE_LANGUAGE_VERSION"
2627
}

cmd/cue/cmd/testdata/script/modinit_with_pseudoversion.txtar

Lines changed: 0 additions & 28 deletions
This file was deleted.

cmd/cue/cmd/testdata/script/modinit_without_version.txtar

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
# Check that cue mod init uses the latest schema
2-
# version when it lacks any version information at all.
3-
# A zero pseudo-version is one such case, as there are no semver numbers.
1+
# Check that cue mod init is independent of the module version.
42
env CUE_EXPERIMENT=modules
53
env CUE_VERSION_OVERRIDE=v0.0.0-00010101000000-000000000000
4+
env-fill want-module
65
exec cue mod init foo.example
76
cmp cue.mod/module.cue want-module
87

@@ -15,5 +14,5 @@ cmp cue.mod/module.cue want-module
1514
-- want-module --
1615
module: "foo.example@v0"
1716
language: {
18-
version: "v0.9.0-alpha.0"
17+
version: "$CUE_LANGUAGE_VERSION"
1918
}

cmd/cue/cmd/testdata/script/modinit_withsource.txtar

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Test that we can use the --source flag to cue mod init.
22

3-
env CUE_VERSION_OVERRIDE=v0.9.0-alpha.2
3+
env-fill $WORK/want-module.cue-0
4+
env-fill $WORK/want-module.cue-1
45

56
mkdir $WORK/test0
67
cd $WORK/test0
@@ -20,15 +21,15 @@ cmp stderr $WORK/want-stderr
2021
-- want-module.cue-0 --
2122
module: "test.example"
2223
language: {
23-
version: "v0.9.0-alpha.2"
24+
version: "$CUE_LANGUAGE_VERSION"
2425
}
2526
source: {
2627
kind: "self"
2728
}
2829
-- want-module.cue-1 --
2930
module: "test.example"
3031
language: {
31-
version: "v0.9.0-alpha.2"
32+
version: "$CUE_LANGUAGE_VERSION"
3233
}
3334
source: {
3435
kind: "git"

cmd/cue/cmd/testdata/script/modtidy_logging.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ env CUE_DEBUG=http
33
# Smoke test that the logging looks reasonable. The actual log messages
44
# are tested more fully in the httplog package itself.
55
exec cue mod tidy
6-
stderr '{"time":".*","level":"INFO","msg":"http client->","info":{"id":[0-9]+,"method":"GET","url":"http://[^/]+/v2/example.com/tags/list\?n=\d+","contentLength":0,"header":{"User-Agent":\["Cue/.* \(cmd/cue\) Go/[^ ]+ \(.*\)"\]}}}'
6+
stderr '{"time":".*","level":"INFO","msg":"http client->","info":{"id":[0-9]+,"method":"GET","url":"http://[^/]+/v2/example.com/tags/list\?n=\d+","contentLength":0,"header":{"User-Agent":\["Cue/[^ ]+ \(cmd/cue; .*\) Go/[^ ]+ \(.*\)"\]}}}'
77

88
# Check that the resulting module evaluates as expected.
99
exec cue export .

0 commit comments

Comments
 (0)