siyuan-note/siyuan · error

parse template tree failed

Error message

parse template tree failed

What it means

parseTemplateKTree parses rendered template markdown with Lute and fails when the parser returns a nil tree. This is a defensive invariant check: Lute's parse.Parse virtually always returns a tree, so a nil result indicates an unexpected parser failure on the rendered markdown bytes.

Solutions

  1. Dump the rendered markdown that was passed to parseTemplateKTree and inspect it for corruption or truncation
  2. Fix the template so its output is well-formed Markdown/Kramdown
  3. Check output size limits; a truncated render (e.g. output-size cap cutting mid construct) can yield unparseable bytes
  4. If it reproduces with valid markdown, report it as a Lute/kernel bug since nil tree is unexpected
Defensive patterns

Strategy: try-catch

Type guard

if tree == nil { return nil, errors.New("parse template tree failed") }

Try / catch

tree, err := parseTemplateKTree(md); if err != nil { return fmt.Errorf("render failed: %w", err) }

Prevention

When it happens

Trigger: Raised from parseTemplateKTree when parse.Parse("", markdown, engine.ParseOptions) returns nil, i.e. the markdown produced by a createDocTree template render could not be turned into a parse tree by the Lute engine.

Common situations: Extremely malformed template output after .action{} expansion (e.g. output that truncates mid-block), or a Lute engine/ParseOptions misconfiguration introduced by a kernel change.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at kernel/model/template_export_attrs.go:34

package model

import (
	"bytes"
	"errors"
	"strings"

	"github.com/88250/lute/ast"
	"github.com/88250/lute/parse"
)

const templateDocumentAttributeMarker = "siyuan-template-doc-attrs-v1"

func parseTemplateKTree(markdown []byte) (*parse.Tree, error) {
	engine := NewLute()
	tree := parse.Parse("", markdown, engine.ParseOptions)
	if tree == nil {
		return nil, errors.New("parse template tree failed")
	}
	normalizeTree(tree)
	if err := applyTemplateDocumentAttributes(tree); err != nil {
		return nil, err
	}
	return tree, nil
}

// 独立文档属性保留在原位置求值,渲染完成后再合并到根节点。
func applyTemplateDocumentAttributes(tree *parse.Tree) error {
	for node := tree.Root.FirstChild; node != nil; {
		next := node.Next
		info := node.ChildByType(ast.NodeCodeBlockFenceInfoMarker)
		if node.Type == ast.NodeCodeBlock && info != nil && string(info.CodeBlockInfo) == templateDocumentAttributeMarker {
			code := node.ChildByType(ast.NodeCodeBlockCode)
			if code == nil {
				return errors.New("invalid template document attributes")
			}

View on GitHub (pinned to 9f775e8a12)