larksuite/cli · error

body container is nil

Error message

body container is nil

What it means

ensureBodyPartRef was handed a nil **Part pointer, meaning there is no addressable slot in the MIME tree in which to create or find the body part. Unlike a nil *Part (an empty slot that gets filled with a new leaf), a nil **Part reference is a programming/state error in the caller. Only reached when the caller passes a literally nil pointer rather than the address of a (possibly nil) slot.

Source

Thrown at shortcuts/mail/draft/patch.go:464

	if strings.EqualFold(part.MediaType, "multipart/mixed") {
		for idx := range part.Children {
			child := part.Children[idx]
			if child == nil || strings.EqualFold(child.ContentDisposition, "attachment") {
				continue
			}
			return &part.Children[idx]
		}
		if len(part.Children) == 0 {
			part.Children = append(part.Children, nil)
			return &part.Children[0]
		}
	}
	return root
}

func ensureBodyPartRef(partRef **Part, bodyKind string) (*Part, error) {
	if partRef == nil {
		return nil, fmt.Errorf("body container is nil")
	}
	if *partRef == nil {
		leaf := newBodyLeaf(bodyKind)
		leaf.Dirty = true
		*partRef = leaf
		return leaf, nil
	}
	part := *partRef
	if !part.IsMultipart() {
		if strings.EqualFold(part.MediaType, bodyKind) {
			return part, nil
		}
		if !isBodyKind(part.MediaType) {
			return nil, fmt.Errorf("cannot rewrite non-body media type %q", part.MediaType)
		}
		newLeaf := newBodyLeaf(bodyKind)
		alt := newMultipartContainer("multipart/alternative")
		if strings.EqualFold(part.MediaType, "text/plain") {

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Pass a valid &snapshot.Body (or the address of an actual child slot) instead of a nil pointer.
  2. Guard callers so they only invoke ensureBodyPartRef with references obtained from primaryBodyRootRef or a real Children[idx] slot.
  3. Repair/rebuild the snapshot tree if earlier mutations left nil or dangling child references.

Example fix

// before
part, err := ensureBodyPartRef(nil, "text/html")
// after
partRef := primaryBodyRootRef(&snapshot.Body)
part, err := ensureBodyPartRef(partRef, "text/html")
Defensive patterns

Strategy: type-guard

Validate before calling

if partRef == nil { partRef = primaryBodyRootRef(&snapshot.Body) }
part, err := ensureBodyPartRef(partRef, "text/html")

Type guard

func validPartRef(r **Part) bool { return r != nil }

Prevention

When it happens

Trigger: Calling ensureBodyPartRef(nil, bodyKind) directly, or recursively via the multipart/related branch when a child slot yields a nil reference; also reachable through ensureBodyPart if primaryBodyRootRef returned a nil root pointer under unusual tree states.

Common situations: Custom callers of the internal body-ensure helpers passing an unaddressable/nil reference; MIME trees corrupted by earlier mutations that left dangling child slots; reflection-based or generic tree code constructing bad references.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/68140da1a520cf63. Report an issue: GitHub.