{"record":{"id":"21a9c7e085614346","repo":"siyuan-note/siyuan","slug":"cannot-stage-an-empty-tree","errorCode":null,"errorMessage":"cannot stage an empty tree","messagePattern":"cannot stage an empty tree","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/import_obsidian.go","lineNumber":2361,"sourceCode":"\t\tnode.SetIALAttr(\"updated\", util.TimeFromID(node.ID))\n\t\treturn ast.WalkContinue\n\t})\n\ttree.ID = doc.ID\n\ttree.Box = \"\"\n\ttree.Path = doc.TargetPath\n\ttree.HPath = doc.HPath\n\ttree.Root.Box = \"\"\n\ttree.Root.Path = doc.TargetPath\n\ttree.Root.Spec = treenode.CurrentSpec\n\ttree.Root.SetIALAttr(\"id\", doc.ID)\n\ttree.Root.SetIALAttr(\"title\", doc.Title)\n\ttree.Root.SetIALAttr(\"updated\", util.TimeFromID(doc.ID))\n\ttree.Root.RemoveIALAttrsByPrefix(\"custom-\")\n}\n\nfunc writeObsidianTempTree(docsTemp string, tree *parse.Tree) error {\n\tif tree == nil || tree.Root == nil {\n\t\treturn errors.New(\"cannot stage an empty tree\")\n\t}\n\tif tree.Root.FirstChild == nil {\n\t\ttree.Root.AppendChild(treenode.NewParagraph(\"\"))\n\t}\n\ttreenode.UpgradeSpec(tree)\n\ttree.Root.SetIALAttr(\"type\", \"doc\")\n\tluteEngine := util.NewLute()\n\trenderer := render.NewJSONRenderer(tree, luteEngine.RenderOptions, luteEngine.ParseOptions)\n\tdata := renderer.Render()\n\tif !json.Valid(data) {\n\t\treturn fmt.Errorf(\"generated tree [%s] is not valid JSON\", tree.HPath)\n\t}\n\tif !util.UseSingleLineSave {\n\t\tvar buffer bytes.Buffer\n\t\tif err := json.Indent(&buffer, data, \"\", \"\\t\"); err != nil {\n\t\t\treturn err\n\t\t}\n\t\tdata = buffer.Bytes()","sourceCodeStart":2343,"sourceCodeEnd":2379,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/import_obsidian.go#L2343-L2379","documentation":"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'.","triggerScenarios":"Calling `writeObsidianTempTree(docsTemp, tree)` with `tree == nil` or `tree.Root == nil`, e.g. after a conversion step returned nil for a document.","commonSituations":"An upstream conversion failure was swallowed and the nil tree was passed through; a skipped/empty document slipped into the staging loop.","solutions":["Skip documents whose conversion produced a nil tree instead of staging them","Ensure every conversion step returns a valid tree or an explicit error, never a bare nil","Guard the staging call with a nil check on tree and tree.Root"],"exampleFix":"// before\nerr := writeObsidianTempTree(docsTemp, tree)\n// after\nif tree == nil || tree.Root == nil {\n    stats.AddSkipped(\"empty document\")\n    return nil\n}\nerr := writeObsidianTempTree(docsTemp, tree)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"func stageableTree(t *parse.Tree) bool {\n    return t != nil && t.Root != nil\n}","tryCatchPattern":"if err := writeObsidianTempTree(docsTemp, tree); err != nil {\n    stats.AddSkipped(err.Error())\n    return nil\n}","preventionTips":["Ensure every conversion step returns a non-nil tree or an explicit error","Check for nil before staging","Treat skipped documents as an explicit stat rather than silent nils"],"tags":["internal","obsidian-import","null-check"],"backgroundTag":"null-argument","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}