{"record":{"id":"6f09303e799a4689","repo":"BoundaryML/baml","slug":"a-source-root-already-exists-at-this-path","errorCode":null,"errorMessage":"a source root already exists at this path","messagePattern":"a source root already exists at this path","errorType":"exception","errorClass":"SourceRootError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_db/src/db.rs","lineNumber":86,"sourceCode":"    #[must_use]\n    pub fn served_from(mut self, interface: Vec<u8>) -> Self {\n        self.interface = Some(interface);\n        self\n    }\n\n    #[must_use]\n    pub fn depending_on(mut self, dependencies: Vec<Dependency>) -> Self {\n        self.dependencies = dependencies;\n        self\n    }\n}\n\n/// Why [`ProjectDatabase::add_source_root`] or\n/// [`ProjectDatabase::add_dependency`] refused.\n#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]\npub enum SourceRootError {\n    /// A live root already sits at this (canonical) path.\n    #[error(\"a source root already exists at this path\")]\n    PathTaken(SourceRoot),\n    /// The edge name is one no package may declare: a stdlib package's name\n    /// (already an implicit edge of every package) or a source-level\n    /// qualifier (`root`, `env`).\n    #[error(\"dependency name `{name}` is reserved\")]\n    ReservedDependencyName { name: Name },\n    /// The root already has an edge under this name.\n    #[error(\"dependency `{name}` is declared twice\")]\n    DuplicateDependencyName { name: Name },\n    /// The edge names a root that is not live in this database.\n    #[error(\"dependency `{name}` names a source root that does not exist\")]\n    UnknownDependencyRoot { name: Name },\n    /// The edge would make the dependency graph cyclic.\n    #[error(\"dependency `{name}` would form a dependency cycle\")]\n    DependencyCycle { name: Name },\n    /// The interface bytes are not a valid `PackageInterface` artifact.\n    #[error(\"invalid package interface: {message}\")]\n    InvalidInterface { message: String },","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_db/src/db.rs#L68-L104","documentation":"SourceRootError::PathTaken is returned by ProjectDatabase::add_source_root (and add_dependency paths) when a live source root already sits at the same canonical path. The DB refuses to register a duplicate root so each file belongs to exactly one package; the error carries the existing SourceRoot.","triggerScenarios":"Calling add_source_root with a path that canonicalizes to a root already added (including via symlinks or relative paths like ./src vs an absolute /abs/src), or re-adding the same root twice in one session.","commonSituations":"Configuring the same directory twice (once relative, once absolute or through a symlink); adding a dependency root that overlaps a project root; hot-reload logic re-registering roots without checking existing ones.","solutions":["Check whether the path is already registered (e.g. keep a set of canonical roots) and skip the duplicate add_source_root call.","Canonicalize your paths (std::fs::canonicalize) before comparing, and dedupe symlinks/relative forms.","Match on SourceRootError::PathTaken and treat it as a no-op if re-registration is expected in your tooling.","If you truly need a fresh database, rebuild the ProjectDatabase instead of re-adding roots to the existing one."],"exampleFix":"// before\ndb.add_source_root(path).unwrap();\n// after\nmatch db.add_source_root(&path) {\n    Ok(()) => {},\n    Err(SourceRootError::PathTaken(existing)) if existing.path() == path => {}, // already registered\n    Err(e) => return Err(e.into()),\n}","handlingStrategy":"validation","validationCode":"let canonical = std::fs::canonicalize(&path)?;\nif registered_roots.contains(&canonical) {\n    // skip add_source_root\n}","typeGuard":null,"tryCatchPattern":"match db.add_source_root(&path) {\n    Ok(()) => {},\n    Err(SourceRootError::PathTaken(_)) => { /* already registered: treat as no-op */ }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Canonicalize all root paths (resolving symlinks and relative segments) before registering.","Keep a registry of live roots and dedupe before calling add_source_root.","On config reload, diff roots instead of re-adding all of them."],"tags":["rust","database","duplicate-registration","filesystem"],"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-14T11:17:12.474Z"}