From f277522b2aade38fe65262cf3d5e05e62ccf0ba4 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Fri, 14 Mar 2025 19:58:37 -0600 Subject: [PATCH 1/3] add test --- d2compiler/compile_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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: { From cc1e647dfc8ddcf3ac2af251e283c4626d834845 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Fri, 14 Mar 2025 21:17:41 -0600 Subject: [PATCH 2/3] fix var panic --- d2ir/compile.go | 4 ++ .../TestCompile/import-var-chain.exp.json | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 testdata/d2compiler/TestCompile/import-var-chain.exp.json 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 +} From 4bce55eb493f7dd4ae62329a8d030ab0852cd68d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Fri, 14 Mar 2025 21:18:45 -0600 Subject: [PATCH 3/3] next --- ci/release/changelogs/next.md | 1 + 1 file changed, 1 insertion(+) 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)