swc-project/swc · error · anyhow::Error

destination `{}` already exists

Error message

destination `{}` already exists

What it means

Thrown by `swc plugin new` (PluginScaffoldOptions::execute) when the destination path for the new plugin project already exists on disk. The scaffolder refuses to overwrite an existing directory, mirroring cargo new behavior.

Source

Thrown at crates/swc_cli_impl/src/commands/plugin.rs:116

    let mut f = OpenOptions::new()
        .append(true)
        .create(true)
        .open(&ignore_file_path)?;

    write!(f, "{ignore}").context("failed to write to .gitignore file")?;

    Ok(())
}

impl super::CommandRunner for PluginScaffoldOptions {
    /// Create a rust project for the plugin from template.
    /// This largely mimic https://github.com/rust-lang/cargo/blob/master/src/cargo/ops/cargo_new.rs,
    /// but also thinner implementation based on some assumptions like skipping
    /// to support non-git based vcs.
    fn execute(&self) -> Result<()> {
        let path = &self.path;
        if path.exists() {
            anyhow::bail!("destination `{}` already exists", path.display())
        }

        let name = get_name(self)?;

        // Choose to rely on system's git binary instead of depends on git lib for now
        // to avoid bring in large / heavy dependencies into cli binaries.
        // Depends on our usecase grows, we can revisit this.
        let mut base_git_cmd = if cfg!(target_os = "windows") {
            let mut c = std::process::Command::new("cmd");
            c.arg("/C").arg("git");
            c
        } else {
            std::process::Command::new("git")
        };

        // init git repo
        base_git_cmd
            .args(["init", name])

View on GitHub (pinned to d7d7434666)

Solutions

  1. Pick a fresh destination path that does not exist yet
  2. Remove or rename the existing directory if it is a leftover: rm -rf ./my_plugin
  3. If the existing directory is wanted content, scaffold into a temp path and merge manually

Example fix

# before
$ swc plugin new ./my_plugin   # ./my_plugin already exists -> error

# after
$ rm -rf ./my_plugin && swc plugin new ./my_plugin
# or
$ swc plugin new ./my_plugin_v2
Defensive patterns

Strategy: validation

Validate before calling

// Rust: check before invoking the scaffold command
let path = std::path::Path::new(&opts.path);
if path.exists() {
    anyhow::bail!("refusing to scaffold: {} already exists", path.display());
}
PluginScaffoldOptions { path: opts.path, .. }.execute()?;

Try / catch

// CLI wrapper
match cmd.execute() {
    Err(e) if e.to_string().contains("already exists") => {
        println!("destination taken; pass --force or remove the directory");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Running `swc plugin new <path>` (or the plugin scaffold subcommand) with a path that already exists as a file or directory, including an empty leftover directory from a previous failed run.

Common situations: Re-running the scaffold after an interrupted attempt, choosing a name that collides with an existing folder in the cwd, or scripting the CLI without cleaning up between runs.

Related errors


AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16). Data as JSON: /api/errors/9e87d45df9a41a69. Report an issue: GitHub.