Skip to content

Commit

Permalink
internal/mod/modload: support adding language version
Browse files Browse the repository at this point in the history
We want to add a language version declaration to the module.cue
file when it's not present. This enables that.

Also change the modload tests to use `modfile.File.Format` to
format their output, as that's more representative of the
actual use.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I627d2522d6bf7a88a90c9031058addd7b8d5758d
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173894
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
  • Loading branch information
rogpeppe committed Jan 29, 2024
1 parent c6e4adf commit 2120803
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 55 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/modtidy.go
Expand Up @@ -58,7 +58,7 @@ func runModTidy(cmd *Command, args []string) error {
if err != nil {
return err
}
mf, err := modload.Load(ctx, os.DirFS(modRoot), ".", reg)
mf, err := modload.Tidy(ctx, os.DirFS(modRoot), ".", reg, "")
if err != nil {
return err
}
Expand Down
15 changes: 11 additions & 4 deletions internal/mod/modload/load.go
Expand Up @@ -37,9 +37,11 @@ type loader struct {
registry Registry
}

// Load evaluates all the requirements of the given main module, using the given
// Tidy evaluates all the requirements of the given main module, using the given
// registry to download requirements and returns a resolved and tidied module file.
func Load(ctx context.Context, fsys fs.FS, modRoot string, reg Registry) (*modfile.File, error) {
// If there's no language version in the module file and cueVers is non-empty
// it will be used to populate the language version field.
func Tidy(ctx context.Context, fsys fs.FS, modRoot string, reg Registry, cueVers string) (*modfile.File, error) {
modFilePath := path.Join(modRoot, "cue.mod/module.cue")
data, err := fs.ReadFile(fsys, modFilePath)
if err != nil {
Expand Down Expand Up @@ -79,15 +81,20 @@ func Load(ctx context.Context, fsys fs.FS, modRoot string, reg Registry) (*modfi
if err != nil {
return nil, fmt.Errorf("cannot tidy requirements: %v", err)
}
return modfileFromRequirements(mf, rs), nil
return modfileFromRequirements(mf, rs, cueVers), nil
}

func modfileFromRequirements(old *modfile.File, rs *modrequirements.Requirements) *modfile.File {
func modfileFromRequirements(old *modfile.File, rs *modrequirements.Requirements, cueVers string) *modfile.File {
mf := &modfile.File{
Module: old.Module,
Language: old.Language,
Deps: make(map[string]*modfile.Dep),
}
if cueVers != "" && (mf.Language == nil || mf.Language.Version == "") {
mf.Language = &modfile.Language{
Version: cueVers,
}
}
defaults := rs.DefaultMajorVersions()
for _, v := range rs.RootModules() {
mf.Deps[v.Path()] = &modfile.Dep{
Expand Down
11 changes: 6 additions & 5 deletions internal/mod/modload/load_test.go
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/google/go-cmp/cmp"
"golang.org/x/tools/txtar"

"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/internal/mod/modfile"
"cuelang.org/go/internal/mod/modpkgload"
"cuelang.org/go/internal/mod/modregistry"
Expand All @@ -39,14 +38,16 @@ func TestLoad(t *testing.T) {
want, err := fs.ReadFile(tfs, "want")
qt.Assert(t, qt.IsNil(err))

cueVers, _ := fs.ReadFile(tfs, "cue-version")

var out strings.Builder
mf, err := Load(context.Background(), tfs, ".", reg)
mf, err := Tidy(context.Background(), tfs, ".", reg, strings.TrimSpace(string(cueVers)))
if err != nil {
fmt.Fprintf(&out, "error: %v\n", err)
} else {
ctx := cuecontext.New()
v := ctx.Encode(mf)
fmt.Fprintln(&out, v)
data, err := mf.Format()
qt.Assert(t, qt.IsNil(err))
out.Write(data)
}
if diff := cmp.Diff(string(want), out.String()); diff != "" {
t.Log("actual result:\n", out.String())
Expand Down
12 changes: 12 additions & 0 deletions internal/mod/modload/testdata/addversion.txtar
@@ -0,0 +1,12 @@
# Test that a version is added if not present
-- cue-version --
v1.2.3
-- want --
module: "main.org@v0"
language: {
version: "v1.2.3"
}
-- cue.mod/module.cue --
module: "main.org@v0"
-- main.cue --
package main
10 changes: 4 additions & 6 deletions internal/mod/modload/testdata/canuseprerelease.txtar
Expand Up @@ -2,12 +2,10 @@
# no stable releases available.

-- want --
{
module: "main.org@v0"
deps: {
"example.com@v0": {
v: "v0.0.3-alpha"
}
module: "main.org@v0"
deps: {
"example.com@v0": {
v: "v0.0.3-alpha"
}
}
-- cue.mod/module.cue --
Expand Down
@@ -1,13 +1,11 @@
# This test checks we can have have an import with an implied major version
# when there's an explicit default in the module file.
-- want --
{
module: "main.org@v0"
deps: {
"example.com@v0": {
v: "v0.0.1"
default: true
}
module: "main.org@v0"
deps: {
"example.com@v0": {
v: "v0.0.1"
default: true
}
}
-- cue.mod/module.cue --
Expand Down
@@ -1,13 +1,11 @@
# This test checks we can have have an import with an implied major version
# when there's no explicit default in the module file.
-- want --
{
module: "main.org@v0"
deps: {
"example.com@v1": {
v: "v1.0.0"
default: true
}
module: "main.org@v0"
deps: {
"example.com@v1": {
v: "v1.0.0"
default: true
}
}
-- cue.mod/module.cue --
Expand Down
28 changes: 13 additions & 15 deletions internal/mod/modload/testdata/nested-deps.txtar
Expand Up @@ -2,21 +2,19 @@
# and a module that's unused.

-- want --
{
module: "main.org@v0"
deps: {
"bar.com@v0": {
v: "v0.5.0"
}
"baz.org@v0": {
v: "v0.10.1"
}
"example.com@v0": {
v: "v0.0.1"
}
"foo.com/bar/hello@v0": {
v: "v0.2.3"
}
module: "main.org@v0"
deps: {
"bar.com@v0": {
v: "v0.5.0"
}
"baz.org@v0": {
v: "v0.10.1"
}
"example.com@v0": {
v: "v0.0.1"
}
"foo.com/bar/hello@v0": {
v: "v0.2.3"
}
}
-- cue.mod/module.cue --
Expand Down
13 changes: 13 additions & 0 deletions internal/mod/modload/testdata/noaddversion.txtar
@@ -0,0 +1,13 @@
# Test that a version is not added if present
-- cue-version --
v1.2.3
-- want --
module: "main.org@v0"
language: {
version: "v1.0.3"
}
-- cue.mod/module.cue --
module: "main.org@v0"
language: version: "v1.0.3"
-- main.cue --
package main
10 changes: 4 additions & 6 deletions internal/mod/modload/testdata/preferstable.txtar
Expand Up @@ -2,12 +2,10 @@
# pre-releases.

-- want --
{
module: "main.org@v0"
deps: {
"example.com@v0": {
v: "v0.0.1"
}
module: "main.org@v0"
deps: {
"example.com@v0": {
v: "v0.0.1"
}
}
-- cue.mod/module.cue --
Expand Down
5 changes: 1 addition & 4 deletions internal/mod/modload/testdata/stdlibpackagenotfound.txtar
Expand Up @@ -2,10 +2,7 @@
# the standard library but that doesn't exist.

-- want --
{
module: "main.org@v0"
deps: {}
}
module: "main.org@v0"
-- cue.mod/module.cue --
module: "main.org@v0"

Expand Down

0 comments on commit 2120803

Please sign in to comment.