Hmbown/CodeWhale · error · anyhow::Error
failed to parse original config for comment merge; file cont
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 file text with toml_edit so user comments, whitespace, and trailing notes survive a config rewrite. This error says the original_raw argument is not valid TOML; the merge is refused before any output is produced.
Source
Thrown at crates/config/src/lib.rs:5330
}
} 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 8880682c63)
Solutions
- Validate and repair the original file before the rewrite; this function intentionally refuses to guess
- Remove duplicate keys; toml_edit is strict about them
- If the original is unrecoverable, write the serialized document without comment merging and accept losing comments
Example fix
// before: merging against broken original text
let merged = merge_and_preserve_comments(&serialized, &broken_raw)?;
// after: only merge when the original still parses
let merged = match broken_raw.parse::<toml_edit::DocumentMut>() {
Ok(_) => merge_and_preserve_comments(&serialized, &broken_raw)?,
Err(_) => serialized.clone(), // rewrite without preserved comments
}; Defensive patterns
Strategy: fallback
Validate before calling
// Only merge comments when the original text still parses: let can_merge = original_raw.parse::<toml_edit::DocumentMut>().is_ok();
Try / catch
Wrap the merge call and on Err fall back to writing the serialized document without comment preservation. A failed merge must not block the save: prefer losing comments over losing the config change.
Prevention
- Capture original_raw immediately before the rewrite, not at startup, to shrink the window where another process can corrupt it
- Run a TOML lint in CI for any committed config fixtures
When it happens
Trigger: Calling the public merge_and_preserve_comments with original_raw that fails toml_edit parsing (bad syntax or duplicate keys), typically because the on-disk file was already broken when a save was attempted or a test fed hand-written original text.
Common situations: A save path that captured original_raw at startup while another process corrupted the file before the write; embedders/tests constructing the original string by hand with a typo.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- failed to parse config at {}; file contents were omitted
- failed to parse config at {}; file contents were omitted
- failed to parse config TOML while removing plaintext API key
- failed to parse serialized config for comment merge; file co
- failed to parse permissions at {}; file contents were omitte
AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16).
Data as JSON: /api/errors/b95ec9222b74a4f4.
Report an issue: GitHub.