{"record":{"id":"b2247df6d3429be1","repo":"Hmbown/CodeWhale","slug":"error-rolled-back-to-the-pre-import-document","errorCode":null,"errorMessage":"{error:#}; rolled back to the pre-import document","messagePattern":"(.+?); rolled back to the pre-import document","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/cli/src/config_bundles.rs","lineNumber":1090,"sourceCode":"    let target = store.path().to_path_buf();\n    let original_config = store.config.clone();\n    let backup_path = if target\n        .try_exists()\n        .with_context(|| format!(\"checking config target {}\", target.display()))?\n    {\n        Some(create_collision_safe_backup(&target)?)\n    } else {\n        None\n    };\n\n    let mut target_written = false;\n    let apply_result = apply(candidate, store, &mut target_written);\n    if let Err(error) = apply_result {\n        store.config = original_config;\n        let rollback = rollback_import_target(&target, backup_path.as_deref(), target_written)\n            .and_then(|()| store.reload());\n        match rollback {\n            Ok(()) => bail!(\"{error:#}; rolled back to the pre-import document\"),\n            Err(_) => bail!(\n                \"{error:#}; ROLLBACK FAILED — the pre-import document is preserved at {}\",\n                backup_path\n                    .as_deref()\n                    .map(Path::display)\n                    .map(|path| path.to_string())\n                    .unwrap_or_else(\n                        || \"<no prior file; remove the new target manually>\".to_string()\n                    )\n            ),\n        }\n    }\n\n    Ok(ImportReceipt {\n        plan,\n        backup_path,\n        target,\n    })","sourceCodeStart":1072,"sourceCodeEnd":1108,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/cli/src/config_bundles.rs#L1072-L1108","documentation":"During apply_prepared_bundle, if the apply callback fails, the library rolls back: it restores the in-memory original config and attempts rollback_import_target plus store.reload(). If rollback succeeds, the original apply error is re-raised with the suffix \"; rolled back to the pre-import document\", telling the user the import failed atomically and nothing was changed.","triggerScenarios":"The user-supplied apply callback (or CAS-guarded store write) returns Err during run_import/apply_bundle; rollback of the target file and store reload both succeed, so this compound error is raised instead of the bare apply error.","commonSituations":"Importing a bundle whose values fail downstream validation in the apply step (e.g. invalid provider slot overrides); a CAS conflict with a concurrent editor that surfaced mid-apply; a migration callback erroring during route import.","solutions":["Read the leading part of the message ({error:#}) — that is the real cause; fix it in the bundle.","Retry the import after correcting the bundle; the pre-import document is intact so no manual restore is needed.","Dry-run the import first to catch plan-level problems before apply.","If the cause is a concurrent edit, re-read the current config and re-attempt when no other process is editing."],"exampleFix":"// before\napply = writes invalid provider slot -> Err -> rolled back\n// after\ndry-run import first, fix the offending bundle entry, then apply cleanly","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match run_import(&bundle, &store, scope) {\n    Err(e) if e.to_string().ends_with(\"rolled back to the pre-import document\") => {\n        // safe: nothing changed; fix the root cause (prefix of the message) and retry\n        eprintln!(\"import failed cleanly: {e:#}\");\n    }\n    other => other?,\n}","preventionTips":["Dry-run first so apply-stage failures are caught before any write attempt.","Fix the root cause in the leading part of the message, not the rollback suffix.","Avoid concurrent edits while importing; ConfigStore CAS rejects racing writes."],"tags":["transaction","rollback","import","config-bundles"],"backgroundTag":"invalid-state-transition","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}