Skip to content

Commit b36b477

Browse files
uhthomasmvdan
authored andcommitted
cmd/cue: fix panic when importing empty files as a list
Empty files are represented as nil when parsed, which causes cue to panic as the condition is not considered when asked to import as a list. Fixes #2721 Change-Id: I20f3b49eb66661f156c06e036579ce6beddfe0d8 Signed-off-by: Thomas Way <[email protected]> Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173072 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent e1ceed4 commit b36b477

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

cmd/cue/cmd/orphans.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,10 @@ func placeOrphans(b *buildPlan, d *encoding.Decoder, pkg string, objs ...*ast.Fi
295295
f.Decls = append(f.Decls, x.Elts...)
296296
case *ast.ListLit:
297297
f.Decls = append(f.Decls, &ast.EmbedDecl{Expr: x})
298+
case nil:
299+
f.Decls = append(f.Decls, ast.NewList())
298300
default:
299-
panic("unreachable")
301+
panic(fmt.Sprintf("unexpected type %T", x))
300302
}
301303
}
302304

cmd/cue/cmd/testdata/script/import_list.txtar

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ cmp stdout expect-stdout2
88
# Issue #369
99
exec cue import --with-context -l '"\(path.Ext(filename))": data' ./import3/data.json
1010
cmpenv import3/data.cue expect3
11+
12+
# Issue #2721
13+
exec cue import -f --list issue2721/empty.yaml
14+
cmp issue2721/empty.cue issue2721/empty.cue.golden
1115
-- expect-stdout1 --
1216
service: [{
1317
kind: "Service"
@@ -50,3 +54,7 @@ deployment: [{
5054
-- import3/data.json --
5155
[1]
5256
-- cue.mod --
57+
58+
-- issue2721/empty.yaml --
59+
-- issue2721/empty.cue.golden --
60+
[]

0 commit comments

Comments
 (0)