From b36b4775e6a890899c460f62f8e59b4182d63c2a Mon Sep 17 00:00:00 2001 From: Thomas Way Date: Sun, 3 Dec 2023 23:22:45 +0000 Subject: [PATCH] cmd/cue: fix panic when importing empty files as a list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173072 Unity-Result: CUE porcuepine TryBot-Result: CUEcueckoo Reviewed-by: Daniel Martí --- cmd/cue/cmd/orphans.go | 4 +++- cmd/cue/cmd/testdata/script/import_list.txtar | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/cue/cmd/orphans.go b/cmd/cue/cmd/orphans.go index 0a9b0244634..d1023f25229 100644 --- a/cmd/cue/cmd/orphans.go +++ b/cmd/cue/cmd/orphans.go @@ -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)) } } diff --git a/cmd/cue/cmd/testdata/script/import_list.txtar b/cmd/cue/cmd/testdata/script/import_list.txtar index b7bc2ebc292..fb34d7faa46 100644 --- a/cmd/cue/cmd/testdata/script/import_list.txtar +++ b/cmd/cue/cmd/testdata/script/import_list.txtar @@ -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" @@ -50,3 +54,7 @@ deployment: [{ -- import3/data.json -- [1] -- cue.mod -- + +-- issue2721/empty.yaml -- +-- issue2721/empty.cue.golden -- +[]