{"record":{"id":"6dd1c51099d4e011","repo":"nikivdev/code","slug":"already-exists-refusing-to-overwrite","errorCode":null,"errorMessage":"{} already exists; refusing to overwrite","messagePattern":"(.+?) already exists; refusing to overwrite","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/init.rs","lineNumber":69,"sourceCode":"#max_local_gate_seconds = 20\n\"#;\n\npub(crate) fn write_template(path: &Path) -> Result<()> {\n    if let Some(parent) = path.parent() {\n        if !parent.as_os_str().is_empty() {\n            fs::create_dir_all(parent)\n                .with_context(|| format!(\"failed to create directory {}\", parent.display()))?;\n        }\n    }\n\n    fs::write(path, TEMPLATE).with_context(|| format!(\"failed to write {}\", path.display()))?;\n    Ok(())\n}\n\npub fn run(opts: InitOpts) -> Result<()> {\n    let target = resolve_path(opts.path);\n    if target.exists() {\n        bail!(\"{} already exists; refusing to overwrite\", target.display());\n    }\n\n    write_template(&target)?;\n    println!(\"created {}\", target.display());\n    Ok(())\n}\n\nfn resolve_path(path: Option<PathBuf>) -> PathBuf {\n    match path {\n        Some(p) if p.is_absolute() => p,\n        Some(p) => std::env::current_dir()\n            .unwrap_or_else(|_| PathBuf::from(\".\"))\n            .join(p),\n        None => std::env::current_dir()\n            .unwrap_or_else(|_| PathBuf::from(\".\"))\n            .join(\"flow.toml\"),\n    }\n}","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/init.rs#L51-L87","documentation":"init::run resolves the target path for a new template file and refuses to proceed if a file or directory already exists at that location, to avoid clobbering user data. write_template is only called on a non-existent path.","triggerScenarios":"Running the init command with a path (after resolve_path) that already exists on disk — e.g. re-running init in the same directory, or pointing at an existing config file.","commonSituations":"Re-running `init` after a previous successful run; a leftover file from a failed previous attempt; passing '.' or an existing config filename by habit.","solutions":["Delete or rename the existing file/directory at the target path, then re-run init.","Choose a different target path for the new template.","If the existing file is a prior valid template, keep it — no init needed.","Inspect `target.display()` from the error to confirm exactly which path is blocking."],"exampleFix":"// before\nf init ./existing-config\n// after\nrm ./existing-config && f init ./existing-config  # or pick a new path","handlingStrategy":"validation","validationCode":"let target = resolve_path(path);\nif target.exists() {\n    // pick another path or remove the file first\n    eprintln!(\"{} exists; init skipped\", target.display());\n} else {\n    init::run(InitOpts { path: path.into() })?;\n}","typeGuard":null,"tryCatchPattern":"match init::run(opts) {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"refusing to overwrite\") => {\n        eprintln!(\"target exists; choose a new path or delete the old file\");\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Check Path::exists() on the target before running init","Treat a second init run as a no-op — check for the template first","Clean up partial files from failed previous runs before re-running"],"tags":["filesystem","overwrite-protection","input-validation"],"backgroundTag":"file-already-exists","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}