Skip to content

Commit

Permalink
cmd/cue: fix --ignore errors flag
Browse files Browse the repository at this point in the history
Fixes #1981
Fixes #1786

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: Ie7461fc6d42bea71db99559c4961271b6a271fd0
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/546243
Unity-Result: CUEcueckoo <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Daniel Martí <[email protected]>
  • Loading branch information
mpvl committed Nov 17, 2022
1 parent c5fbbab commit ab14c86
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
24 changes: 14 additions & 10 deletions cmd/cue/cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func runEval(cmd *Command, args []string) error {
cue.Definitions(true),
cue.Attributes(flagAttributes.Bool(cmd)),
cue.Optional(flagAll.Bool(cmd) || flagOptional.Bool(cmd)),
cue.ErrorsAsValues(flagIgnore.Bool(cmd)),
}

// Keep for legacy reasons. Note that `cue eval` is to be deprecated by
Expand Down Expand Up @@ -143,18 +144,21 @@ func runEval(cmd *Command, args []string) error {
b, _ := format.Node(b.expressions[i%len(b.expressions)])
id = string(b)
}
if err := v.Err(); err != nil {
errHeader()
return v.Validate(syn...)
}

// TODO(#553): this can be removed once v.Syntax() below retains line
// information.
if (e.IsConcrete() || flagConcrete.Bool(cmd)) && !flagIgnore.Bool(cmd) {
if err := v.Validate(cue.Concrete(true)); err != nil {
if !flagIgnore.Bool(cmd) {
if err := v.Err(); err != nil {
errHeader()
exitOnErr(cmd, err, false)
continue
return v.Validate(syn...)
}

// TODO(#553): this can be removed once v.Syntax() below retains line
// information.
if e.IsConcrete() || flagConcrete.Bool(cmd) {
if err := v.Validate(cue.Concrete(true)); err != nil {
errHeader()
exitOnErr(cmd, err, false)
continue
}
}
}

Expand Down
20 changes: 20 additions & 0 deletions cmd/cue/cmd/testdata/script/eval_ignore.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
exec cue eval -i in.cue
cmp stdout expect/stdout

# Issue #1981
# Issue #1786
-- in.cue --
a: 4
a: 5

l: [ 1, 2 ]
l: [ 1, 3 ]

list: [0, 1, 2]
val: list[3]

-- expect/stdout --
a: _|_ // a: conflicting values 5 and 4
l: [1, _|_]
list: [0, 1, 2]
val: _|_ // val: index out of range [3] with length 3
8 changes: 8 additions & 0 deletions cue/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2137,6 +2137,14 @@ func ResolveReferences(resolve bool) Option {
}
}

// ErrorsAsValues treats errors as a regular value, including them at the
// location in the tree where they occur, instead of interpreting them as a
// configuration-wide failure that is returned instead of root value.
// Used by Syntax.
func ErrorsAsValues(show bool) Option {
return func(p *options) { p.showErrors = show }
}

// Raw tells Syntax to generate the value as is without any simplifications.
func Raw() Option {
return func(p *options) { p.raw = true }
Expand Down

0 comments on commit ab14c86

Please sign in to comment.