Hmbown/CodeWhale · error · anyhow::Error

failed to parse serialized config for comment merge; file co

Error message

failed to parse serialized config for comment merge; file contents were omitted

What it means

merge_and_preserve_comments also parses the freshly serialized document it was given. This arm failing means the serialized input itself is invalid TOML: an internal invariant violation, because the caller just produced it via TOML serialization. It does not indicate anything broken on disk.

Source

Thrown at crates/config/src/lib.rs:5336

/// Merge comments and formatting from an original TOML file into a
/// freshly serialized document so user annotations (comments, whitespace,
/// disabled keys) survive config rewrites.
///
/// `original_raw` is the raw text of the file before the change; the
/// function parses it internally with [`toml_edit`] so callers stay free
/// of that dependency.
pub fn merge_and_preserve_comments(serialized: &str, original_raw: &str) -> Result<String> {
    let original = original_raw
        .parse::<toml_edit::DocumentMut>()
        .map_err(|_| {
            anyhow::anyhow!(
                "failed to parse original config for comment merge; file contents were omitted"
            )
        })?;

    let mut new_doc = serialized.parse::<toml_edit::DocumentMut>().map_err(|_| {
        anyhow::anyhow!(
            "failed to parse serialized config for comment merge; file contents were omitted"
        )
    })?;

    // Reuse the original document’s trailing text (file-footer comments /
    // disabled keys) so they survive the rewrite.
    new_doc.set_trailing(original.trailing().clone());

    // Copy the top-level table's decor (document-header comments, whitespace
    // before the first key) which `toml_edit` stores on the root `Table` itself.
    *new_doc.as_table_mut().decor_mut() = original.as_table().decor().clone();

    merge_decor_table(new_doc.as_table_mut(), original.as_table());

    Ok(new_doc.to_string())
}

/// Recursively copy `decor` (prefix/suffix comments and whitespace) from

View on GitHub (pinned to 8880682c63)

Solutions

  1. If you call this API directly, ensure the first argument comes from real TOML serialization (toml::to_string or DocumentMut::to_string)
  2. If the argument came from Codewhale's own serializer, file a bug describing the operation that produced it
  3. Do not patch the serialized text with string surgery; regenerate it
Defensive patterns

Strategy: validation

Validate before calling

// Prove the serialized text parses before merging:
fn serialized_is_valid(serialized: &str) -> bool {
    serialized.parse::<toml_edit::DocumentMut>().is_ok()
}

Try / catch

This arm indicates a bug in the producer of the serialized text; catch it, assert loudly (or file an issue), and never write the malformed output to disk.

Prevention

When it happens

Trigger: A custom caller passes a hand-constructed or corrupted serialized string that is not valid TOML, or a serializer bug produced invalid output. Normal save flows cannot reach this arm.

Common situations: Mostly embedders and tests that build the serialized text themselves; effectively a guard against programmer error rather than user state.

Understand the failure class

Related errors


AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16). Data as JSON: /api/errors/2e32a42ceda3c26e. Report an issue: GitHub.