From a86c15bb61ed943adad5f3fcf00d0d04b4fbbe27 Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Thu, 17 Sep 2026 12:19:43 +0200 Subject: [PATCH] bake: report undefined variables correctly Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- bake/hcl_test.go | 12 ++++++++++++ bake/hclparser/hclparser.go | 12 ++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/bake/hcl_test.go b/bake/hcl_test.go index c57ac78213b7..31aac914c2d0 100644 --- a/bake/hcl_test.go +++ b/bake/hcl_test.go @@ -598,6 +598,18 @@ func TestHCLVariableCycle(t *testing.T) { require.Contains(t, err.Error(), "variable cycle not allowed") } +func TestHCLUndefinedVariable(t *testing.T) { + dt := []byte(` + target "app" { + dockerfile = "${FOO}${FOO}" + } + `) + + _, err := ParseFile(dt, "docker-bake.hcl") + require.ErrorContains(t, err, "Unknown variable") + require.NotContains(t, err.Error(), "variable cycle not allowed") +} + func TestHCLAttrs(t *testing.T) { dt := []byte(` FOO="abc" diff --git a/bake/hclparser/hclparser.go b/bake/hclparser/hclparser.go index ec7bf19036e0..4f0ecc7566fb 100644 --- a/bake/hclparser/hclparser.go +++ b/bake/hclparser/hclparser.go @@ -268,6 +268,13 @@ func (p *parser) resolveValue(ectx *hcl.EvalContext, name string) (err error) { if _, ok := ectx.Variables[name]; ok { return nil } + if _, ok := p.opt.Vars[name]; !ok { + if _, ok := p.attrs[name]; !ok { + if _, ok := p.vars[name]; !ok { + return errors.Wrapf(errUndefined{}, "variable %q does not exist", name) + } + } + } if _, ok := p.progressV[key(ectx, name)]; ok { return errors.Errorf("variable cycle not allowed for %s", name) } @@ -292,10 +299,7 @@ func (p *parser) resolveValue(ectx *hcl.EvalContext, name string) (err error) { varType, typeSpecified := cty.DynamicPseudoType, false def, ok := p.attrs[name] if !ok { - vr, ok := p.vars[name] - if !ok { - return errors.Wrapf(errUndefined{}, "variable %q does not exist", name) - } + vr := p.vars[name] def = vr.Default ectx = p.ectx varType, diags = typeConstraint(vr.Type)