{"record":{"id":"c2a36b8d215cea77","repo":"nikivdev/code","slug":"already-exists-use-force-to-overwrite","errorCode":null,"errorMessage":"{} already exists (use --force to overwrite)","messagePattern":"(.+?) already exists \\(use --force to overwrite\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/registry.rs","lineNumber":259,"sourceCode":"        bail!(\"download failed ({})\", response.status());\n    }\n    let bytes = response.bytes().context(\"failed to read download\")?;\n\n    if !opts.no_verify {\n        if let Some(expected) = target_entry.sha256.get(&bin) {\n            let actual = sha256_bytes(&bytes);\n            if expected != &actual {\n                bail!(\"checksum mismatch for {}\", bin);\n            }\n        }\n    }\n\n    let bin_dir = opts.bin_dir.clone().unwrap_or_else(default_bin_dir);\n    fs::create_dir_all(&bin_dir)\n        .with_context(|| format!(\"failed to create {}\", bin_dir.display()))?;\n    let dest = bin_dir.join(&bin);\n    if dest.exists() && !opts.force {\n        bail!(\n            \"{} already exists (use --force to overwrite)\",\n            dest.display()\n        );\n    }\n\n    let mut temp = NamedTempFile::new_in(&bin_dir)\n        .with_context(|| format!(\"failed to create temp file in {}\", bin_dir.display()))?;\n    temp.write_all(&bytes)?;\n    temp.flush()?;\n    persist_with_permissions(temp, &dest)?;\n\n    println!(\"Installed {} to {}\", bin, dest.display());\n    if !path_in_env(&bin_dir) {\n        println!(\"Add {} to PATH to use it everywhere.\", bin_dir.display());\n    }\n    Ok(())\n}\n","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/registry.rs#L241-L277","documentation":"install refuses to overwrite an existing binary in the destination bin dir unless --force is passed. If bin_dir/<bin> already exists and opts.force is false, it bails with '<path> already exists (use --force to overwrite)'.","triggerScenarios":"Installing a package whose binary name already exists in bin_dir (default bin dir or --bin-dir), with opts.force == false.","commonSituations":"Re-installing a package to upgrade; two different packages shipping binaries with the same name; a previous partial install left the binary in place.","solutions":["Re-run with --force if you intend to replace the existing binary.","Use a different --bin-dir if the collision is with an unrelated binary.","Remove the old binary manually if you want a clean install.","Rename your package's binary if it collides with a well-known tool."],"exampleFix":"// before\nmytool install --name mypkg\n// error: /usr/local/bin/mybin already exists (use --force to overwrite)\n\n// after\nmytool install --name mypkg --force","handlingStrategy":"validation","validationCode":"let dest = bin_dir.join(&bin);\nif dest.exists() && !opts.force {\n    eprintln!(\"{} exists; re-run with --force to replace it\", dest.display());\n    std::process::exit(2);\n}\n// safe to install","typeGuard":null,"tryCatchPattern":"match install(opts.clone()) {\n    Err(e) if e.to_string().contains(\"already exists (use --force\") => {\n        eprintln!(\"Existing binary found; upgrading with --force\");\n        install(InstallOpts { force: true, ..opts })\n    }\n    other => other,\n}","preventionTips":["Use --force deliberately for upgrades; keep plain installs for first-time setup.","Avoid packages with colliding binary names; check bin dir contents first.","Use a dedicated --bin-dir per tool set to isolate collisions.","In upgrade scripts, detect this error and re-invoke with --force automatically."],"tags":["filesystem","conflict","install"],"backgroundTag":"file-already-exists","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}