{"record":{"id":"d52471af38115b48","repo":"siyuan-note/siyuan","slug":"document-not-found-or-empty","errorCode":null,"errorMessage":"document not found or empty","messagePattern":"document not found or empty","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/cli/cmd/block.go","lineNumber":168,"sourceCode":"\t\tif mode == \"\" {\n\t\t\tmode = \"md\"\n\t\t}\n\t\tfmt.Print(model.GetBlockKramdown(id, mode))\n\t\treturn nil\n\t},\n}\n\nvar blockStatCmd = &cobra.Command{\n\tUse:   \"stat --id <id>\",\n\tShort: \"Get block content statistics\",\n\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\tid, _ := cmd.Flags().GetString(\"id\")\n\t\tif id == \"\" {\n\t\t\treturn fmt.Errorf(\"--id is required\")\n\t\t}\n\t\tstat := filesys.StatTree(id)\n\t\tif stat == nil {\n\t\t\treturn fmt.Errorf(\"document not found or empty\")\n\t\t}\n\t\tswitch outputFormat {\n\t\tcase \"json\":\n\t\t\tdata, _ := json.MarshalIndent(stat, \"\", \"  \")\n\t\t\tfmt.Println(string(data))\n\t\tdefault:\n\t\t\tfmt.Printf(\"Characters: %d\\n\", stat.RuneCount)\n\t\t\tfmt.Printf(\"Words:      %d\\n\", stat.WordCount)\n\t\t\tfmt.Printf(\"Blocks:     %d\\n\", stat.BlockCount)\n\t\t\tfmt.Printf(\"Links:      %d\\n\", stat.LinkCount)\n\t\t\tfmt.Printf(\"Images:     %d\\n\", stat.ImageCount)\n\t\t\tfmt.Printf(\"Refs:       %d\\n\", stat.RefCount)\n\t\t}\n\t\treturn nil\n\t},\n}\n\n// ─── Write ─────────────────────────────────────────────────────────────────────","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/cli/cmd/block.go#L150-L186","documentation":"Thrown by `block stat` when `filesys.StatTree(id)` returns nil. `StatTree` returns nil if the tree cannot be loaded — `LoadTrees` yields no tree (block not in blocktree, or the .sy file is unreadable/missing). This is a data-state error, not an argument error: `--id` was present but resolves to nothing.","triggerScenarios":"Passing an `--id` that is not a registered block in `blocktrees`, belongs to a notebook that is not opened/mounted, points to an encrypted notebook whose key is unloaded, or whose `.sy` file was deleted/corrupted on disk.","commonSituations":"Workspace not fully mounted at CLI time; referencing a block ID from another workspace; encrypted notebook locked; the document was deleted but an old ID was cached; running the CLI before the kernel finished indexing.","solutions":["Verify the ID exists: `siyuan-kernel block get --id <id>` or query `siyuan-kernel sql \"SELECT * FROM blocks WHERE id='<id>'\"`","Ensure the notebook containing the block is mounted/opened in the active workspace","For encrypted notebooks, unlock them first so `LoadTrees` can read the `.sy` files","If the file was moved/deleted, reindex or use the correct ID"],"exampleFix":"// before\nsiyuan-kernel block stat --id 00000000000000-nonexist\n// after\nsiyuan-kernel block stat --id 20260605100657-v080a4j  # confirmed via `sql` first","handlingStrategy":"validation","validationCode":"// Pre-check existence before calling `block stat` to avoid the nil-tree error.\n// In a Go caller of the model layer:\nbt := treenode.GetBlockTree(docID)\nif bt == nil {\n    return fmt.Errorf(\"document %s not found in blocktree\", docID)\n}\nstat := filesys.StatTree(docID)\nif stat == nil {\n    return fmt.Errorf(\"document %s could not be loaded\", docID)\n}","typeGuard":"// isLoadableDoc returns true when a tree can be resolved for the given root id.\nfunc isLoadableDoc(id string) bool {\n    if strings.TrimSpace(id) == \"\" {\n        return false\n    }\n    return treenode.GetBlockTree(id) != nil\n}","tryCatchPattern":null,"preventionTips":["Resolve the blocktree entry before loading the tree to get a clearer nil cause","Ensure notebooks are mounted and encrypted ones unlocked before batch stats","Run existence checks (`sql`) once per ID and cache the result"],"tags":["cli","block","data-state","stat","go"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}