libnyanpasu/clash-nyanpasu · error

failed to serialize migrated profiles: {e}

Error message

failed to serialize migrated profiles: {e}

What it means

Once the migrated document passes domain validation, run_clean_schema serializes the typed Profiles back to YAML with serde_yaml::to_string. Serialization of the validated domain model should rarely fail, but if it does (non-string mapping keys, unsupported value types introduced by a model change), the migration aborts before the atomic write.

Source

Thrown at backend/tauri/src/core/migration/modules/profiles.rs:262

    // R15: backup first, then transform (D3: mandatory .bak)
    let bak = path.with_extension("yaml.bak");
    crate::core::migration::fs::atomic_write(&bak, raw.as_bytes())?;

    let migrated = migrate_clean_schema(doc)?;

    // Typed round-trip: the only accepted output is a document the new domain
    // model can load AND validate (design §14.4). Duplicate uids are rejected
    // here by the items deserializer (R13).
    let profiles: nyanpasu_config::profile::Profiles =
        serde_yaml::from_value(Value::Mapping(migrated))
            .map_err(|e| anyhow::anyhow!("clean-schema output rejected by domain model: {e}"))?;
    profiles
        .validate()
        .map_err(|errors| anyhow::anyhow!("clean-schema output failed validation: {errors:?}"))?;

    let body = serde_yaml::to_string(&profiles)
        .map_err(|e| anyhow::anyhow!("failed to serialize migrated profiles: {e}"))?;
    let content = format!("# Profiles Config for Clash Nyanpasu\n\n{body}");
    crate::core::migration::fs::atomic_write(&path, content.as_bytes())?;
    Ok(())
}

fn rollback_clean_schema(ctx: &mut Ctx) -> anyhow::Result<()> {
    let path = ctx.profiles_path();
    let bak = path.with_extension("yaml.bak");
    if !bak.exists() {
        eprintln!("profiles.yaml.bak not found, nothing to roll back");
        return Ok(());
    }
    let raw = std::fs::read(&bak)?;
    crate::core::migration::fs::atomic_write(&path, &raw)
}

#[derive(Debug, thiserror::Error)]
#[error("profiles clean-schema migration failed (uid={uid:?}, field={field_path}): {reason}")]

View on GitHub (pinned to f7dbce2997)

Solutions

  1. Align the nyanpasu-config crate version with the migration code (rebuild so both use the same Profiles model).
  2. Inspect {e} to identify the unserializable field/type and fix the model or transform.
  3. This indicates a code bug, not user data: report it with the error message if it reproduces on a normal profiles file.
  4. As a workaround, restore profiles.yaml.bak to keep the legacy file usable until the bug is fixed.

Example fix

// before
let body = serde_yaml::to_string(&profiles)
    .map_err(|e| anyhow::anyhow!("failed to serialize migrated profiles: {e}"))?;
// after
let body = serde_yaml::to_string(&profiles)
    .with_context(|| "failed to serialize migrated profiles — check nyanpasu-config Profiles model for YAML-incompatible types")?;
Defensive patterns

Strategy: try-catch

Validate before calling

null

Type guard

null

Try / catch

let body = serde_yaml::to_string(&profiles).map_err(|e| {
    eprintln!("serialize failed (code bug, data untouched): {e}");
    anyhow::anyhow!("failed to serialize migrated profiles: {e}")
})?; // restore profiles.yaml.bak; file was not written

Prevention

When it happens

Trigger: serde_yaml::to_string(&profiles) fails — practically only when the domain model contains values serde_yaml cannot represent (e.g. non-string map keys, nested types changed by a model update) or a serde serialize impl on a Profiles field returns an error.

Common situations: Version skew between nyanpasu-config and the migration code where a new field type is not YAML-serializable; a custom Serialize impl returning Err; extremely unusual profile data surviving validation but breaking the serializer.

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 libnyanpasu/clash-nyanpasu@f7dbce2997 (2026-09-08). Data as JSON: /api/errors/82a91165d5146172. Report an issue: GitHub.