nikivdev/code · error

{} already exists; refusing to overwrite

Error message

{} already exists; refusing to overwrite

What it means

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.

Source

Thrown at src/init.rs:69

#max_local_gate_seconds = 20
"#;

pub(crate) fn write_template(path: &Path) -> Result<()> {
    if let Some(parent) = path.parent() {
        if !parent.as_os_str().is_empty() {
            fs::create_dir_all(parent)
                .with_context(|| format!("failed to create directory {}", parent.display()))?;
        }
    }

    fs::write(path, TEMPLATE).with_context(|| format!("failed to write {}", path.display()))?;
    Ok(())
}

pub fn run(opts: InitOpts) -> Result<()> {
    let target = resolve_path(opts.path);
    if target.exists() {
        bail!("{} already exists; refusing to overwrite", target.display());
    }

    write_template(&target)?;
    println!("created {}", target.display());
    Ok(())
}

fn resolve_path(path: Option<PathBuf>) -> PathBuf {
    match path {
        Some(p) if p.is_absolute() => p,
        Some(p) => std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join(p),
        None => std::env::current_dir()
            .unwrap_or_else(|_| PathBuf::from("."))
            .join("flow.toml"),
    }
}

View on GitHub (pinned to a747e741ae)

Solutions

  1. Delete or rename the existing file/directory at the target path, then re-run init.
  2. Choose a different target path for the new template.
  3. If the existing file is a prior valid template, keep it — no init needed.
  4. Inspect `target.display()` from the error to confirm exactly which path is blocking.

Example fix

// before
f init ./existing-config
// after
rm ./existing-config && f init ./existing-config  # or pick a new path
Defensive patterns

Strategy: validation

Validate before calling

let target = resolve_path(path);
if target.exists() {
    // pick another path or remove the file first
    eprintln!("{} exists; init skipped", target.display());
} else {
    init::run(InitOpts { path: path.into() })?;
}

Try / catch

match init::run(opts) {
    Ok(()) => {},
    Err(e) if e.to_string().contains("refusing to overwrite") => {
        eprintln!("target exists; choose a new path or delete the old file");
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: 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.

Common situations: Re-running `init` after a previous successful run; a leftover file from a failed previous attempt; passing '.' or an existing config filename by habit.

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/6dd1c51099d4e011. Report an issue: GitHub.