{"record":{"id":"a53286e68d6b7783","repo":"BoundaryML/baml","slug":"dependency-name-would-form-a-dependency-cycle","errorCode":null,"errorMessage":"dependency `{name}` would form a dependency cycle","messagePattern":"dependency `(.+?)` would form a dependency cycle","errorType":"exception","errorClass":"SourceRootError","httpStatus":null,"severity":"error","filePath":"baml_language/crates/baml_db/src/db.rs","lineNumber":100,"sourceCode":"/// [`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 },\n}\n\n/// The main database for BAML projects.\n///\n/// `ProjectDatabase` owns the Salsa storage directly and implements all the\n/// compiler `Db` traits. It provides high-level APIs for:\n/// - Source-root management (add/remove roots, longest-prefix lookup)\n/// - File management within a root (add/update/remove files)\n/// - Diagnostics collection via `check()`\n///\n/// ## Example\n///\n/// ```ignore\n/// let mut db = ProjectDatabase::new();","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_db/src/db.rs#L82-L118","documentation":"Part of `SourceRootError` returned by `ProjectDatabase::add_source_root` / `add_dependency`. Adding the dependency edge would make the dependency graph cyclic (A depends on B depends on ... depends on A). The database keeps the package graph a DAG and refuses the edge.","triggerScenarios":"Calling `add_source_root`/`add_dependency` where the new edge closes a cycle among existing roots, e.g. root X declares a dependency on a root that (transitively) already depends on X.","commonSituations":"Two packages mutually depending on each other after a refactor; cyclic package splits; accidentally naming the package itself as a dependency.","solutions":["Remove the back-edge dependency so the graph stays acyclic.","Split the shared code into a new common package that both depend on.","Audit the dependency chain with a graph walk before adding the edge."],"exampleFix":"// before\n// core depends on ui, and now: ui_spec.depending_on(vec![dep(\"core\")]) -> cycle\n// after\n// move shared types into a new \"shared\" package; ui -> shared, core -> shared","handlingStrategy":"validation","validationCode":"fn would_cycle(deps: &[Dependency], target: SourceRoot, db: &ProjectDatabase) -> bool {\n    deps.iter().any(|d| {\n        let mut stack = vec![d.root];\n        while let Some(r) = stack.pop() {\n            if r == target { return true; }\n            stack.extend(db.dependencies_of(r));\n        }\n        false\n    })\n}","typeGuard":null,"tryCatchPattern":"match db.add_source_root(spec) {\n    Err(SourceRootError::DependencyCycle { name }) => eprintln!(\"edge `{name}` closes a cycle; drop it\"),\n    Err(e) => eprintln!(\"root error: {e}\"),\n    Ok(root) => root,\n}","preventionTips":["Keep the package graph a DAG by design; use a shared package for mutual code.","Walk the transitive dependency graph before adding back-edges.","Never let a package depend on itself."],"tags":["dependency-management","rust","circular-dependency"],"backgroundTag":"invalid-state-transition","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"}