{"record":{"id":"f4d13a523c5a0cb3","repo":"tonhowtf/omniget","slug":"app-data-dir-unavailable-cookie","errorCode":null,"errorMessage":"app_data_dir unavailable","messagePattern":"app_data_dir unavailable","errorType":"exception","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"src-tauri/src/platforms/bilibili/cookie.rs","lineNumber":67,"sourceCode":"            .join(\"_anonymous_meta.json\"),\n    )\n}\n\npub fn load_state() -> AnonymousCookieState {\n    let path = match meta_file_path() {\n        Some(p) => p,\n        None => return AnonymousCookieState::default(),\n    };\n    let content = match std::fs::read_to_string(&path) {\n        Ok(c) => c,\n        Err(_) => return AnonymousCookieState::default(),\n    };\n    serde_json::from_str(&content).unwrap_or_default()\n}\n\npub fn save_state(state: &AnonymousCookieState) -> std::io::Result<()> {\n    let path = meta_file_path().ok_or_else(|| {\n        std::io::Error::new(std::io::ErrorKind::Other, \"app_data_dir unavailable\")\n    })?;\n    if let Some(parent) = path.parent() {\n        std::fs::create_dir_all(parent)?;\n    }\n    let serialized = serde_json::to_string_pretty(state)\n        .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;\n    std::fs::write(path, serialized)\n}\n\npub async fn ensure_fresh() -> Result<AnonymousCookieState> {\n    let cached = load_state();\n    if cached.is_fresh() {\n        write_netscape_for_anonymous(&cached).ok();\n        return Ok(cached);\n    }\n    bootstrap_anonymous().await\n}\n","sourceCodeStart":49,"sourceCodeEnd":85,"githubUrl":"https://github.com/tonhowtf/omniget/blob/8600b91f4246848bac346874daa9e61c1fc5677a/src-tauri/src/platforms/bilibili/cookie.rs#L49-L85","documentation":"save_state persists the anonymous Bilibili cookie state as JSON. It resolves the metadata file path via meta_file_path(), which returns None when the app data directory cannot be resolved; the function then raises an io::Error with message \"app_data_dir unavailable\". Without a writable data dir, anonymous cookie state cannot be persisted.","triggerScenarios":"bootstrap_anonymous → save_state runs while meta_file_path() returns None because tauri/dirs cannot resolve the platform app-data directory (no HOME on Linux, sandboxed environment, path resolver not initialized).","commonSituations":"Headless CI or Docker runs without HOME/XDG_DATA_HOME; portable/flatpak builds with restricted data dirs; calling save_state before the Tauri app setup has run; corrupted user session environment.","solutions":["Run the app in an environment where HOME (or the OS equivalent) is set and writable.","Ensure save_state is only called after Tauri app setup so the path resolver is available.","Add a fallback path (e.g. std::env::temp_dir or explicit config) when meta_file_path() returns None.","Log the underlying reason in meta_file_path and surface it in the error message for diagnosis."],"exampleFix":"// before\nlet path = meta_file_path().ok_or_else(|| {\n    std::io::Error::new(std::io::ErrorKind::Other, \"app_data_dir unavailable\")\n})?;\n\n// after\nlet path = meta_file_path().or_else(|| {\n    std::env::var_os(\"HOME\").map(|h| std::path::PathBuf::from(h).join(\".local/share/omniget/bilibili-anonymous.json\"))\n}).ok_or_else(|| {\n    std::io::Error::new(std::io::ErrorKind::Other, \"app_data_dir unavailable: HOME not set\")\n})?;","handlingStrategy":"fallback","validationCode":"// Rust\nif meta_file_path().is_none() {\n    tracing::warn!(\"app_data_dir unavailable; anonymous cookie state will not persist\");\n}","typeGuard":null,"tryCatchPattern":"match save_state(&state) {\n    Err(e) if e.to_string().contains(\"app_data_dir unavailable\") => {\n        tracing::warn!(\"skipping anonymous state persistence: {e}\");\n    }\n    r => r?,\n}","preventionTips":["Ensure HOME / OS data dir is set in containers and CI","Resolve paths once at app setup and fail fast there","Treat persistence as optional in headless environments"],"tags":["filesystem","rust","app-data-dir","tauri"],"backgroundTag":"missing-env-var","analyzedSha":"8600b91f4246848bac346874daa9e61c1fc5677a","analyzedAt":"2026-09-12T14:29:19.317Z","contentChangedAt":"2026-09-12T14:29:19.317Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}