cue icon indicating copy to clipboard operation
cue copied to clipboard

list.Max has error after compile

Open verdverm opened this issue 2 years ago • 2 comments

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

0.6.0

What did you do?

exec go run main.go

-- main.go --
package main

import (
	"fmt"
	"os"

	"cuelang.org/go/cue/cuecontext"
)

const data = `
import "list"

ports: [... int]
newPort: list.Max(ports) + 1
`

func main() {
	ctx := cuecontext.New()

	v := ctx.CompileString(data)

	if v.Err() != nil {
		fmt.Println(v.Err())
		os.Exit(1)
	}

	fmt.Println(v)
}

or

$ cue def in.cue

-- in.cue --
import "list"

ports: [... int]
newPort: list.Max(ports) + 1

if list.Max is not used, there is no error

seems like too early evaluation or something

What did you expect to see?

An incomplete value printed, like with def

What did you see instead?

newPort: error in call to list.Max: empty list:
    ./v/file.cue:4:10

This example is based on https://github.com/cue-lang/cue/blob/master/cmd/cue/cmd/testdata/script/vet_listmax.txtar which could probably use an extra checks for the above

verdverm avatar Aug 24 '23 19:08 verdverm

Not sure if this is related, but it is close in reproducer, just changing the cue file.

Basically what I am seeing is that CompileString (also via ctx.BuildInstance), that calling value.Err() on an incomplete value seems to produce an error. Intuitively, this does not seem like a compile or build error, and that it would be more appropriate if I was asking for a concrete value. I haven't even used the value yet, so why is it in an error state?

package main

import (
	"fmt"
	"os"

	"cuelang.org/go/cue/cuecontext"
)

const schema = `
package foo

_env: =~"(prd)-"

#Foo

#Foo: {
	env: "dev" | _env
	baz: bool | *false

	if env == "dev" {
		_defaults.dev
	}
}

_defaults: {
	"dev": {
		baz: true
	}
}
`

func main() {
	ctx := cuecontext.New()

	v := ctx.CompileString(schema)

	if v.Err() != nil {
		fmt.Println(v.Err())
		os.Exit(1)
	}

	fmt.Println(v)
}


verdverm avatar Aug 24 '23 21:08 verdverm

Still happens as of v0.13.0-alpha.4. Testscript below:

exec cue vet -c=false in.cue

-- in.cue --
import "list"

ports: [... int]
newPort: list.Max(ports) + 1

which fails:

> exec cue vet -c=false in.cue
[stderr]
newPort: error in call to list.Max: empty list:
    ./in.cue:4:10
[exit status 1]
FAIL: repro-cmd.txtar:1: unexpected command failure

mvdan avatar May 07 '25 20:05 mvdan