diff --git a/cmd/d2/main.go b/cmd/d2/main.go index 04e37aa3e..9e8d290d1 100644 --- a/cmd/d2/main.go +++ b/cmd/d2/main.go @@ -169,7 +169,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) { _ = 343 } - _, err = compile(ctx, ms, plugin, *themeFlag, inputPath, outputPath, pw.Page) + _, err = compile(ctx, ms, false, plugin, *themeFlag, inputPath, outputPath, pw.Page) if err != nil { return err } @@ -177,7 +177,7 @@ func run(ctx context.Context, ms *xmain.State) (err error) { return nil } -func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, themeID int64, inputPath, outputPath string, page playwright.Page) ([]byte, error) { +func compile(ctx context.Context, ms *xmain.State, isWatching bool, plugin d2plugin.Plugin, themeID int64, inputPath, outputPath string, page playwright.Page) ([]byte, error) { input, err := ms.ReadPath(inputPath) if err != nil { return nil, err @@ -205,16 +205,24 @@ func compile(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, theme if err != nil { return nil, err } - svg, err = imgbundler.InlineLocal(ms, svg) + svg, err = imgbundler.InlineLocal(svg) if err != nil { - return nil, err + // Missing/broken images are fine during watch mode, as the user is likely building up a diagram. + // Otherwise, the assumption is that this diagram is building for production, and broken images are not okay. + if !isWatching { + return nil, err + } + ms.Log.Debug.Printf("ignoring missing/broken local image in watch mode: %v", err) } out := svg if filepath.Ext(outputPath) == ".png" { - svg, err = imgbundler.InlineRemote(ms, svg) + svg, err = imgbundler.InlineRemote(svg) if err != nil { - return nil, err + if !isWatching { + return nil, err + } + ms.Log.Debug.Printf("ignoring missing/broken remote image in watch mode: %v", err) } out, err = png.ConvertSVG(ms, page, svg) diff --git a/cmd/d2/watch.go b/cmd/d2/watch.go index b49489df6..ed528ead3 100644 --- a/cmd/d2/watch.go +++ b/cmd/d2/watch.go @@ -345,7 +345,7 @@ func (w *watcher) compileLoop(ctx context.Context) error { w.pw = newPW } - b, err := compile(ctx, w.ms, w.layoutPlugin, w.themeID, w.inputPath, w.outputPath, w.pw.Page) + b, err := compile(ctx, w.ms, true, w.layoutPlugin, w.themeID, w.inputPath, w.outputPath, w.pw.Page) if err != nil { err = fmt.Errorf("failed to %scompile: %w", recompiledPrefix, err) w.ms.Log.Error.Print(err) diff --git a/lib/imgbundler/imgbundler.go b/lib/imgbundler/imgbundler.go index 71aaa9ee9..8782bea0e 100644 --- a/lib/imgbundler/imgbundler.go +++ b/lib/imgbundler/imgbundler.go @@ -16,8 +16,6 @@ import ( "go.uber.org/multierr" "oss.terrastruct.com/xdefer" - - "oss.terrastruct.com/d2/lib/xmain" ) var imageRe = regexp.MustCompile(` `, svgURL, pngURL) - ms := &xmain.State{ - Name: "test", - - Stdin: os.Stdin, - Stdout: os.Stdout, - Stderr: os.Stderr, - - Env: xos.NewEnv(os.Environ()), - } - ms.Log = cmdlog.Log(ms.Env, os.Stderr) - out, err := InlineLocal(ms, []byte(sampleSVG)) + out, err := InlineLocal([]byte(sampleSVG)) if err != nil { t.Fatal(err) }