{"record":{"id":"362174ecc6cd5bdb","repo":"siyuan-note/siyuan","slug":"marshal-master-password-migration-failed-w","errorCode":null,"errorMessage":"marshal master password migration failed: %w","messagePattern":"marshal master password migration failed: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/crypto.go","lineNumber":544,"sourceCode":"\nfunc masterPasswordMigrationPath() string {\n\treturn filepath.Join(util.DataDir, \".siyuan\", \"master-password-migration.json\")\n}\n\nfunc writeMasterPasswordMigration(m *masterPasswordMigration) error {\n\tmasterPasswordMigrationMu.Lock()\n\tdefer masterPasswordMigrationMu.Unlock()\n\treturn writeMasterPasswordMigrationUnlocked(m)\n}\n\nfunc writeMasterPasswordMigrationUnlocked(m *masterPasswordMigration) error {\n\tp := masterPasswordMigrationPath()\n\tif err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {\n\t\treturn fmt.Errorf(\"mkdir master password migration dir failed: %w\", err)\n\t}\n\tdata, err := gulu.JSON.MarshalIndentJSON(m, \"\", \"  \")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"marshal master password migration failed: %w\", err)\n\t}\n\treturn filelock.WriteFile(p, data)\n}\n\nfunc readMasterPasswordMigration() (*masterPasswordMigration, error) {\n\tmasterPasswordMigrationMu.Lock()\n\tdefer masterPasswordMigrationMu.Unlock()\n\treturn readMasterPasswordMigrationUnlocked()\n}\n\nfunc readMasterPasswordMigrationUnlocked() (*masterPasswordMigration, error) {\n\tp := masterPasswordMigrationPath()\n\tif !filelock.IsExist(p) {\n\t\treturn nil, nil\n\t}\n\tdata, err := filelock.ReadFile(p)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"read master password migration failed: %w\", err)","sourceCodeStart":526,"sourceCodeEnd":562,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/crypto.go#L526-L562","documentation":"After ensuring the directory, writeMasterPasswordMigrationUnlocked serializes the masterPasswordMigration struct to indented JSON via gulu.JSON.MarshalIndentJSON. Failure is wrapped as 'marshal master password migration failed'. Marshaling a plain data struct essentially only fails if a field type cannot be represented in JSON or a custom marshaler errors.","triggerScenarios":"gulu.JSON.MarshalIndentJSON(m, \"\", \"  \") returns an error while writing the migration record during writeMasterPasswordMigration or removeMasterPasswordMigrationBox: a non-serializable field (chan/func) added to masterPasswordMigration, or a custom MarshalJSON on a nested type returning an error.","commonSituations":"A developer extended masterPasswordMigration with runtime-only state (locks, channels) that is not JSON-representable; a nested key type gained a faulty custom marshaler; struct/unmarshal target drift after a refactor.","solutions":["Review recent changes to masterPasswordMigration for fields that JSON cannot serialize and move them out of the persisted struct","Add proper json tags or convert unsupported types to serializable forms (string/[]byte/base64)","Fix the error path of any custom MarshalJSON on nested types","Rebuild and re-run the migration flow to confirm the record writes correctly"],"exampleFix":"// before\ntype masterPasswordMigration struct {\n    mu sync.Mutex // not serializable\n    ...\n}\n\n// after\ntype masterPasswordMigration struct {\n    // mutex kept as a field of the manager, not the persisted record\n    ...\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := writeMasterPasswordMigration(m); err != nil && strings.Contains(err.Error(), \"marshal master password migration failed\") {\n    // struct contains non-JSON-serializable fields; abort without corrupting state\n    return err\n}","preventionTips":["Persist only plain data in masterPasswordMigration; keep locks and runtime handles in a separate manager struct","Round-trip the struct through gulu.JSON.MarshalIndentJSON/UnmarshalJSON in a unit test","Tag all persisted fields explicitly with json tags"],"tags":["go","json","serialization","migration"],"backgroundTag":"json-marshal-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"}