{"record":{"id":"3ef44b9cc8bc6325","repo":"siyuan-note/siyuan","slug":"clone-attribute-view-s-view-failed","errorCode":null,"errorMessage":"clone attribute view [%s] view failed","messagePattern":"clone attribute view \\[(.+?)\\] view failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template.go","lineNumber":627,"sourceCode":"\t\t\treturn view\n\t\t}\n\t}\n\tview, _ := attrView.GetFirstView()\n\treturn view\n}\n\nfunc copyTemplateAttributeView(source *av.AttributeView) (ret *templateAttributeViewCopy, err error) {\n\ttarget := source.Clone()\n\tif nil == target {\n\t\treturn nil, fmt.Errorf(\"clone attribute view [%s] failed\", source.ID)\n\t}\n\tif len(source.Views) != len(target.Views) {\n\t\treturn nil, fmt.Errorf(\"clone attribute view [%s] views failed\", source.ID)\n\t}\n\tcopiedViewIDs := map[string]string{}\n\tfor i, sourceView := range source.Views {\n\t\tif nil == sourceView || nil == target.Views[i] {\n\t\t\treturn nil, fmt.Errorf(\"clone attribute view [%s] view failed\", source.ID)\n\t\t}\n\t\tcopiedViewIDs[sourceView.ID] = target.Views[i].ID\n\t}\n\treturn &templateAttributeViewCopy{target: target, copiedViewIDs: copiedViewIDs}, nil\n}\n\nfunc prepareTemplateAttributeViews(tree *parse.Tree, preview bool) (plans map[*ast.Node]*templateAttributeViewPlan,\n\tcopies []*templateAttributeViewCopy, err error) {\n\tplans = map[*ast.Node]*templateAttributeViewPlan{}\n\treferenceSources := map[string]*av.AttributeView{}\n\tcopySources := map[string]*av.AttributeView{}\n\tcopyBySourceID := map[string]*templateAttributeViewCopy{}\n\tboxID := templateAttributeViewBoxID(tree)\n\tast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {\n\t\tif !entering || ast.NodeAttributeView != n.Type {\n\t\t\treturn ast.WalkContinue\n\t\t}\n","sourceCodeStart":609,"sourceCodeEnd":645,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template.go#L609-L645","documentation":"During the copy, each index i must yield both a non-nil source view and a non-nil cloned view; otherwise the copiedViewIDs mapping cannot be built. A nil entry aborts with 'clone attribute view [%s] view failed'. This guarantees the copy's view-ID remapping (source view ID -> cloned view ID) is complete before the cloned attribute view is used.","triggerScenarios":"Copy-mode template rendering where source.Views or target.Views contains a nil element at some index despite matching lengths — usually a partially serialized view or a Clone bug producing nil placeholders.","commonSituations":"Corrupted attribute view JSON with null view entries, upstream av.Clone changes leaving nil slots, or templates referencing databases damaged by interrupted writes.","solutions":["Open and re-save the source database in the UI to normalize its views","Remove null view entries from the database's storage/av JSON (with a backup) or restore from history","Update SiYuan if Clone behavior changed recently; report a reproducible case","Recreate the database and regenerate the template"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"const av = await getAttrView(avID);\nif (av.views.some(v => v == null)) throw new Error(\"source attribute view contains null views\");","typeGuard":"function hasNoNullViews(views: Array<View | null>): views is View[] {\n  return views.every((v): v is View => v != null);\n}","tryCatchPattern":"try {\n  await insertTemplate(path);\n} catch (e) {\n  if (String(e).includes(\"view failed\")) {\n    await repairNullViews(avID); // remove null entries or restore from history\n  }\n}","preventionTips":["Never write null entries into storage/av JSON","Back up before editing database files externally","Keep the kernel from being killed mid-write (clean shutdowns) to avoid partial serialization"],"tags":["attribute-view","clone","nil"],"backgroundTag":"internal-invariant-violation","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"}