nikivdev/code · error

{} already exists (use --force to overwrite)

Error message

{} already exists (use --force to overwrite)

What it means

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)'.

Source

Thrown at src/registry.rs:259

        bail!("download failed ({})", response.status());
    }
    let bytes = response.bytes().context("failed to read download")?;

    if !opts.no_verify {
        if let Some(expected) = target_entry.sha256.get(&bin) {
            let actual = sha256_bytes(&bytes);
            if expected != &actual {
                bail!("checksum mismatch for {}", bin);
            }
        }
    }

    let bin_dir = opts.bin_dir.clone().unwrap_or_else(default_bin_dir);
    fs::create_dir_all(&bin_dir)
        .with_context(|| format!("failed to create {}", bin_dir.display()))?;
    let dest = bin_dir.join(&bin);
    if dest.exists() && !opts.force {
        bail!(
            "{} already exists (use --force to overwrite)",
            dest.display()
        );
    }

    let mut temp = NamedTempFile::new_in(&bin_dir)
        .with_context(|| format!("failed to create temp file in {}", bin_dir.display()))?;
    temp.write_all(&bytes)?;
    temp.flush()?;
    persist_with_permissions(temp, &dest)?;

    println!("Installed {} to {}", bin, dest.display());
    if !path_in_env(&bin_dir) {
        println!("Add {} to PATH to use it everywhere.", bin_dir.display());
    }
    Ok(())
}

View on GitHub (pinned to a747e741ae)

Solutions

  1. Re-run with --force if you intend to replace the existing binary.
  2. Use a different --bin-dir if the collision is with an unrelated binary.
  3. Remove the old binary manually if you want a clean install.
  4. Rename your package's binary if it collides with a well-known tool.

Example fix

// before
mytool install --name mypkg
// error: /usr/local/bin/mybin already exists (use --force to overwrite)

// after
mytool install --name mypkg --force
Defensive patterns

Strategy: validation

Validate before calling

let dest = bin_dir.join(&bin);
if dest.exists() && !opts.force {
    eprintln!("{} exists; re-run with --force to replace it", dest.display());
    std::process::exit(2);
}
// safe to install

Try / catch

match install(opts.clone()) {
    Err(e) if e.to_string().contains("already exists (use --force") => {
        eprintln!("Existing binary found; upgrading with --force");
        install(InstallOpts { force: true, ..opts })
    }
    other => other,
}

Prevention

When it happens

Trigger: Installing a package whose binary name already exists in bin_dir (default bin dir or --bin-dir), with opts.force == false.

Common situations: Re-installing a package to upgrade; two different packages shipping binaries with the same name; a previous partial install left the binary in place.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of nikivdev/code@a747e741ae (2026-09-01). Data as JSON: /api/errors/c2a36b8d215cea77. Report an issue: GitHub.