From b3ebded99a195f3300ed02d80ff5876a9784f575 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Tue, 31 Oct 2023 15:52:48 -0700 Subject: [PATCH] cli: Handle invalid board paths --- ci/release/changelogs/next.md | 1 + d2cli/main.go | 2 +- d2target/d2target.go | 13 ++++--------- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index add6512b65..7ef4b59747 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -14,3 +14,4 @@ - Fixes use of `null` in `sql_table` constraints (ty @landmaj) [#1660](https://github.com/terrastruct/d2/pull/1660) - Fixes elk growing shapes with width/height set [#1679](https://github.com/terrastruct/d2/pull/1679) - Adds a compiler error when accidentally using an arrowhead on a shape [#1686](https://github.com/terrastruct/d2/pull/1686) +- Fixes crash when using `--watch` and navigating to an invalid board path [#1693](https://github.com/terrastruct/d2/pull/1693) diff --git a/d2cli/main.go b/d2cli/main.go index 52f3a2ed0e..4aae3c4943 100644 --- a/d2cli/main.go +++ b/d2cli/main.go @@ -503,7 +503,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, la board := diagram.GetBoard(boardPath) if board == nil { - return nil, false, fmt.Errorf("Diagram with path %s not found", boardPath) + return nil, false, fmt.Errorf(`Diagram with path "%s" not found. Did you mean to specify a board like "layers.%s"?`, boardPath, boardPath) } boards, err := render(ctx, ms, compileDur, plugin, renderOpts, inputPath, outputPath, bundle, forceAppendix, page, ruler, board) diff --git a/d2target/d2target.go b/d2target/d2target.go index 46e086985e..01ff19dadd 100644 --- a/d2target/d2target.go +++ b/d2target/d2target.go @@ -95,29 +95,24 @@ func (d *Diagram) getBoard(boardPath []string) *Diagram { return d } + if len(boardPath) < 2 { + return nil + } + switch head { case "layers": - if len(boardPath) < 2 { - return nil - } for _, b := range d.Layers { if b.Name == boardPath[1] { return b.getBoard(boardPath[2:]) } } case "scenarios": - if len(boardPath) < 2 { - return nil - } for _, b := range d.Scenarios { if b.Name == boardPath[1] { return b.getBoard(boardPath[2:]) } } case "steps": - if len(boardPath) < 2 { - return nil - } for _, b := range d.Steps { if b.Name == boardPath[1] { return b.getBoard(boardPath[2:])