{"record":{"id":"752a99a78b494f68","repo":"BoundaryML/baml","slug":"destination-already-exists-use-baml-init-to-initialize","errorCode":null,"errorMessage":"destination `{}` already exists. Use `baml init` to initialize inside an existing directory, or pick a new path.","messagePattern":"destination `(.+?)` already exists\\. Use `baml init` to initialize inside an existing directory, or pick a new path\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_cli/src/init_command.rs","lineNumber":103,"sourceCode":"    /// Directory to create. Errors if it already exists.\n    #[arg(value_name = \"PATH\")]\n    pub path: PathBuf,\n\n    /// Project name written to `baml.toml`'s `[package].name`. Defaults\n    /// to the basename of `<PATH>`.\n    #[arg(long, help_heading = \"Project options\")]\n    pub name: Option<String>,\n}\n\nimpl NewArgs {\n    pub fn run(&self) -> Result<crate::ExitCode> {\n        let reporter = Reporter::new();\n        self.run_with_reporter(&reporter)\n    }\n\n    fn run_with_reporter(&self, reporter: &Reporter) -> Result<crate::ExitCode> {\n        if self.path.exists() {\n            anyhow::bail!(\n                \"destination `{}` already exists. Use `baml init` to initialize \\\n                 inside an existing directory, or pick a new path.\",\n                self.path.display()\n            );\n        }\n        std::fs::create_dir(&self.path)\n            .with_context(|| format!(\"failed to create directory {}\", self.path.display()))?;\n        let canonical = std::fs::canonicalize(&self.path)\n            .with_context(|| format!(\"failed to canonicalize path {}\", self.path.display()))?;\n        scaffold(&canonical, self.name.as_deref(), reporter, \"Created\")\n    }\n}\n\n/// Shared writer for `init` and `new`. Both flows reach this once the\n/// destination directory exists and is known not to already be a BAML\n/// project. `verb` is the past-tense label rendered in the final status\n/// line (\"Initialized\" vs. \"Created\").\nfn scaffold(","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_cli/src/init_command.rs#L85-L121","documentation":"`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.","triggerScenarios":"Run `baml init <path>` where `<path>` is any existing file or directory (checked with path.exists() before create_dir).","commonSituations":"`baml init .` in a non-empty existing directory, or re-running init after a previous successful run created the directory.","solutions":["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.","Pick a fresh path: `baml init ./new-project` where the path does not exist yet.","Remove the stale directory if it is safe to delete, then re-run init."],"exampleFix":"// before\nmkdir myproj && baml init myproj   // bails: destination exists\n// after\nbaml init myproj                   // let init create the directory","handlingStrategy":"validation","validationCode":"# guard before `baml init <path>`\nif [ -e \"<path>\" ]; then echo \"destination exists; choose another path\"; exit 1; fi\nbaml init <path>","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never mkdir the target before baml init (init creates it)","Use `test -e \"$path\" &&` guards in scripts","Prefer initializing into fresh paths"],"tags":["cli","scaffold","directory-exists","already-exists"],"backgroundTag":"file-already-exists","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}