BoundaryML/baml · error

destination ` ` already exists. Use `baml init` to…

Error message

destination `{}` already exists. Use `baml init` to initialize inside an existing directory, or pick a new path.

What it means

`baml init <path>` requires the destination directory to not exist; it creates it itself with create_dir. This bail fires up front when self.path already exists, telling the user to use `baml init` semantics inside an existing directory or choose a new path.

Solutions

  1. If the directory exists but has no baml.toml, run `baml init` from inside it (init without a new target path per the message) — note existing-dir+toml is separately guarded.
  2. Pick a fresh path: `baml init ./new-project` where the path does not exist yet.
  3. Remove the stale directory if it is safe to delete, then re-run init.

Example fix

// before
mkdir myproj && baml init myproj   // bails: destination exists
// after
baml init myproj                   // let init create the directory
Defensive patterns

Strategy: validation

Validate before calling

# guard before `baml init <path>`
if [ -e "<path>" ]; then echo "destination exists; choose another path"; exit 1; fi
baml init <path>

Prevention

When it happens

Trigger: Run `baml init <path>` where `<path>` is any existing file or directory (checked with path.exists() before create_dir).

Common situations: `baml init .` in a non-empty existing directory, or re-running init after a previous successful run created the directory.

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 BoundaryML/baml@bd85ce9dee (2026-09-12). Data as JSON: /api/errors/752a99a78b494f68. Report an issue: GitHub.

Appendix: source

Thrown at baml_language/crates/baml_cli/src/init_command.rs:103

    /// Directory to create. Errors if it already exists.
    #[arg(value_name = "PATH")]
    pub path: PathBuf,

    /// Project name written to `baml.toml`'s `[package].name`. Defaults
    /// to the basename of `<PATH>`.
    #[arg(long, help_heading = "Project options")]
    pub name: Option<String>,
}

impl NewArgs {
    pub fn run(&self) -> Result<crate::ExitCode> {
        let reporter = Reporter::new();
        self.run_with_reporter(&reporter)
    }

    fn run_with_reporter(&self, reporter: &Reporter) -> Result<crate::ExitCode> {
        if self.path.exists() {
            anyhow::bail!(
                "destination `{}` already exists. Use `baml init` to initialize \
                 inside an existing directory, or pick a new path.",
                self.path.display()
            );
        }
        std::fs::create_dir(&self.path)
            .with_context(|| format!("failed to create directory {}", self.path.display()))?;
        let canonical = std::fs::canonicalize(&self.path)
            .with_context(|| format!("failed to canonicalize path {}", self.path.display()))?;
        scaffold(&canonical, self.name.as_deref(), reporter, "Created")
    }
}

/// Shared writer for `init` and `new`. Both flows reach this once the
/// destination directory exists and is known not to already be a BAML
/// project. `verb` is the past-tense label rendered in the final status
/// line ("Initialized" vs. "Created").
fn scaffold(

View on GitHub (pinned to bd85ce9dee)