terrastruct / d2

D2 is a modern diagram scripting language that turns text to diagrams.
https://d2lang.com
Mozilla Public License 2.0
18.13k stars 444 forks source link

Elk algorithms cause runtime error: index out of range #1417

Open gitsmol opened 1 year ago

gitsmol commented 1 year ago

Using the elk layout engine with non-default algorithms reliably crashes d2 when adding edges.

Running:

d2 --layout=elk --elk-algorithm org.eclipse.elk.rectpacking --debug test.d2

On this file works fine:

# comment
Test
Thing

But this file crashes:

# comment
Test -> thing

With error:

debug: using theme Neutral default (ID: 0)
debug: using layout plugin elk (bundled)
panic: runtime error: index out of range [0] with length 0

goroutine 22 [running]:
oss.terrastruct.com/d2/d2layouts/d2elklayout.Layout({0x104425ce8, 0x140007e2a50}, 0x1400016e630, 0x0?)
    oss.terrastruct.com/d2/d2layouts/d2elklayout/layout.go:482 +0x17ac
oss.terrastruct.com/d2/d2plugin.elkPlugin.Layout(...)
    oss.terrastruct.com/d2/d2plugin/plugin_elk.go:108
oss.terrastruct.com/d2/d2layouts/d2grid.Layout.func1({0x104425ce8, 0x140007e2a50}, 0x1400016e630)
    oss.terrastruct.com/d2/d2layouts/d2grid/layout.go:41 +0xbc
oss.terrastruct.com/d2/d2layouts/d2sequence.Layout({0x104425ce8, 0x140007e2a50}, 0x1400016e630, 0x1400041c290)
    oss.terrastruct.com/d2/d2layouts/d2sequence/layout.go:35 +0xf0
oss.terrastruct.com/d2/d2lib.compile({0x104425ce8, 0x140007e2a50}, 0x1400016e630, 0x140005cebf0)
    oss.terrastruct.com/d2/d2lib/d2.go:85 +0x168
oss.terrastruct.com/d2/d2lib.Compile({0x104425ce8, 0x140007e2a50}, {0x1400095f9c8, 0x18}, 0x140000e27a8?)
    oss.terrastruct.com/d2/d2lib/d2.go:50 +0xf0
oss.terrastruct.com/d2/d2cli.compile({0x104425ce8, 0x140007e2a50}, 0x14000166c40, {0x104427aa8, 0x104c24af8}, {0x64, 0x0, 0x0, 0x0, 0x0, ...}, ...)
    oss.terrastruct.com/d2/d2cli/main.go:343 +0x20c
oss.terrastruct.com/d2/d2cli.Run({0x104425c78?, 0x14000779900?}, 0x14000166c40)
    oss.terrastruct.com/d2/d2cli/main.go:304 +0x1ca8
oss.terrastruct.com/util-go/xmain.(*State).Main.func1()
    oss.terrastruct.com/util-go@v0.0.0-20230604222829-11c3c60fec14/xmain/xmain.go:105 +0x68
created by oss.terrastruct.com/util-go/xmain.(*State).Main
    oss.terrastruct.com/util-go@v0.0.0-20230604222829-11c3c60fec14/xmain/xmain.go:103 +0x11c

I've manually tried all of the algorithms elk provides and only the default one seems to compile.

Is this expected behaviour, are the other elk algo's not supported? I've looked at layout.go but can't spot an obvious culprit for this indexing error. Apart from the obvious suspicion that g.Edges doesn't contain any edges.

Any pointers?

gitsmol commented 1 year ago

Alright, I looked closer at the ELK docs and answered my own question. The problem is some of the algorithms are specifically designed for unconnected graphs. A nice to have could be an error message in that case, as there is no indication that this shouldn't work. Or just strip all edges and present only the nodes.

Anyway, sorry to make a fuss :)

alixander commented 1 year ago

@gitsmol Thanks for reporting this! If it's alright with you I'd like to keep this open -- no matter what, we shouldn't panic. A helpful error message should be present.