Skip to content

Commit

Permalink
cmd/cue: fix panic when importing empty files as a list
Browse files Browse the repository at this point in the history
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 <thomas@6f.io>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173072
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
  • Loading branch information
uhthomas authored and mvdan committed Dec 4, 2023
1 parent e1ceed4 commit b36b477
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/cue/cmd/orphans.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,10 @@ func placeOrphans(b *buildPlan, d *encoding.Decoder, pkg string, objs ...*ast.Fi
f.Decls = append(f.Decls, x.Elts...)
case *ast.ListLit:
f.Decls = append(f.Decls, &ast.EmbedDecl{Expr: x})
case nil:
f.Decls = append(f.Decls, ast.NewList())
default:
panic("unreachable")
panic(fmt.Sprintf("unexpected type %T", x))
}
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/cue/cmd/testdata/script/import_list.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ cmp stdout expect-stdout2
# Issue #369
exec cue import --with-context -l '"\(path.Ext(filename))": data' ./import3/data.json
cmpenv import3/data.cue expect3

# Issue #2721
exec cue import -f --list issue2721/empty.yaml
cmp issue2721/empty.cue issue2721/empty.cue.golden
-- expect-stdout1 --
service: [{
kind: "Service"
Expand Down Expand Up @@ -50,3 +54,7 @@ deployment: [{
-- import3/data.json --
[1]
-- cue.mod --

-- issue2721/empty.yaml --
-- issue2721/empty.cue.golden --
[]

0 comments on commit b36b477

Please sign in to comment.