{"record":{"id":"4db7218a0fb0ffbc","repo":"siyuan-note/siyuan","slug":"unmarshal-box-document-metadata-failed-w","errorCode":null,"errorMessage":"unmarshal box document metadata failed: %w","messagePattern":"unmarshal box document metadata failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/box_doc.go","lineNumber":71,"sourceCode":"func boxDocPath(boxID string) string {\n\tif \"\" == boxID {\n\t\treturn \"\"\n\t}\n\treturn \"/\" + boxID + \".sy\"\n}\n\nfunc readBoxDocID(boxID string) (ret string, err error) {\n\tdata, err := filelock.ReadFile(boxDocMetaPath(boxID))\n\tif err != nil {\n\t\tif errors.Is(err, os.ErrNotExist) {\n\t\t\terr = nil\n\t\t}\n\t\treturn\n\t}\n\n\tmeta := &boxDocMeta{}\n\tif err = gulu.JSON.UnmarshalJSON(data, meta); err != nil {\n\t\treturn \"\", fmt.Errorf(\"unmarshal box document metadata failed: %w\", err)\n\t}\n\tif boxDocMetaSpec != meta.Spec {\n\t\treturn \"\", fmt.Errorf(\"unsupported box document metadata spec [%d]\", meta.Spec)\n\t}\n\tif !ast.IsNodeIDPattern(meta.BoxDocID) {\n\t\treturn \"\", fmt.Errorf(\"invalid box document ID [%s]\", meta.BoxDocID)\n\t}\n\tif boxID != meta.BoxDocID {\n\t\treturn \"\", fmt.Errorf(\"box document ID [%s] does not match box ID [%s]\", meta.BoxDocID, boxID)\n\t}\n\treturn boxID, nil\n}\n\nfunc writeBoxDocID(boxID string) error {\n\tmeta := &boxDocMeta{Spec: boxDocMetaSpec, BoxDocID: boxID}\n\tdata, err := gulu.JSON.MarshalIndentJSON(meta, \"\", \"  \")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"marshal box document metadata failed: %w\", err)","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/box_doc.go#L53-L89","documentation":"readBoxDocID parses the notebook's .siyuan/boxDoc.json metadata file; this error is returned when the file content is not valid JSON or does not decode into the boxDocMeta structure (spec/boxDocID fields). It wraps the underlying unmarshal error. A corrupt or truncated boxDoc.json makes the notebook's document-node identity unreadable.","triggerScenarios":"ensureBoxDoc0 or TestBoxDocMetadataMatchesBoxID calls readBoxDocID on a notebook whose .siyuan/boxDoc.json contains invalid JSON — e.g. empty file, partial write, BOM, or non-JSON content.","commonSituations":"Interrupted write leaving a truncated file; user or tooling editing boxDoc.json by hand and breaking syntax; file sync conflict markers written into the file; encoding issues after manual copy.","solutions":["Inspect .siyuan/boxDoc.json for the notebook and fix or remove the invalid content; it is regenerated with {\"spec\":1,\"boxDocID\":\"<boxID>\"}","Delete the corrupt boxDoc.json and let ensureBoxDoc0 rewrite it via writeBoxDocID (the file is optional; absence is tolerated)","Restore the file from backup or resync the workspace"],"exampleFix":"// before: corrupt file\n\"{\"spec\": 1, \"boxDocID\": \"20240101120000-abcd  // truncated\n// after: valid boxDoc.json\n{\n  \"spec\": 1,\n  \"boxDocID\": \"20240101120000-abcdefg\"\n}","handlingStrategy":"try-catch","validationCode":"data, _ := os.ReadFile(filepath.Join(util.DataDir, boxID, \".siyuan\", \"boxDoc.json\"))\nvar probe map[string]any\nif len(data) > 0 && json.Unmarshal(data, &probe) != nil {\n    // corrupt boxDoc.json: delete it so it is regenerated\n}","typeGuard":null,"tryCatchPattern":"id, err := readBoxDocID(boxID)\nif err != nil && strings.Contains(err.Error(), \"unmarshal box document metadata failed\") {\n    // corrupt JSON: remove boxDoc.json and let ensureBoxDoc0 rewrite it\n}","preventionTips":["Do not edit boxDoc.json manually","Ensure sync tools do not leave conflict markers in .siyuan files","Verify file integrity after interrupted writes or crashes"],"tags":["json","parsing","notebook","corrupt-file"],"backgroundTag":"json-unmarshal-failed","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"}