this repo has no description
0
fork

Configure Feed

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

cmd/cue: do not panic on empty jsonl inputs

buildPlan.instances always returned an iterator with at least
one instance, and in the case of an empty jsonl file,
this would result in iterating over a single nil instance.
This would cause the following logic, like exporting, to panic.

An empty jsonl input means zero files, and is allowed by the spec.
Moreover, zero bytes is not valid input, unlike YAML or TOML,
which produce null and {} respectively.
For these reasons, treat empty jsonl inputs as a stream of zero values.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ie0a62bb59d347011de5aa47f5ee6d175824451a3
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199451
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Matthew Sackman <matthew@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+15 -7
+7 -1
cmd/cue/cmd/common.go
··· 142 142 e: err, 143 143 i: -1, 144 144 } 145 - default: 145 + case b.instance != nil: 146 146 i = &instanceIterator{ 147 147 a: []*instance{b.instance}, 148 148 i: -1, 149 149 } 150 150 b.instance = nil 151 + default: 152 + // No instances; return an iterator with zero values. 153 + // This can happen when the input is an empty jsonl file, for example. 154 + // Zero iteration may not make much sense in some scenarios like export, 155 + // but an empty jsonl file is valid, so doing nothing seems reasonable. 156 + i = &instanceIterator{} 151 157 } 152 158 if len(b.expressions) > 0 { 153 159 return &expressionIter{
+8 -6
cmd/cue/cmd/testdata/script/encoding_empty.txtar
··· 1 1 # Test that the various encodings cope with empty files correctly. 2 - 3 - # TODO(mvdan): fix the panics below; see https://cuelang.org/issue/1790 and https://cuelang.org/issue/2714. 2 + # Note that empty files or inputs have different meanings per encoding. 3 + # For example, in CUE and TOML they mean an empty struct or object, 4 + # in JSON they are invalid, and in JSONL they are a stream of zero values. 4 5 5 6 # TODO(mvdan): cover more encodings: jsonschema, openapi, textproto, proto. 6 7 ··· 12 13 cmp stdout as-cue.stdout 13 14 ! exec cue export json: empty 14 15 stderr 'unexpected end of JSON input' 15 - ! exec cue export jsonl: empty 16 - stderr '^panic: ' 16 + exec cue export jsonl: empty 17 + cmp stdout as-jsonl.stdout 17 18 exec cue export yaml: empty 18 19 cmp stdout as-yaml.stdout 19 20 exec cue export toml: empty ··· 25 26 cmp stdout as-cue.stdout 26 27 ! exec cue export json: newlines 27 28 stderr 'unexpected end of JSON input' 28 - ! exec cue export jsonl: newlines 29 - stderr '^panic: ' 29 + exec cue export jsonl: newlines 30 + cmp stdout as-jsonl.stdout 30 31 exec cue export yaml: newlines 31 32 cmp stdout as-yaml.stdout 32 33 exec cue export toml: newlines ··· 48 49 49 50 -- as-cue.stdout -- 50 51 {} 52 + -- as-jsonl.stdout -- 51 53 -- as-yaml.stdout -- 52 54 null 53 55 -- as-toml.stdout --