From 2aa22597f07bbbdb85198aff20234c3cd230bb4f Mon Sep 17 00:00:00 2001 From: Justin Chadwell Date: Wed, 22 Jun 2022 11:15:35 +0100 Subject: [PATCH] bake: forbid empty result and params in userfuncs Signed-off-by: Justin Chadwell --- bake/hclparser/hclparser.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/bake/hclparser/hclparser.go b/bake/hclparser/hclparser.go index cb27c615..3fdfb0f8 100644 --- a/bake/hclparser/hclparser.go +++ b/bake/hclparser/hclparser.go @@ -111,6 +111,13 @@ func (p *parser) resolveFunction(name string) error { } p.progressF[name] = struct{}{} + if f.Result == nil { + return errors.Errorf("empty result not allowed for %s", name) + } + if f.Params == nil { + return errors.Errorf("empty params not allowed for %s", name) + } + paramExprs, paramsDiags := hcl.ExprList(f.Params.Expr) if paramsDiags.HasErrors() { return paramsDiags @@ -306,7 +313,7 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics { return diags } - _, b, diags = b.PartialContent(defsSchema) + blocks, b, diags := b.PartialContent(defsSchema) if diags.HasErrors() { return diags } @@ -370,13 +377,27 @@ func Parse(b hcl.Body, opt Opt, val interface{}) hcl.Diagnostics { if diags, ok := err.(hcl.Diagnostics); ok { return diags } + var subject *hcl.Range + var context *hcl.Range + if p.funcs[k].Params != nil { + subject = &p.funcs[k].Params.Range + context = subject + } else { + for _, block := range blocks.Blocks { + if block.Type == "function" && len(block.Labels) == 1 && block.Labels[0] == k { + subject = &block.LabelRanges[0] + context = &block.DefRange + break + } + } + } return hcl.Diagnostics{ &hcl.Diagnostic{ Severity: hcl.DiagError, Summary: "Invalid function", Detail: err.Error(), - Subject: &p.funcs[k].Params.Range, - Context: &p.funcs[k].Params.Range, + Subject: subject, + Context: context, }, } }