Hmbown/CodeWhale · error

failed to parse original config for comment merge; file…

Error message

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

What it means

merge_and_preserve_comments re-parses the original config text with toml_edit to merge newly serialized settings while keeping comments. If the original raw text cannot be parsed, this error is thrown with contents omitted.

Solutions

  1. Fix the TOML syntax in the original config file, then retry the save
  2. Validate the original config with a TOML parser to locate the error
  3. Restore a valid config from backup so the comment-preserving merge can proceed
  4. Avoid editing the config while the app is running/saving
Defensive patterns

Strategy: validation

Validate before calling

toml::from_str::<toml::Value>(&original_raw).map_err(|e| format!("original config invalid: {e}"))?;

Try / catch

match save_settings() {
    Ok(_) => {},
    Err(e) => eprintln!("{e}; the on-disk config is not valid TOML — fix or restore it before saving"),
}

Prevention

When it happens

Trigger: Calling merge_and_preserve_comments(serialized, original_raw) where original_raw is invalid TOML (e.g. the on-disk config was edited or corrupted between read and merge).

Common situations: User hand-edited the config into invalid TOML, then the app tried to save settings; external sync tools wrote malformed content that the app then tried to merge into.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/b95ec9222b74a4f4. Report an issue: GitHub.

Appendix: source

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

            }
        } else if let Some(nested) = item.as_table_like_mut() {
            remove_plaintext_api_keys_recursive(nested);
        }
    }
}

/// 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();

View on GitHub (pinned to 73e0f67d83)