{"record":{"id":"8695ff4bdc86977c","repo":"BoundaryML/baml","slug":"library-already-initialized-from-existing-path-cannot-change","errorCode":null,"errorMessage":"Library already initialized from {existing_path}, cannot change to {requested_path}","messagePattern":"Library already initialized from (.+?), cannot change to (.+?)","errorType":"exception","errorClass":"BamlSysError","httpStatus":null,"severity":"error","filePath":"languages/rust/baml-sys/src/error.rs","lineNumber":57,"sourceCode":"\n    /// Failed to determine cache directory.\n    #[error(\"Failed to determine cache directory: {0}\")]\n    CacheDir(String),\n\n    /// Download failed.\n    #[error(\"Failed to download library: {0}\")]\n    DownloadFailed(String),\n\n    /// Checksum mismatch after download.\n    #[error(\"Checksum mismatch: expected {expected}, got {actual}\")]\n    ChecksumMismatch { expected: String, actual: String },\n\n    /// IO error.\n    #[error(\"IO error: {0}\")]\n    Io(#[from] std::io::Error),\n\n    /// Library already initialized with different path.\n    #[error(\"Library already initialized from {existing_path}, cannot change to {requested_path}\")]\n    AlreadyInitialized {\n        existing_path: PathBuf,\n        requested_path: PathBuf,\n    },\n}\n\n/// Result type for baml-sys operations.\npub type Result<T> = std::result::Result<T, BamlSysError>;\n","sourceCodeStart":39,"sourceCodeEnd":66,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/languages/rust/baml-sys/src/error.rs#L39-L66","documentation":"baml-sys initializes the native library through a process-wide singleton (once-style init); this error is raised when an init call requests a library path different from the one already loaded. The process cannot have two different native library instances, so the conflicting request is rejected, and both the existing and requested paths are reported.","triggerScenarios":"Calling the init/load API twice with different explicit library paths in the same process, or mixing code paths where one initializes with a system-installed library and another with a downloaded cached one.","commonSituations":"Test suites where one test sets a custom library path and later tests call the default init, embedding code that configures baml-sys at startup and a dependency that re-initializes with its own path, or hot-reload logic re-running initialization with new arguments.","solutions":["Initialize baml-sys exactly once at process startup with the final path and reuse it","Remove duplicate init calls with conflicting paths (search for the init/load call sites)","Align all components on the same library path (or drop explicit paths to use the default resolution)","If a different library is genuinely required, load it in a separate process"],"exampleFix":"// before\nbaml_sys::init(\"/opt/baml/libbaml.so\");\n// later in another module\nbaml_sys::init(\"/usr/lib/libbaml.so\"); // panics/errors\n// after\nbaml_sys::init(\"/opt/baml/libbaml.so\"); // single init, shared via OnceLock\n","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"static INIT: OnceLock<PathBuf> = OnceLock::new();\nfn ensure_init(path: Option<PathBuf>) -> Result<()> {\n    let p = INIT.get_or_init(|| path.unwrap_or_else(default_path)).clone();\n    baml_sys::init(&p).map_err(|e| match e {\n        BamlSysError::AlreadyInitialized { existing_path, .. } if existing_path == p => Ok(()),\n        BamlSysError::AlreadyInitialized { existing_path, requested_path } => Err(e),\n        other => Err(other),\n    }?)\n}","preventionTips":["Wrap initialization in a OnceCell/OnceLock so it runs at most once","Centralize init in one module; forbid other modules from choosing library paths","Accept an already-initialized library with the same path as a no-op","In tests, use a single global init fixture instead of per-test paths"],"tags":["rust","initialization","singleton","state","native-library"],"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"}