zed-industries/zed · error · anyhow::Error

{target:?} already exists

Error message

{target:?} already exists

What it means

RealFs::copy_file refuses to overwrite: when the target path already exists (smol::fs::metadata succeeds) and CopyOptions has overwrite=false, it bails with the target path — unless ignore_if_exists=true, which turns the collision into a silent Ok. The check is deliberately TOCTOU-racy upstream of smol::fs::copy; it is a policy guard, not an atomic compare.

Source

Thrown at crates/fs/src/fs.rs:806

        futures::io::copy(content, &mut file).await?;
        Ok(())
    }

    async fn extract_tar_file(
        &self,
        path: &Path,
        content: Archive<Pin<&mut (dyn AsyncRead + Send)>>,
    ) -> Result<()> {
        content.unpack(path).await?;
        Ok(())
    }

    async fn copy_file(&self, source: &Path, target: &Path, options: CopyOptions) -> Result<()> {
        if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
            if options.ignore_if_exists {
                return Ok(());
            } else {
                anyhow::bail!("{target:?} already exists");
            }
        }

        smol::fs::copy(source, target).await?;
        Ok(())
    }

    async fn rename(&self, source: &Path, target: &Path, options: RenameOptions) -> Result<()> {
        if options.create_parents {
            if let Some(parent) = target.parent() {
                self.create_dir(parent).await?;
            }
        }

        if options.overwrite {
            smol::fs::rename(source, target).await?;
            return Ok(());
        }

View on GitHub (pinned to 9d272b0363)

Solutions

  1. Pass CopyOptions { overwrite: true, .. } when replacing is intended
  2. Pass ignore_if_exists: true to skip existing targets silently
  3. Otherwise remove/rename the target before copying

Example fix

// before
fs.copy_file(&source, &target, CopyOptions { overwrite: false, ignore_if_exists: false }).await?; // "{target:?} already exists"

// after
fs.copy_file(&source, &target, CopyOptions { overwrite: true, ignore_if_exists: false }).await?;
Defensive patterns

Strategy: validation

Validate before calling

if !options.overwrite && smol::fs::metadata(target).await.is_ok() {
    // decide now: skip, replace, or abort — instead of letting copy_file bail
    return Ok(());
}

Try / catch

if let Err(e) = fs.copy_file(&source, &target, options).await {
    if e.to_string().contains("already exists") { /* skip or overwrite */ } else { return Err(e); }
}

Prevention

When it happens

Trigger: fs.copy_file(source, target, CopyOptions { overwrite: false, ignore_if_exists: false, .. }) with an existing file or directory at target.

Common situations: Re-running an install/sync step that copies binaries or assets into a populated directory, two tasks writing the same destination, or restoring a backup over existing files.

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 zed-industries/zed@9d272b0363 (2026-08-20). Data as JSON: /api/errors/0cc0f30afd2cd4f2. Report an issue: GitHub.