siyuan-note/siyuan · error

marshal master password migration failed

Error message

marshal master password migration failed: %w

What it means

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.

Solutions

  1. Review recent changes to masterPasswordMigration for fields that JSON cannot serialize and move them out of the persisted struct
  2. Add proper json tags or convert unsupported types to serializable forms (string/[]byte/base64)
  3. Fix the error path of any custom MarshalJSON on nested types
  4. Rebuild and re-run the migration flow to confirm the record writes correctly

Example fix

// before
type masterPasswordMigration struct {
    mu sync.Mutex // not serializable
    ...
}

// after
type masterPasswordMigration struct {
    // mutex kept as a field of the manager, not the persisted record
    ...
}
Defensive patterns

Strategy: try-catch

Try / catch

if err := writeMasterPasswordMigration(m); err != nil && strings.Contains(err.Error(), "marshal master password migration failed") {
    // struct contains non-JSON-serializable fields; abort without corrupting state
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/362174ecc6cd5bdb. Report an issue: GitHub.

Appendix: source

Thrown at kernel/model/crypto.go:544

func masterPasswordMigrationPath() string {
	return filepath.Join(util.DataDir, ".siyuan", "master-password-migration.json")
}

func writeMasterPasswordMigration(m *masterPasswordMigration) error {
	masterPasswordMigrationMu.Lock()
	defer masterPasswordMigrationMu.Unlock()
	return writeMasterPasswordMigrationUnlocked(m)
}

func writeMasterPasswordMigrationUnlocked(m *masterPasswordMigration) error {
	p := masterPasswordMigrationPath()
	if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
		return fmt.Errorf("mkdir master password migration dir failed: %w", err)
	}
	data, err := gulu.JSON.MarshalIndentJSON(m, "", "  ")
	if err != nil {
		return fmt.Errorf("marshal master password migration failed: %w", err)
	}
	return filelock.WriteFile(p, data)
}

func readMasterPasswordMigration() (*masterPasswordMigration, error) {
	masterPasswordMigrationMu.Lock()
	defer masterPasswordMigrationMu.Unlock()
	return readMasterPasswordMigrationUnlocked()
}

func readMasterPasswordMigrationUnlocked() (*masterPasswordMigration, error) {
	p := masterPasswordMigrationPath()
	if !filelock.IsExist(p) {
		return nil, nil
	}
	data, err := filelock.ReadFile(p)
	if err != nil {
		return nil, fmt.Errorf("read master password migration failed: %w", err)

View on GitHub (pinned to 9f775e8a12)