siyuan-note/siyuan · error

cannot stage an empty tree

Error message

cannot stage an empty tree

What it means

writeObsidianTempTree stages a converted document tree to the temporary docs directory as a .sy file. A nil tree or nil root is invalid staging input, and the function rejects it with 'cannot stage an empty tree'.

Solutions

  1. Skip documents whose conversion produced a nil tree instead of staging them
  2. Ensure every conversion step returns a valid tree or an explicit error, never a bare nil
  3. Guard the staging call with a nil check on tree and tree.Root

Example fix

// before
err := writeObsidianTempTree(docsTemp, tree)
// after
if tree == nil || tree.Root == nil {
    stats.AddSkipped("empty document")
    return nil
}
err := writeObsidianTempTree(docsTemp, tree)
Defensive patterns

Strategy: type-guard

Type guard

func stageableTree(t *parse.Tree) bool {
    return t != nil && t.Root != nil
}

Try / catch

if err := writeObsidianTempTree(docsTemp, tree); err != nil {
    stats.AddSkipped(err.Error())
    return nil
}

Prevention

When it happens

Trigger: Calling `writeObsidianTempTree(docsTemp, tree)` with `tree == nil` or `tree.Root == nil`, e.g. after a conversion step returned nil for a document.

Common situations: An upstream conversion failure was swallowed and the nil tree was passed through; a skipped/empty document slipped into the staging loop.

Related errors


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

Appendix: source

Thrown at kernel/model/import_obsidian.go:2361

		node.SetIALAttr("updated", util.TimeFromID(node.ID))
		return ast.WalkContinue
	})
	tree.ID = doc.ID
	tree.Box = ""
	tree.Path = doc.TargetPath
	tree.HPath = doc.HPath
	tree.Root.Box = ""
	tree.Root.Path = doc.TargetPath
	tree.Root.Spec = treenode.CurrentSpec
	tree.Root.SetIALAttr("id", doc.ID)
	tree.Root.SetIALAttr("title", doc.Title)
	tree.Root.SetIALAttr("updated", util.TimeFromID(doc.ID))
	tree.Root.RemoveIALAttrsByPrefix("custom-")
}

func writeObsidianTempTree(docsTemp string, tree *parse.Tree) error {
	if tree == nil || tree.Root == nil {
		return errors.New("cannot stage an empty tree")
	}
	if tree.Root.FirstChild == nil {
		tree.Root.AppendChild(treenode.NewParagraph(""))
	}
	treenode.UpgradeSpec(tree)
	tree.Root.SetIALAttr("type", "doc")
	luteEngine := util.NewLute()
	renderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions, luteEngine.ParseOptions)
	data := renderer.Render()
	if !json.Valid(data) {
		return fmt.Errorf("generated tree [%s] is not valid JSON", tree.HPath)
	}
	if !util.UseSingleLineSave {
		var buffer bytes.Buffer
		if err := json.Indent(&buffer, data, "", "\t"); err != nil {
			return err
		}
		data = buffer.Bytes()

View on GitHub (pinned to 9f775e8a12)