diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 7cb23da6a..6a900a0df 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -18,3 +18,4 @@ - Board links imported with spread imports work [#1972](https://github.com/terrastruct/d2/pull/1972) - Fix importing a file with nested boards [#1998](https://github.com/terrastruct/d2/pull/1998) - Fix importing a file with underscores in links [#1999](https://github.com/terrastruct/d2/pull/1999) +- Replace a panic with an error message resulting from invalid `link` usage [#2011](https://github.com/terrastruct/d2/pull/2011) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index b5ed15cf4..90c2f26fb 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -2986,6 +2986,38 @@ layers: { tassert.Equal(t, "root.layers.x.layers.k", g.Layers[0].Layers[0].Objects[1].Link.Value) }, }, + { + name: "invalid-link-1", + text: `k + +layers: { + x: {...@x} +}`, + files: map[string]string{ + "x.d2": `k +layers: { + y.link: @n +}`, + "n.d2": `n`, + }, + expErr: `d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked`, + }, + { + name: "invalid-link-2", + text: `k + +layers: { + x: @x +}`, + files: map[string]string{ + "x.d2": `k +layers: { + y.link: @n +}`, + "n.d2": `n`, + }, + expErr: `d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked`, + }, { name: "import-nested-layers", text: `k diff --git a/d2ir/compile.go b/d2ir/compile.go index 3b62cc1c7..fc8f558b9 100644 --- a/d2ir/compile.go +++ b/d2ir/compile.go @@ -854,8 +854,13 @@ func (c *compiler) ignoreLazyGlob(n Node) bool { // When importing a file, all of its board links need to be extended to reflect their new path func (c *compiler) extendLinks(m *Map, importF *Field) { + nodeBoardKind := NodeBoardKind(m) for _, f := range m.Fields { if f.Name == "link" { + if nodeBoardKind != "" { + c.errorf(f.LastRef().AST(), "a board itself cannot be linked; only objects within a board can be linked") + continue + } val := f.Primary().Value.ScalarString() link, err := d2parser.ParseKey(val) if err != nil { @@ -879,8 +884,13 @@ func (c *compiler) extendLinks(m *Map, importF *Field) { } func (c *compiler) updateLinks(m *Map) { + nodeBoardKind := NodeBoardKind(m) for _, f := range m.Fields { if f.Name == "link" { + if nodeBoardKind != "" { + c.errorf(f.LastRef().AST(), "a board itself cannot be linked; only objects within a board can be linked") + continue + } val := f.Primary().Value.ScalarString() link, err := d2parser.ParseKey(val) if err != nil { diff --git a/testdata/d2compiler/TestCompile/invalid-link-1.exp.json b/testdata/d2compiler/TestCompile/invalid-link-1.exp.json new file mode 100644 index 000000000..e7cc159a4 --- /dev/null +++ b/testdata/d2compiler/TestCompile/invalid-link-1.exp.json @@ -0,0 +1,11 @@ +{ + "graph": null, + "err": { + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/x.d2,2:4:16-2:8:20", + "errmsg": "d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile/invalid-link-2.exp.json b/testdata/d2compiler/TestCompile/invalid-link-2.exp.json new file mode 100644 index 000000000..e7cc159a4 --- /dev/null +++ b/testdata/d2compiler/TestCompile/invalid-link-2.exp.json @@ -0,0 +1,11 @@ +{ + "graph": null, + "err": { + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/x.d2,2:4:16-2:8:20", + "errmsg": "d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile/invalid-link.exp.json b/testdata/d2compiler/TestCompile/invalid-link.exp.json new file mode 100644 index 000000000..e7cc159a4 --- /dev/null +++ b/testdata/d2compiler/TestCompile/invalid-link.exp.json @@ -0,0 +1,11 @@ +{ + "graph": null, + "err": { + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/x.d2,2:4:16-2:8:20", + "errmsg": "d2/testdata/d2compiler/TestCompile/x.d2:3:5: a board itself cannot be linked; only objects within a board can be linked" + } + ] + } +}