{"record":{"id":"9cd8f1f4f75bfc40","repo":"zed-industries/zed","slug":"database-not-initialized","errorCode":null,"errorMessage":"database not initialized","messagePattern":"database not initialized","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/db/src/db.rs","lineNumber":88,"sourceCode":"    /// inventory-registered migrations in dependency order.\n    #[cfg(any(test, feature = \"test-support\"))]\n    pub fn test_new() -> Self {\n        let name = format!(\"test-db-{}\", uuid::Uuid::new_v4());\n        let connection = gpui::block_on(open_test_db::<AppMigrator>(&name));\n        Self(connection)\n    }\n\n    /// Returns the per-App connection if set, otherwise falls back to\n    /// the shared LazyLock.\n    pub fn global(cx: &App) -> &ThreadSafeConnection {\n        #[allow(unreachable_code)]\n        if let Some(db) = cx.try_global::<Self>() {\n            return &db.0;\n        } else {\n            #[cfg(any(feature = \"test-support\", test))]\n            return &TEST_APP_DATABASE.0;\n\n            panic!(\"database not initialized\")\n        }\n    }\n}\n\nfn topological_sort<'a>(registrations: &[&'a DomainMigration]) -> Vec<&'a DomainMigration> {\n    let mut sorted: Vec<&DomainMigration> = Vec::new();\n    let mut visited: std::collections::HashSet<&str> = std::collections::HashSet::new();\n\n    fn visit<'a>(\n        name: &str,\n        registrations: &[&'a DomainMigration],\n        sorted: &mut Vec<&'a DomainMigration>,\n        visited: &mut std::collections::HashSet<&'a str>,\n    ) {\n        if visited.contains(name) {\n            return;\n        }\n        if let Some(reg) = registrations.iter().find(|r| r.name == name) {","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/db/src/db.rs#L70-L106","documentation":"Database::global(cx) returns the per-App connection global set during startup, falling back to a shared LazyLock. The panic fires when no Database global was installed and the build is not test/test-support (those builds return TEST_APP_DATABASE first, which is why the code carries #[allow(unreachable_code)]). It means some code path queried the database before initialization ran — tests can pass while production panics.","triggerScenarios":"Calling Database::global before the startup code that does cx.set_global; a new entrypoint (CLI, embedder, plugin host) wired up without the db init sequence; init made conditional on a feature that is compiled out.","commonSituations":"Refactors moving init after first use; new binaries copying only part of the app bootstrap; tests green via TEST_APP_DATABASE while the shipped build crashes at first query.","solutions":["Find the init that installs the global (cx.set_global of the Database wrapper) and run it during app startup, before any entity touches the db","Copy the full init sequence into the new entrypoint","In test binaries, ensure the test-support feature is enabled so TEST_APP_DATABASE is used","Grep for Database::global callers reachable before init and reorder"],"exampleFix":"// before\nfn main() {\n    app.run(|cx| {\n        let conn = db::Database::global(cx); // panic: database not initialized\n    });\n}\n\n// after\nfn main() {\n    app.run(|cx| {\n        cx.set_global(db::Database::open_default(cx)); // install the global first\n        let conn = db::Database::global(cx);\n    });\n}","handlingStrategy":"validation","validationCode":"use gpui::App;\n\nfn ensure_database(cx: &mut App) {\n    if cx.try_global::<db::Database>().is_none() {\n        cx.set_global(db::Database::open_default(cx));\n    }\n}","typeGuard":"fn database_ready(cx: &App) -> bool {\n    cx.try_global::<db::Database>().is_some()\n}","tryCatchPattern":null,"preventionTips":["Install the Database global in every entrypoint's bootstrap, before spawning entities that query it","Add a startup assertion that the global exists before first use","Remember test builds silently fall back to TEST_APP_DATABASE — test green does not prove init ran"],"tags":["gpui","global-state","initialization","panic","database"],"backgroundTag":"global-state-not-initialized","analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}