{"record":{"id":"9f6b704935cb4e16","repo":"zed-industries/zed","slug":"environment-filter-cannot-be-initialized-twice","errorCode":null,"errorMessage":"Environment filter cannot be initialized twice","messagePattern":"Environment filter cannot be initialized twice","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/zlog/src/filter.rs","lineNumber":53,"sourceCode":"pub static LEVEL_ENABLED_MAX_CONFIG: AtomicU8 = AtomicU8::new(LEVEL_ENABLED_MAX_DEFAULT as u8);\n\nconst DEFAULT_FILTERS: &[(&str, log::LevelFilter)] = &[\n    #[cfg(any(target_os = \"linux\", target_os = \"freebsd\"))]\n    (\"zbus\", log::LevelFilter::Warn),\n    #[cfg(any(target_os = \"linux\", target_os = \"freebsd\", target_os = \"windows\"))]\n    (\"naga::back::spv::writer\", log::LevelFilter::Warn),\n    // usvg prints a lot of warnings on rendering an SVG with partial errors, which\n    // can happen a lot with the SVG preview\n    (\"usvg::parser\", log::LevelFilter::Error),\n    (\"pet\", log::LevelFilter::Warn),\n];\n\npub fn init_env_filter(filter: env_config::EnvFilter) {\n    if let Some(level_max) = filter.level_global {\n        LEVEL_ENABLED_MAX_STATIC.store(level_max as u8, Ordering::Release)\n    }\n    if ENV_FILTER.set(filter).is_err() {\n        panic!(\"Environment filter cannot be initialized twice\");\n    }\n}\n\npub fn is_possibly_enabled_level(level: log::Level) -> bool {\n    level as u8 <= LEVEL_ENABLED_MAX_CONFIG.load(Ordering::Acquire)\n}\n\npub fn is_scope_enabled(\n    scope: &ScopeRef<'_>,\n    module_path: Option<&str>,\n    level: log::Level,\n) -> bool {\n    // TODO: is_always_allowed_level that checks against LEVEL_ENABLED_MIN_CONFIG\n    if !is_possibly_enabled_level(level) {\n        // [FAST PATH]\n        // if the message is above the maximum enabled log level\n        // (where error < warn < info etc) then disable without checking\n        // scope map","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/zed-industries/zed/blob/f4178619acd0d47ea1f76a2025c42962c6d6638c/crates/zlog/src/filter.rs#L35-L71","documentation":"zlog, Zed's logging stack, stores the process-wide env filter in a set-once static; init_env_filter panics if the filter is already installed (crates/zlog/src/filter.rs:53), because log filtering is configured exactly once per process. Note the global level maximum is stored before the set, so a second call mutates shared state before it panics — one more reason re-init must be prevented, not caught.","triggerScenarios":"Calling zlog::filter::init_env_filter (directly or via a zlog init entry point) a second time in the same process: per-test logging init when tests share a process, a library and its host both running init, or a re-init attempt after config reload.","commonSituations":"cargo test suites where each test calls the logging setup helper; plugins or embedding hosts that also initialize zlog; code that treats logging init as idempotent.","solutions":["Initialize logging exactly once per process: call init from main or a single setup path.","Guard shared or test setup with std::sync::Once so repeated calls are no-ops.","In tests, use the provided zlog::init_test() entry point, which is designed for test binaries.","For runtime changes, adjust dynamic level/scope controls instead of re-calling init."],"exampleFix":"// before: init runs per test / per call site\nfn setup_logging() {\n    zlog::filter::init_env_filter(filter);\n}\n\n// after: once per process\nstatic INIT_LOGGING: std::sync::Once = std::sync::Once::new();\nfn setup_logging() {\n    INIT_LOGGING.call_once(|| zlog::filter::init_env_filter(filter));\n}","handlingStrategy":"validation","validationCode":"static INIT_LOGGING: std::sync::Once = std::sync::Once::new();\n\nfn setup_logging() {\n    INIT_LOGGING.call_once(|| zlog::filter::init_env_filter(filter));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call logging init from one place (main, or one shared test fixture).","Guard any reachable-from-multiple-entry-points init with Once.","Use zlog::init_test() in tests instead of raw filter init.","Remember the second call mutates the level static before it panics — prevent, do not catch."],"tags":["logging","zlog","initialization","once","panic","zed"],"backgroundTag":"double-initialization","analyzedSha":"f4178619acd0d47ea1f76a2025c42962c6d6638c","analyzedAt":"2026-08-20T19:29:52.058Z","contentChangedAt":"2026-08-20T19:29:52.058Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}