Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d2compile: invalidate self-links #2203

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

#### Improvements 🧹

- Composition: links pointing to own board are purged [#2203](https://github.com/terrastruct/d2/pull/2203)

#### Bugfixes ⛑️
6 changes: 6 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"io/fs"
"net/url"
"slices"
"strconv"
"strings"

Expand Down Expand Up @@ -1224,6 +1225,11 @@ func (c *compiler) validateBoardLinks(g *d2graph.Graph) {
obj.Link = nil
continue
}

if slices.Equal(linkKey.IDA(), obj.Graph.IDA()) {
obj.Link = nil
continue
}
}
for _, b := range g.Layers {
c.validateBoardLinks(b)
Expand Down
21 changes: 21 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2317,6 +2317,27 @@ scenarios: {
tassert.Equal(t, "root.layers.cat", g.Scenarios[0].Objects[0].Link.Value)
},
},
{
name: "no-self-link",
text: `
x.link: scenarios.a

layers: {
g: {
s.link: _.layers.g
}
}

scenarios: {
a: {
b
}
}`,
assertions: func(t *testing.T, g *d2graph.Graph) {
tassert.Equal(t, (*d2graph.Scalar)(nil), g.Scenarios[0].Objects[0].Link)
tassert.Equal(t, (*d2graph.Scalar)(nil), g.Layers[0].Objects[0].Link)
},
},
{
name: "link-board-not-found-1",
text: `x.link: layers.x
Expand Down
60 changes: 60 additions & 0 deletions d2graph/d2graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,66 @@ func (g *Graph) RootBoard() *Graph {
return g
}

func (g *Graph) IDA() []string {
if g == nil {
return nil
}

var parts []string

current := g
for current != nil {
if current.Name != "" {
parts = append(parts, current.Name)
}
current = current.Parent
}

for i := 0; i < len(parts)/2; i++ {
j := len(parts) - 1 - i
parts[i], parts[j] = parts[j], parts[i]
}

if len(parts) == 0 {
return []string{"root"}
}

parts = append([]string{"root"}, parts...)

if g.Parent != nil {
var containerName string
if len(g.Parent.Layers) > 0 {
for _, l := range g.Parent.Layers {
if l == g {
containerName = "layers"
break
}
}
}
if len(g.Parent.Scenarios) > 0 {
for _, s := range g.Parent.Scenarios {
if s == g {
containerName = "scenarios"
break
}
}
}
if len(g.Parent.Steps) > 0 {
for _, s := range g.Parent.Steps {
if s == g {
containerName = "steps"
break
}
}
}
if containerName != "" {
parts = append(parts[:1], append([]string{containerName}, parts[1:]...)...)
}
}

return parts
}

type LayoutGraph func(context.Context, *Graph) error
type RouteEdges func(context.Context, *Graph, []*Edge) error

Expand Down
Loading
Loading