{"record":{"id":"0705134977ef4425","repo":"wasmerio/wasmer","slug":"could-not-parse-yaml-for-format-preserving-edit","errorCode":null,"errorMessage":"could not parse YAML for format-preserving edit: {e}","messagePattern":"could not parse YAML for format-preserving edit: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/cli/src/utils/yaml.rs","lineNumber":50,"sourceCode":"                \"format-preserving app YAML edit produced invalid YAML; \\\n                 rewriting the file without preserving formatting\"\n            );\n            Ok(serde_yaml::to_string(target)?)\n        }\n        Err(err) => {\n            tracing::warn!(\n                ?err,\n                \"cannot format-preserve app YAML edit; \\\n                 rewriting the file without preserving formatting\"\n            );\n            Ok(serde_yaml::to_string(target)?)\n        }\n    }\n}\n\nfn try_format_preserving_edit(text: &str, target: &Value) -> anyhow::Result<String> {\n    let doc = Document::from_str(text)\n        .map_err(|e| anyhow::anyhow!(\"could not parse YAML for format-preserving edit: {e}\"))?;\n    // Second parse, used to detect which keys changed. `yaml_edit` node text\n    // cannot be reparsed for this: it is dedented on the first line only, so\n    // block values are not valid standalone YAML.\n    let original: Value = serde_yaml::from_str(text)\n        .map_err(|e| anyhow::anyhow!(\"could not parse YAML semantically: {e}\"))?;\n\n    match (doc.as_mapping(), target, &original) {\n        (Some(mapping), Value::Mapping(target_mapping), Value::Mapping(original_mapping)) => {\n            merge_into_mapping(&mapping, target_mapping, Some(original_mapping), true)?;\n            let out = doc.to_string();\n            // Restore the leading comment block that `yaml_edit` drops (see\n            // `leading_trivia`).\n            let header = leading_trivia(text);\n            if !header.is_empty() && !out.starts_with(header) {\n                Ok(format!(\"{header}{out}\"))\n            } else {\n                Ok(out)\n            }","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/wasmerio/wasmer/blob/8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5/lib/cli/src/utils/yaml.rs#L32-L68","documentation":"try_format_preserving_edit in lib/cli/src/utils/yaml.rs first parses the YAML text with yaml_edit's `Document::from_str` to preserve comments/formatting while editing app config. If the text is not syntactically valid YAML, the parse fails with this error before any merge is attempted. It is the syntax-level (format-preserving) parse failure, distinct from the semantic serde_yaml parse that follows.","triggerScenarios":"apply_app_config_to_yaml is given a config file whose content is not valid YAML — tabs used for indentation, unclosed quotes/brackets, duplicate anchors, or a file that is not YAML at all (JSON5, TOML, binary).","commonSituations":"Hand-edited app.yaml with tab indentation; a template engine left an unrendered placeholder like {{ .Value }} producing invalid syntax; user pointed the CLI at the wrong file (e.g. a TOML config); merge conflict markers (<<<<<<<) left in the file.","solutions":["Fix the YAML syntax at the reported position from the inner `{e}` (tabs are illegal for indentation — replace with spaces).","Validate the file with an external linter: `yamllint <file>` or paste into a YAML validator.","Remove merge-conflict markers or unrendered template placeholders before running the command.","Confirm the target file is actually a YAML config, not TOML/JSON.","If the file cannot be repaired, regenerate a minimal valid config and re-apply the desired settings."],"exampleFix":"// before (invalid: tab indentation)\n# app.yaml\nkind:\n\\tapp: my-app\n// after\nkind:\n  app: my-app","handlingStrategy":"validation","validationCode":"// validate YAML syntax before handing it to format-preserving edit\nfn validate_yaml(path: &Path) -> anyhow::Result<()> {\n    let text = std::fs::read_to_string(path)?;\n    serde_yaml::from_str::<serde_yaml::Value>(&text)\n        .map_err(|e| anyhow::anyhow!(\"{} is not valid YAML: {}\", path.display(), e))?;\n    anyhow::ensure!(!text.contains('\\t'), \"{} uses tab indentation — not valid YAML\", path.display());\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match apply_app_config_to_yaml(&text, &cfg) {\n    Ok(out) => out,\n    Err(e) if e.to_string().contains(\"could not parse YAML\") => {\n        eprintln!(\"Fix YAML syntax first (no tabs, balanced quotes/brackets): {e}\");\n        std::process::exit(1);\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Run yamllint (or a CI YAML check) on config files before applying changes","Use spaces, never tabs, for YAML indentation","Commit rendered config — don't ship unrendered template placeholders","Resolve merge conflicts in YAML files before running CLI commands"],"tags":["rust","yaml","parsing","config"],"backgroundTag":"yaml-parse-error","analyzedSha":"8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5","analyzedAt":"2026-09-01T23:06:31.009Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}