d2lang/d2 · error

dimensions for class method %#v not found

Error message

dimensions for class method %#v not found

What it means

Identical mechanism to the class-field error but for method labels: when sizing a class shape, each method's text must be measured with the mono font. A nil result from GetTextDimensionsWithMono aborts with this error because the class's height/width cannot be computed without method label dimensions.

Source

Thrown at d2graph/d2graph.go:1053

		fontSize := d2fonts.FONT_SIZE_L
		if obj.Style.FontSize != nil {
			fontSize, _ = strconv.Atoi(obj.Style.FontSize.Value)
		}

		for _, f := range obj.Class.Fields {
			var fdims *d2target.TextDimensions
			fdims = GetTextDimensionsWithMono(mtexts, ruler, f.Text(fontSize), go2.Pointer(d2fonts.SourceCodePro), monoFontFamily)
			if fdims == nil {
				return nil, fmt.Errorf("dimensions for class field %#v not found", f.Text(fontSize))
			}
			maxWidth = go2.Max(maxWidth, fdims.Width)
		}
		for _, m := range obj.Class.Methods {
			var mdims *d2target.TextDimensions
			mdims = GetTextDimensionsWithMono(mtexts, ruler, m.Text(fontSize), go2.Pointer(d2fonts.SourceCodePro), monoFontFamily)
			if mdims == nil {
				return nil, fmt.Errorf("dimensions for class method %#v not found", m.Text(fontSize))
			}
			maxWidth = go2.Max(maxWidth, mdims.Width)
		}
		//    ┌─PrefixWidth ┌─CenterPadding
		// ┌─┬─┬───────┬──────┬───┬──┐
		// │ + getJobs()      Job[]  │
		// └─┴─┴───────┴──────┴───┴──┘
		//  └─PrefixPadding        └──TypePadding
		//     ├───────┤   +  ├───┤  = maxWidth
		dims.Width = d2target.PrefixPadding + d2target.PrefixWidth + maxWidth + d2target.CenterPadding + d2target.TypePadding

		// All rows should be the same height
		var anyRowText *d2target.MText
		if len(obj.Class.Fields) > 0 {
			anyRowText = obj.Class.Fields[0].Text(fontSize)
		} else if len(obj.Class.Methods) > 0 {
			anyRowText = obj.Class.Methods[0].Text(fontSize)
		}

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Provide a non-nil, font-loaded textmeasure.Ruler to SetDimensions
  2. Regenerate mtexts (graph.SetText) after any change to class methods
  3. Ensure the mono font family (default SourceCodePro) is loaded in the ruler
  4. Keep class shape definitions immutable between text measurement and SetDimensions

Example fix

// before
ruler, _ := textmeasure.NewRuler() // fonts never initialized
// after
ruler, _ := textmeasure.NewRuler()
ruler.InitFonts(d2fonts.D2Fonts)
err := graph.SetDimensions(mtexts, ruler, fontFamily, &d2fonts.SourceCodePro)
Defensive patterns

Strategy: validation

Validate before calling

ruler, _ := textmeasure.NewRuler()
ruler.InitFonts(d2fonts.D2Fonts)
for _, obj := range graph.Objects {
    if obj.Class != nil {
        for _, m := range obj.Class.Methods {
            txt := m.Text(d2fonts.DEFAULT_FONT_SIZE).Text
            if strings.TrimSpace(txt) == "" { continue }
            found := false
            for _, mt := range mtexts { if mt.Text == txt { found = true; break } }
            if !found { return fmt.Errorf("unmeasured class method: %q", txt) }
        }
    }
}

Type guard

func allMethodsMeasured(obj *d2graph.Object, mtexts []*d2target.MText) bool {
    if obj.Class == nil { return true }
    for _, m := range obj.Class.Methods {
        txt := m.Text(d2fonts.DEFAULT_FONT_SIZE).Text
        found := false
        for _, mt := range mtexts { if mt.Text == txt { found = true; break } }
        if !found { return false }
    }
    return true
}

Try / catch

if err := graph.SetDimensions(mtexts, ruler, fontFamily, mono); err != nil {
    if strings.Contains(err.Error(), "dimensions for class method") {
        mtexts = graph.SetText(ruler, fontFamily)
        err = graph.SetDimensions(mtexts, ruler, fontFamily, mono)
    }
    return err
}

Prevention

When it happens

Trigger: Rendering a class shape whose Class.Methods contain a method whose text at the given fontSize is absent from mtexts and cannot be measured (nil ruler, or ruler missing the mono font/text combination).

Common situations: Class shapes with methods defined but measurement texts generated before the methods existed; nil ruler in embedded usage; custom mono font family not registered; third-party tooling constructing classes directly.

Related errors


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