nikivdev/code · error

Destination already exists: {}

Error message

Destination already exists: {}

What it means

Thrown by new_from_template (src/code.rs:141) when the destination path resolved for the new project already exists on disk. The tool refuses to overwrite, protecting existing code from being clobbered by a template copy.

Source

Thrown at src/code.rs:141

                || trimmed.starts_with("../")
                || trimmed.starts_with('/')
                || trimmed.starts_with('~')
            {
                let expanded = config::expand_path(trimmed);
                if expanded.is_absolute() {
                    expanded
                } else {
                    std::env::current_dir()?.join(&expanded)
                }
            } else {
                // Relative name like "zerg" → ~/code/zerg
                config::expand_path(DEFAULT_CODE_ROOT).join(trimmed)
            }
        }
    };

    if target.exists() {
        bail!("Destination already exists: {}", target.display());
    }

    if opts.dry_run {
        println!(
            "Would copy template {} -> {}",
            template_dir.display(),
            target.display()
        );
        return Ok(());
    }

    // Create parent directories if needed
    if let Some(parent) = target.parent() {
        if !parent.exists() {
            fs::create_dir_all(parent).with_context(|| {
                format!("failed to create parent directory {}", parent.display())
            })?;
        }

View on GitHub (pinned to a747e741ae)

Solutions

  1. Choose a different destination path or project name.
  2. Remove or rename the existing directory if it's safe to discard (`mv ~/code/name ~/code/name.bak`).
  3. Run with --dry-run (`f new <template> <path> --dry-run`) to confirm the resolved target before creating.
  4. Resume from the existing directory instead — if the previous run completed, no need to re-create.

Example fix

// before
$ f new rust-cli myapp
Error: Destination already exists: /home/user/code/myapp
// after
$ mv ~/code/myapp ~/code/myapp.bak
$ f new rust-cli myapp   # or use --dry-run first
Defensive patterns

Strategy: validation

Validate before calling

// Shell: ensure the destination is free before creating
TARGET="$HOME/code/myapp"
[ ! -e "$TARGET" ] || { echo "destination exists: $TARGET"; exit 1; }

Try / catch

match new_from_template(opts) {
    Err(e) if e.to_string().contains("Destination already exists") => {
        eprintln!("Pick a new name or move the existing directory aside.");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Running `f new <template> <target>` where the resolved target exists: no path arg → ./<template> already present in cwd; bare name → ~/code/<name> already exists; explicit path that already exists (even an empty directory).

Common situations: Re-running `f new` after a partial/interrupted previous run left the directory; creating a project with a name you already used; cwd already contains a directory named like the template.

Related errors


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