this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

encoding/yaml: consistently use cue.Concrete when calling Value.Syntax

The call to Value.Syntax without cue.Concrete means we don't use
export's ExpandReferences option, so we use the dep package,
which ends performing evaluations to finalize values.

It's these evaluations which then trigger the panic in the issue:

panic: runtime error: index out of range [25] with length 19 [recovered, repanicked]

goroutine 1 gp=0x1c42c03361c0 m=0 mp=0x1aa8e20 [running]:
panic({0x1086880?, 0x1c42c0370be8?})
/home/mvdan/tip/src/runtime/panic.go:877 +0x16f fp=0x1c42c0679af0 sp=0x1c42c0679a40 pc=0x49344f
cuelang.org/go/internal/core/dep.(*visitor).visit.func1()
/home/mvdan/src/c/cue/internal/core/dep/dep.go:235 +0xd8 fp=0x1c42c0679b38 sp=0x1c42c0679af0 pc=0x8cd378
[...]
cuelang.org/go/internal/core/adt.(*nodeContext).addReplacement(...)
/home/mvdan/src/c/cue/internal/core/adt/typocheck.go:272
cuelang.org/go/internal/core/adt.(*nodeContext).insertConstraint.func1({0x1c42c0645520, {0x12c95e0, 0x1c42c06891d0}, {0x1, 0x19, 0x0, 0x0, 0x0, 0x0, 0x0, ...}})
/home/mvdan/src/c/cue/internal/core/adt/constraints.go:109 +0x1b1 fp=0x1c42c0679f08 sp=0x1c42c0679e98 pc=0x881e91

Luckily, given that the YAML encoder never uses anchors currently,
we can avoid using dep entirely by using the cue.Concrete option,
meaning that we will marshal a fully expanded value as YAML.
This should result in the same encoded YAML, but reduce the amount
of required work, and sidestep this bug entirely.

There is still likely an underlying bug in either typocheck.go
or the dep package, but given that we are currently rethinking
typo checking via the explicitopen experiment, adding a regression test
for now seems like it's enough. If we ever stopped the YAML encoding
from expanding references again, the bug would surface via the test
if it still existed.

Fixes #4131.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: I881b4f4f5f12399a4c257bf1406fb039c1e8d530
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1224352
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+37 -4
+31
cmd/cue/cmd/testdata/script/issue4131.txtar
··· 1 + # This used to cause a panic in typocheck.go. 2 + 3 + exec cue export -e out.keys --out yaml in.cue 4 + cmp stdout stdout.golden 5 + 6 + -- in.cue -- 7 + _inputs: foo: _ 8 + _inputs: [_]: "foo value" 9 + 10 + _cfg: ({ 11 + x: (_CopyKeys & {_in: _inputs})._out 12 + }).x 13 + 14 + out: (_Files & {cfg: _cfg}).files 15 + 16 + _Files: { 17 + cfg: keys: {} 18 + cfg: copied: {} 19 + files: { 20 + keys: [for k, _ in cfg.keys {k}] 21 + for k, v in cfg.copied {(k): v} 22 + } 23 + } 24 + 25 + _CopyKeys: { 26 + _in: {} 27 + _out: keys: _in 28 + _out: copied: {for k, v in _in {(k): v}} 29 + } 30 + -- stdout.golden -- 31 + - foo
+4 -2
encoding/yaml/yaml.go
··· 70 70 71 71 // Encode returns the YAML encoding of v. 72 72 func Encode(v cue.Value) ([]byte, error) { 73 - n := v.Syntax(cue.Final()) 73 + // Note that we use [cue.Concrete] in this package, which expands all references. 74 + // If we want YAML to encode with anchors in the future, we can change this. 75 + n := v.Syntax(cue.Concrete(true)) 74 76 b, err := cueyaml.Encode(n) 75 77 return b, err 76 78 } ··· 84 86 if i > 0 { 85 87 buf.WriteString("---\n") 86 88 } 87 - n := iter.Value().Syntax(cue.Final()) 89 + n := iter.Value().Syntax(cue.Concrete(true)) 88 90 b, err := cueyaml.Encode(n) 89 91 if err != nil { 90 92 return nil, err
+2 -2
pkg/encoding/yaml/manual.go
··· 31 31 if err := v.Validate(cue.Concrete(true)); err != nil { 32 32 return "", err 33 33 } 34 - n := v.Syntax(cue.Final(), cue.Concrete(true)) 34 + n := v.Syntax(cue.Concrete(true)) 35 35 b, err := cueyaml.Encode(n) 36 36 return string(b), err 37 37 } ··· 52 52 if err := v.Validate(cue.Concrete(true)); err != nil { 53 53 return "", err 54 54 } 55 - n := v.Syntax(cue.Final(), cue.Concrete(true)) 55 + n := v.Syntax(cue.Concrete(true)) 56 56 b, err := cueyaml.Encode(n) 57 57 if err != nil { 58 58 return "", err