cue icon indicating copy to clipboard operation
cue copied to clipboard

Dynamic field expression alias

Open Pythoner6 opened this issue 1 year ago • 4 comments

According to the language spec, one of the supported alias positions is before the expression of a dynamic field:

In front of a dynamic field expression ((X=expr): value):

  • binds the identifier to the concrete label resulting from evaluating expr.

However, currently this just seems to cause a syntax error.

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

$ cue version
cue version 0.7.0

go version go1.21.5
      -buildmode exe
       -compiler gc
       -trimpath true
  DefaultGODEBUG panicnil=1
     CGO_ENABLED 1
          GOARCH amd64
            GOOS linux
         GOAMD64 v1

Does this issue reproduce with the latest stable release?

Yes

What did you do?

// test.cue
(x="foo"): x
$ cue eval test.cue

What did you expect to see?

foo: "foo"

What did you see instead?

expected ')', found '=':
    ../../tmp/foo/test.cue:1:3

Pythoner6 avatar Dec 15 '23 07:12 Pythoner6

Well spotted! This is definitely a bug.

mvdan avatar Dec 15 '23 09:12 mvdan

I just rediscovered this spec deviation.

Here's a txtar repro that should pass:

exec cue export
cmp stdout out
-- file.cue --
package p

(X="foo"): bar: 1
baz: X
-- out --
{
    "foo": {
        "bar": 1
    },
    "baz": "foo"
}

Here's the failure with v0.12.1:

> exec cue export
[stderr]
expected ')', found '=':
    ./file.cue:3:3
[exit status 1]
FAIL: foo.txtar:1: unexpected command failure
failed run

jpluscplusm avatar Apr 24 '25 11:04 jpluscplusm

Just to make clear a let-based workaround that follows the exact same scoping rules:

exec cue export
cmp stdout out
-- file.cue --
package p

let X="foo"
(X): bar: 1
baz: X
-- out --
{
    "foo": {
        "bar": 1
    },
    "baz": "foo"
}

myitcv avatar Apr 25 '25 11:04 myitcv

Note that this spec deviation bug will disappear with the aliasv2 language change: https://github.com/cue-lang/cue/discussions/4014

mvdan avatar Dec 06 '25 14:12 mvdan