Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evalv3: lookup failure even when field apparently exists #3577

Open
rogpeppe opened this issue Nov 15, 2024 · 2 comments
Open

evalv3: lookup failure even when field apparently exists #3577

rogpeppe opened this issue Nov 15, 2024 · 2 comments

Comments

@rogpeppe
Copy link
Member

What version of CUE are you using (cue version)?

v0.11.0-rc.1

Does this issue reproduce with the latest stable release?

Yes (v0.10.0)

What did you do?

exec go mod tidy
exec go test

env CUE_EXPERIMENT=evalv3

exec go test

-- go.mod --
module test

go 1.24

require (
	cuelang.org/go v0.11.0-rc.1
	github.com/go-quicktest/qt v1.101.0
)
replace cuelang.org/go => /home/rogpeppe/src/cuelabs/cue
-- main.go --
package main

func main() {}
-- main_test.go --
package main

import (
	"os"
	"slices"
	"testing"

	"cuelang.org/go/cue"
	"cuelang.org/go/cue/cuecontext"
	"github.com/go-quicktest/qt"
)

func TestLoad(t *testing.T) {
	evalVersion := cuecontext.EvalV2
	if os.Getenv("CUE_EXPERIMENT") == "evalv3" {
		// Earlier CUE versions don't recognise CUE_EXPERIMENT
		// by default, so simulate it so bisection works OK.
		evalVersion = cuecontext.EvalV3
	}
	ctx := cuecontext.New(cuecontext.EvaluatorVersion(evalVersion))
	schemasv := ctx.CompileString(`pkg: x, x: y: "hello"`)

	pkgv := schemasv.LookupPath(cue.MakePath(cue.Str("pkg")))
	if !pkgv.Exists() {
		t.Fatalf("cannot find package in generated schemas")
	}

	// We can see the field "y" in the resulting value when we iterate through its fields.
	var fields []string
	iter, err := pkgv.Fields(cue.All())
	qt.Assert(t, qt.IsNil(err))
	for iter.Next() {
		fields = append(fields, iter.Selector().String())
	}
	if !slices.Contains(fields, "y") {
		t.Fatalf("pkg value does not contain field y when iterating")
	}

	// ... but it fails on lookup (with v3 only)!
	schemav := pkgv.LookupPath(cue.MakePath(cue.Str("y")))
	if !schemav.Exists() {
		t.Fatalf("schema not found")
	}
}

What did you expect to see?

A passing test.

What did you see instead?

> exec go mod tidy
> exec go test
[stdout]
PASS
ok  	test	0.004s
> env CUE_EXPERIMENT=evalv3
> exec go test
[stdout]
--- FAIL: TestLoad (0.00s)
    main_test.go:42: schema not found
FAIL
exit status 1
FAIL	test	0.004s
[exit status 1]
FAIL: y.txtar:6: unexpected command failure
@rogpeppe
Copy link
Member Author

I dug into the difference between Value.Fields and Value.LookupPath here and the difference seems to be that Fields obtains a value from Value.Default before iteration, where LookupPath does not.

@mvdan mvdan changed the title eval: lookup failure even when field apparently exists evalv3: lookup failure even when field apparently exists Nov 15, 2024
@mvdan
Copy link
Member

mvdan commented Nov 15, 2024

Retitled this as it seems to only happen with evalv3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants