{"record":{"id":"aa2ba0aa7b2f5c77","repo":"siyuan-note/siyuan","slug":"read-document-s-failed-w","errorCode":null,"errorMessage":"read document [%s] failed: %w","messagePattern":"read document \\[(.+?)\\] failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/history.go","lineNumber":994,"sourceCode":"\tluteEngine := util.NewLute()\n\tfor _, file := range files {\n\t\tif err = generateDocHistoryFile(box.ID, file, historyDir, luteEngine); err != nil {\n\t\t\tlogging.LogErrorf(\"generate history failed: %s\", err)\n\t\t\treturn\n\t\t}\n\t}\n\n\tindexHistoryDir(filepath.Base(historyDir), util.NewLute())\n\treturn\n}\n\nfunc generateDocHistoryFile(boxID, file, historyDir string, luteEngine *lute.Lute) error {\n\tdata, err := filelock.ReadFile(file)\n\tif err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\treturn nil\n\t\t}\n\t\treturn fmt.Errorf(\"read document [%s] failed: %w\", file, err)\n\t}\n\n\treturn generateDocHistoryFromData(boxID, file, historyDir, data, luteEngine)\n}\n\nfunc generateDocHistoryFromData(boxID, file, historyDir string, data []byte, luteEngine *lute.Lute) error {\n\thistoryPath := filepath.Join(historyDir, boxID, strings.TrimPrefix(file, filepath.Join(util.DataDir, boxID)))\n\tif err := os.MkdirAll(filepath.Dir(historyPath), 0755); err != nil {\n\t\treturn err\n\t}\n\tif err := gulu.File.WriteFileSafer(historyPath, data, 0644); err != nil {\n\t\treturn err\n\t}\n\tif !strings.HasSuffix(file, \".sy\") {\n\t\treturn nil\n\t}\n\n\ttree, err := loadTreeByData(file, data, luteEngine)","sourceCodeStart":976,"sourceCodeEnd":1012,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/history.go#L976-L1012","documentation":"generateDocHistoryFile wraps the underlying OS error returned by filelock.ReadFile when it cannot read the .sy document file for which a history snapshot is being generated. The wrapper preserves the original error (e.g. permission denied, I/O error) while adding the failing file path. Note that os.IsNotExist is deliberately treated as a benign no-op (return nil), so this error only fires for real read failures of an existing file.","triggerScenarios":"generateDocHistoryFile -> filelock.ReadFile(file) fails with an error other than os.IsNotExist: the file exists but is unreadable (permissions changed mid-operation), locked by another process with a hard error, on a failing disk, or the path became invalid between the directory listing and the read (race with file deletion on platforms where ReadFile reports a non-ENOENT error).","commonSituations":"Sync or external tools altered file permissions inside data/<box>/; antivirus or backup software holds the file; disk full/corrupt sectors; the file was removed by another goroutine (sync/rollback) between listing and reading; network/CloudDrive-synced workspace briefly made files unavailable with an error other than not-exist.","solutions":["Check the wrapped cause (%w) in the error message; it names the actual OS failure - fix that root cause first (permissions, disk, lock).","Run the kernel process with an account that has read access to the workspace data directory.","Close programs that lock workspace files (backup/AV/sync clients) or exclude the workspace from them.","Verify the workspace is on a healthy local disk, not an unreliable network mount.","Re-trigger history generation; if the file is now gone, the code treats not-exist as success."],"exampleFix":"// caller in generateDocHistory0\nif err := generateDocHistoryFile(boxID, file, historyDir, luteEngine); err != nil {\n    logging.LogErrorf(\"generate doc history for [%s] failed: %v\", file, err)\n    // surface the wrapped cause instead of swallowing it\n    return fmt.Errorf(\"history generation aborted: %w\", err)\n}","handlingStrategy":"try-catch","validationCode":"if _, err := os.Stat(file); err != nil {\n    if os.IsNotExist(err) {\n        return nil // benign, matches kernel behavior\n    }\n    return fmt.Errorf(\"document file unreadable before history generation: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := generateDocHistoryFile(boxID, file, historyDir, luteEngine); err != nil {\n    var perr *fs.PathError\n    if errors.As(err, &perr) {\n        logging.LogErrorf(\"read failed for %s: %v\", perr.Path, perr.Err)\n    }\n    return err\n}","preventionTips":["Keep the workspace on a healthy local disk and exclude it from aggressive AV/backup lockers","Run the kernel with sufficient file permissions on data/","Avoid concurrent tools that move/delete workspace files while SiYuan runs"],"tags":["filesystem","file-read","history","error-wrapping"],"backgroundTag":"file-read-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"}