diff --git a/bake/hcl_test.go b/bake/hcl_test.go index dedde357..005b4785 100644 --- a/bake/hcl_test.go +++ b/bake/hcl_test.go @@ -634,6 +634,29 @@ func TestHCLMultiFileAttrs(t *testing.T) { require.Equal(t, ptrstr("pre-ghi"), c.Targets[0].Args["v1"]) } +func TestHCLMultiFileGlobalAttrs(t *testing.T) { + dt := []byte(` + FOO = "abc" + target "app" { + args = { + v1 = "pre-${FOO}" + } + } + `) + dt2 := []byte(` + FOO = "def" + `) + + c, err := ParseFiles([]File{ + {Data: dt, Name: "c1.hcl"}, + {Data: dt2, Name: "c2.hcl"}, + }, nil) + require.NoError(t, err) + require.Equal(t, 1, len(c.Targets)) + require.Equal(t, c.Targets[0].Name, "app") + require.Equal(t, "pre-def", *c.Targets[0].Args["v1"]) +} + func TestHCLDuplicateTarget(t *testing.T) { dt := []byte(` target "app" { diff --git a/bake/hclparser/hclparser.go b/bake/hclparser/hclparser.go index d7461cd7..7efdc9f6 100644 --- a/bake/hclparser/hclparser.go +++ b/bake/hclparser/hclparser.go @@ -613,7 +613,7 @@ func Parse(b hcl.Body, opt Opt, val interface{}) (map[string]map[string][]string attrs, diags := b.JustAttributes() if diags.HasErrors() { - if d := removeAttributesDiags(diags, reserved, p.vars); len(d) > 0 { + if d := removeAttributesDiags(diags, reserved, p.vars, attrs); len(d) > 0 { return nil, d } } @@ -857,7 +857,7 @@ func getNameIndex(v reflect.Value) (int, bool) { return 0, false } -func removeAttributesDiags(diags hcl.Diagnostics, reserved map[string]struct{}, vars map[string]*variable) hcl.Diagnostics { +func removeAttributesDiags(diags hcl.Diagnostics, reserved map[string]struct{}, vars map[string]*variable, attrs hcl.Attributes) hcl.Diagnostics { var fdiags hcl.Diagnostics for _, d := range diags { if fout := func(d *hcl.Diagnostic) bool { @@ -879,6 +879,12 @@ func removeAttributesDiags(diags hcl.Diagnostics, reserved map[string]struct{}, return true } } + for a := range attrs { + // Do the same for attributes + if strings.HasPrefix(d.Detail, fmt.Sprintf(`Argument "%s" was already set at `, a)) { + return true + } + } return false }(d); !fout { fdiags = append(fdiags, d) diff --git a/bake/hclparser/merged.go b/bake/hclparser/merged.go index cc3f2b2b..d53c788b 100644 --- a/bake/hclparser/merged.go +++ b/bake/hclparser/merged.go @@ -123,9 +123,7 @@ func (mb mergedBodies) JustAttributes() (hcl.Attributes, hcl.Diagnostics) { ), Subject: &attr.NameRange, }) - continue } - attrs[name] = attr } } @@ -196,7 +194,6 @@ func (mb mergedBodies) mergedContent(schema *hcl.BodySchema, partial bool) (*hcl ), Subject: &attr.NameRange, }) - continue } content.Attributes[name] = attr }