siyuan-note/siyuan · error

template call depth exceeds

Error message

template call depth exceeds %d

What it means

validateTemplateCallGraph computes the longest chain of nested {{template}} calls from the start definition and rejects graphs whose depth exceeds maxTemplateCallDepth. This caps nesting so deeply chained template packages cannot exhaust memory or stack, even though each link individually is legal and acyclic.

Solutions

  1. Flatten the definition chain by inlining some wrappers or calling leaf content directly
  2. Split the output into multiple sibling definitions instead of one deep nesting chain
  3. Reduce one level of indirection per render unit; aim for a shallow, flat hierarchy
  4. Check the constant maxTemplateCallDepth in the kernel to know the exact allowed limit

Example fix

// before: page -> section -> block -> inline (4 levels)
// after: page -> block (inline content directly in block)
{{define "block"}}...{{template "inline"}}...{{end}}
Defensive patterns

Strategy: validation

Validate before calling

depth := maxNestingDepth(calls); if depth > maxTemplateCallDepth { /* reject */ }

Try / catch

if err := validateTemplateCallGraph(root, start); err != nil { return err }

Prevention

When it happens

Trigger: Raised when the computed depth from visit(start) exceeds maxTemplateCallDepth during renderTemplateSource or renderTemplateDocTreeNodeContent: e.g. a chain of definitions A -> B -> C -> ... longer than the allowed maximum.

Common situations: Elaborate template packages that layer many wrapper definitions (page -> section -> block -> inline), often built incrementally over time until the chain silently exceeds the limit.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/776025ca4a82aa35. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/template_doc_tree_render.go:141

		for _, called := range collectCalledTemplateNames(tmpl.Tree.Root) {
			calledDepth, err := visit(called)
			if nil != err {
				return 0, err
			}
			if depth < calledDepth+1 {
				depth = calledDepth + 1
			}
		}
		delete(visiting, name)
		depths[name] = depth
		return depth, nil
	}
	depth, err := visit(start)
	if nil != err {
		return err
	}
	if maxTemplateCallDepth < depth {
		return fmt.Errorf("template call depth exceeds %d", maxTemplateCallDepth)
	}
	return nil
}

func collectCalledTemplateNames(node templateparse.Node) (ret []string) {
	if nil == node {
		return nil
	}
	switch typed := node.(type) {
	case *templateparse.ListNode:
		if nil == typed {
			return nil
		}
		for _, child := range typed.Nodes {
			ret = append(ret, collectCalledTemplateNames(child)...)
		}
	case *templateparse.TemplateNode:
		if nil == typed {

View on GitHub (pinned to 9f775e8a12)