Skip to content

ELK non-layered algorithms panic on edges to deeply-nested nodes #2717

@Gerg

Description

@Gerg

Summary

ELK algorithms other than layered panic with "index out of range" when an edge connects an external node to a node inside a nested container.

Reproduction

d2 --version
0.7.1

test.d2:

outer: {
  inner: {
    a
  }
}
external -> outer.inner.a

Working:

❯ d2 --layout=elk --elk-algorithm=layered test.d2 test.png
success: successfully compiled test.d2 to test.png in 372.446125ms

Error:

d2 --layout=elk --elk-algorithm=stress test.d2 test.png
panic: runtime error: index out of range [0] with length 0

goroutine 8 [running]:
oss.terrastruct.com/d2/d2layouts/d2elklayout.Layout(...)
	oss.terrastruct.com/d2/d2layouts/d2elklayout/layout.go:604 +0x1dcc

Root Cause

points := edge.Route
startIndex, endIndex := 0, len(points)-1
start := points[startIndex]
end := points[endIndex]

Non-layered ELK algorithms return empty Route arrays for edges that cross container boundaries. The code assumes edge.Route is non-empty.

AI Disclaimer

Issue diagnosed with assistance from Claude 4.5 Opus.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions