cue-lang / cue

The home of the CUE language! Validate and define text-based and dynamic configuration
https://cuelang.org
Apache License 2.0
5.11k stars 291 forks source link

cmd/cue: export evaluates constraints outside the --expression flag's context inconsistently #3371

Open jpluscplusm opened 2 months ago

jpluscplusm commented 2 months ago

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

$ cue version
cue version v0.10.0

go version go1.23.0
      -buildmode exe
       -compiler gc
       -trimpath true
     CGO_ENABLED 0
          GOARCH amd64
            GOOS linux
         GOAMD64 v1
cue.lang.version v0.10.0

Does this issue reproduce with the latest stable release?

0.10.0 is latest

What did you do?

exec cue export no-package.cue -e foo
exec cue export package-p.cue  -e foo
exec cue export .:p            -e foo
exec cue export data.yml       -e foo

-- package-p.cue --
package p
foo: true
bar: false
bar: true

-- no-package.cue --
foo: true
bar: false
bar: true

-- data.yml --
foo: true
bar: false
bar: true

What did you expect to see?

I expected to see one of these outcomes:

What did you see instead?

$ testscript -continue cue.export.expressionHandledDifferentlyPackageVersusFile.txtar
> exec cue export no-package.cue -e foo
[stdout]
true
> exec cue export package-p.cue  -e foo
[stdout]
true
> exec cue export .:p            -e foo
[stderr]
bar: conflicting values true and false:
    ./package-p.cue:3:6
    ./package-p.cue:4:6
[exit status 1]
FAIL: /tmp/testscript536191441/cue.export.expressionHandledDifferentlyPackageVersusFile.txtar/script.txtar:3: unexpected command failure
> exec cue export data.yml       -e foo
[stderr]
bar: conflicting values true and false:
    ./data.yml:2:6
    ./data.yml:3:6
[exit status 1]
FAIL: /tmp/testscript536191441/cue.export.expressionHandledDifferentlyPackageVersusFile.txtar/script.txtar:4: unexpected command failure

Context

I'm currently writing a concept guide for cuelang.org that explains cue export. The inconsistency highlighted above will either need to be called out explicitly, complicating the guide; or (preferably; if it's an error) corrected in the command's behaviour.

mvdan commented 2 months ago

Thanks for filing this. From reading cue help inputs, cue export package-p.cue and cue export .:p should have exactly the same behavior, so I think this is definitely a bug.

I think all four commands should succeed, with the assumption that -e only evaluates what it needs to - and it clearly already does that in some cases.