diff --git a/d2compiler/compile.go b/d2compiler/compile.go index c1800e571..c1bed291b 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -283,6 +283,9 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) { c.errorf(f.Map().Edges[0].LastRef().AST(), "vars cannot contain an edge") } } + if d2ir.NodeBoardKind(d2ir.ParentMap(f)) == "" { + c.errorf(f.LastRef().AST(), "vars must be defined at the root of a board") + } return } else if isReserved { c.compileReserved(&obj.Attributes, f) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index d64283335..878199312 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3545,6 +3545,18 @@ hi: ${colors} `, `d2/testdata/d2compiler/TestCompile2/vars/errors/map.d2:7:1: cannot reference map variable "colors"`) }, }, + { + name: "non-root", + run: func(t *testing.T) { + assertCompile(t, ` +x: { + vars: { + x: hey + } +} +`, `d2/testdata/d2compiler/TestCompile2/vars/errors/non-root.d2:3:3: vars must be defined at the root of a board`) + }, + }, } for _, tc := range tca {