diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index ab55c0441..0aec8da04 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -32,6 +32,7 @@ - Compiler: - fixes panic when `sql_shape` shape value had mixed casing [#2349](https://github.com/terrastruct/d2/pull/2349) + - fixes panic when importing from a file with spread substitutions in `vars` [#2427](https://github.com/terrastruct/d2/pull/2427) - fixes support for `center` in `d2-config` [#2360](https://github.com/terrastruct/d2/pull/2360) - fixes panic when comment lines appear in arrays [#2378](https://github.com/terrastruct/d2/pull/2378) - fixes inconsistencies when objects were double quoted [#2390](https://github.com/terrastruct/d2/pull/2390) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index c3ce5c5e7..47183fe9c 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -3125,6 +3125,24 @@ x*: { tassert.Equal(t, "x2.ok", g.Objects[3].AbsID()) }, }, + { + name: "import-var-chain", + + text: `...@dev +`, + files: map[string]string{ + "dev.d2": ` +vars: { + a: { + b + } + c: { + ...${a} + } +} +`, + }, + }, { name: "var_in_markdown", text: `vars: { diff --git a/d2ir/compile.go b/d2ir/compile.go index 9473b7e37..a239862a8 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -1134,6 +1134,10 @@ func (c *compiler) extendLinks(m *Map, importF *Field, importDir string) { nodeBoardKind := NodeBoardKind(m) importIDA := IDA(importF) for _, f := range m.Fields { + // A substitute or such + if f.Name == nil { + continue + } if f.Name.ScalarString() == "link" && f.Name.IsUnquoted() { if nodeBoardKind != "" { c.errorf(f.LastRef().AST(), "a board itself cannot be linked; only objects within a board can be linked") diff --git a/testdata/d2compiler/TestCompile/import-var-chain.exp.json b/testdata/d2compiler/TestCompile/import-var-chain.exp.json new file mode 100644 index 000000000..8868dcdb7 --- /dev/null +++ b/testdata/d2compiler/TestCompile/import-var-chain.exp.json @@ -0,0 +1,57 @@ +{ + "graph": { + "name": "", + "isFolderOnly": true, + "ast": { + "range": "d2/testdata/d2compiler/TestCompile/import-var-chain.d2,0:0:0-1:0:8", + "nodes": [ + { + "import": { + "range": "d2/testdata/d2compiler/TestCompile/import-var-chain.d2,0:0:0-0:7:7", + "spread": true, + "pre": "", + "path": [ + { + "unquoted_string": { + "range": "d2/testdata/d2compiler/TestCompile/import-var-chain.d2,0:4:4-0:7:7", + "value": [ + { + "string": "dev", + "raw_string": "dev" + } + ] + } + } + ] + } + } + ] + }, + "root": { + "id": "", + "id_val": "", + "attributes": { + "label": { + "value": "" + }, + "labelDimensions": { + "width": 0, + "height": 0 + }, + "style": {}, + "near_key": null, + "shape": { + "value": "" + }, + "direction": { + "value": "" + }, + "constraint": null + }, + "zIndex": 0 + }, + "edges": null, + "objects": null + }, + "err": null +}