From 03885ec9f1eee40c99f8dc67b7e5296913fadf1e Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Wed, 27 Apr 2022 15:03:44 +0100 Subject: [PATCH] hclparser: strip out blocks for json files Body.JustAttributes cannot distinguish between blocks and attributes for JSON files, so the variable block could be included in the list of attributes returned. This patch ensures that JSON and HCL files behave the same way by removing all known block types first, from the provided config schema and then from a generated definitions schema. Fixes #1051 Signed-off-by: Justin Chadwell --- bake/hcl_test.go | 10 ++++++++++ bake/hclparser/hclparser.go | 16 +++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/bake/hcl_test.go b/bake/hcl_test.go index 18d7a63e..5d66cbf1 100644 --- a/bake/hcl_test.go +++ b/bake/hcl_test.go @@ -745,3 +745,13 @@ target "two" { require.Equal(t, c.Targets[1].Name, "two") require.Equal(t, map[string]string{"b": "pre-jkl"}, c.Targets[1].Args) } + +func TestEmptyVariableJSON(t *testing.T) { + dt := []byte(`{ + "variable": { + "VAR": {} + } + }`) + _, err := ParseFile(dt, "docker-bake.json") + require.NoError(t, err) +} diff --git a/bake/hclparser/hclparser.go b/bake/hclparser/hclparser.go index 233d6557..cb27c615 100644 --- a/bake/hclparser/hclparser.go +++ b/bake/hclparser/hclparser.go @@ -256,6 +256,7 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics { if err := gohcl.DecodeBody(b, nil, &defs); err != nil { return err } + defsSchema, _ := gohcl.ImpliedBodySchema(defs) if opt.LookupVar == nil { opt.LookupVar = func(string) (string, bool) { @@ -300,6 +301,16 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics { p.funcs[v.Name] = v } + content, b, diags := b.PartialContent(schema) + if diags.HasErrors() { + return diags + } + + _, b, diags = b.PartialContent(defsSchema) + if diags.HasErrors() { + return diags + } + attrs, diags := b.JustAttributes() if diags.HasErrors() { if d := removeAttributesDiags(diags, reserved, p.vars); len(d) > 0 { @@ -371,11 +382,6 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics { } } - content, _, diags := b.PartialContent(schema) - if diags.HasErrors() { - return diags - } - for _, a := range content.Attributes { return hcl.Diagnostics{ &hcl.Diagnostic{