d2lang/d2 · error

code snippet formatter "svg" not found

Error message

code snippet formatter "svg" not found

What it means

After obtaining the highlighting style for a code-snippet shape label, d2svg fetches the chroma 'svg' formatter to emit highlighted markup. If formatters.Get("svg") returns nil, the formatter registry lacks the SVG formatter and drawShape fails with this error.

Source

Thrown at d2renderers/d2svg/d2svg.go:2150

				targetShape.Underline,
			))
		} else if targetShape.Language != "" {
			lexer := lexers.Get(targetShape.Language)
			if lexer == nil {
				lexer = lexers.Fallback
			}
			for _, isLight := range []bool{true, false} {
				theme := "github"
				if !isLight {
					theme = "catppuccin-mocha"
				}
				style := styles.Get(theme)
				if style == nil {
					return labelMask, errors.New(`code snippet style "github" not found`)
				}
				formatter := formatters.Get("svg")
				if formatter == nil {
					return labelMask, errors.New(`code snippet formatter "svg" not found`)
				}
				iterator, err := lexer.Tokenise(nil, targetShape.Label)
				if err != nil {
					return labelMask, err
				}

				svgStyles := styleToSVG(style)
				class := "light-code"
				if !isLight {
					class = "dark-code"
				}
				var fontSize string
				if targetShape.FontSize != d2fonts.FONT_SIZE_M {
					fontSize = fmt.Sprintf(` style="font-size:%v"`, targetShape.FontSize)
				}
				fmt.Fprintf(writer, `<g transform="translate(%f %f)" class="%s"%s>`,
					box.TopLeft.X, box.TopLeft.Y, class, fontSize,
				)

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Blank-import github.com/alecthomas/chroma/v2/formatters so embedded formatters (including svg) register
  2. Verify the installed chroma version provides the svg formatter; upgrade or pin chroma
  3. Ensure vendor/build steps do not remove the formatters package

Example fix

// before
import _ "github.com/alecthomas/chroma/v2/styles"
// after
import (
    _ "github.com/alecthomas/chroma/v2/formatters"
    _ "github.com/alecthomas/chroma/v2/styles"
)
Defensive patterns

Strategy: fallback

Validate before calling

if chromaformatters.Get("svg") == nil {
    return errors.New("chroma svg formatter unavailable")
}

Type guard

func chromaFormatterAvailable(name string) bool {
    return chromaformatters.Get(name) != nil
}

Try / catch

out, err := d2svg.Render(diagram, opts)
if err != nil && strings.Contains(err.Error(), "code snippet formatter") {
    // retry after registering formatters or render labels unhighlighted
}

Prevention

When it happens

Trigger: Rendering a shape with a code-snippet label via d2svg.Render when the chroma formatters registry does not contain 'svg' (formatters.Get returns nil).

Common situations: Custom builds that blank-import only chroma styles but not formatters; chroma version changes; vendoring that drops the formatters package.

Related errors


AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31). Data as JSON: /api/errors/641b7e290222357d. Report an issue: GitHub.