{"record":{"id":"b95ec9222b74a4f4","repo":"Hmbown/CodeWhale","slug":"failed-to-parse-original-config-for-comment-merge","errorCode":null,"errorMessage":"failed to parse original config for comment merge; file contents were omitted","messagePattern":"failed to parse original config for comment merge; file contents were omitted","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/config/src/lib.rs","lineNumber":5330,"sourceCode":"            }\n        } else if let Some(nested) = item.as_table_like_mut() {\n            remove_plaintext_api_keys_recursive(nested);\n        }\n    }\n}\n\n/// Merge comments and formatting from an original TOML file into a\n/// freshly serialized document so user annotations (comments, whitespace,\n/// disabled keys) survive config rewrites.\n///\n/// `original_raw` is the raw text of the file before the change; the\n/// function parses it internally with [`toml_edit`] so callers stay free\n/// of that dependency.\npub fn merge_and_preserve_comments(serialized: &str, original_raw: &str) -> Result<String> {\n    let original = original_raw\n        .parse::<toml_edit::DocumentMut>()\n        .map_err(|_| {\n            anyhow::anyhow!(\n                \"failed to parse original config for comment merge; file contents were omitted\"\n            )\n        })?;\n\n    let mut new_doc = serialized.parse::<toml_edit::DocumentMut>().map_err(|_| {\n        anyhow::anyhow!(\n            \"failed to parse serialized config for comment merge; file contents were omitted\"\n        )\n    })?;\n\n    // Reuse the original document’s trailing text (file-footer comments /\n    // disabled keys) so they survive the rewrite.\n    new_doc.set_trailing(original.trailing().clone());\n\n    // Copy the top-level table's decor (document-header comments, whitespace\n    // before the first key) which `toml_edit` stores on the root `Table` itself.\n    *new_doc.as_table_mut().decor_mut() = original.as_table().decor().clone();\n","sourceCodeStart":5312,"sourceCodeEnd":5348,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/8880682c63083a91624de936797efa3ce9e498fd/crates/config/src/lib.rs#L5312-L5348","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before: merging against broken original text\nlet merged = merge_and_preserve_comments(&serialized, &broken_raw)?;\n\n// after: only merge when the original still parses\nlet merged = match broken_raw.parse::<toml_edit::DocumentMut>() {\n    Ok(_) => merge_and_preserve_comments(&serialized, &broken_raw)?,\n    Err(_) => serialized.clone(), // rewrite without preserved comments\n};","handlingStrategy":"fallback","validationCode":"// Only merge comments when the original text still parses:\nlet can_merge = original_raw.parse::<toml_edit::DocumentMut>().is_ok();","typeGuard":null,"tryCatchPattern":"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.","preventionTips":["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"],"tags":["config","toml","parse","comments"],"backgroundTag":null,"analyzedSha":"8880682c63083a91624de936797efa3ce9e498fd","analyzedAt":"2026-08-16T11:31:27.956Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}