{"record":{"id":"11c051b8ec4b817c","repo":"pot-app/pot-desktop","slug":"get-cache-dir-failed","errorCode":null,"errorMessage":"Get Cache Dir Failed","messagePattern":"Get Cache Dir Failed","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src-tauri/src/cmd.rs","lineNumber":28,"sourceCode":"\n#[tauri::command]\npub fn get_text(state: tauri::State<StringWrapper>) -> String {\n    return state.0.lock().unwrap().to_string();\n}\n\n#[tauri::command]\npub fn reload_store() {\n    let state = APP.get().unwrap().state::<StoreWrapper>();\n    let mut store = state.0.lock().unwrap();\n    store.load().unwrap();\n}\n\n#[tauri::command]\npub fn cut_image(left: u32, top: u32, width: u32, height: u32, app_handle: tauri::AppHandle) {\n    use dirs::cache_dir;\n    use image::GenericImage;\n    info!(\"Cut image: {}x{}+{}+{}\", width, height, left, top);\n    let mut app_cache_dir_path = cache_dir().expect(\"Get Cache Dir Failed\");\n    app_cache_dir_path.push(&app_handle.config().tauri.bundle.identifier);\n    app_cache_dir_path.push(\"pot_screenshot.png\");\n    if !app_cache_dir_path.exists() {\n        return;\n    }\n    let mut img = match image::open(&app_cache_dir_path) {\n        Ok(v) => v,\n        Err(e) => {\n            error!(\"{:?}\", e.to_string());\n            return;\n        }\n    };\n    let img2 = img.sub_image(left, top, width, height);\n    app_cache_dir_path.pop();\n    app_cache_dir_path.push(\"pot_screenshot_cut.png\");\n    match img2.to_image().save(&app_cache_dir_path) {\n        Ok(_) => {}\n        Err(e) => {","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/pot-app/pot-desktop/blob/594d32ede96acd106b0256deaa8bb440ffcdff40/src-tauri/src/cmd.rs#L10-L46","documentation":"cut_image calls dirs::cache_dir() and .expect(\"Get Cache Dir Failed\"), panicking when the OS provides no cache directory. cache_dir() returns None when standard environment variables/paths (XDG_CACHE_HOME or $HOME/.cache on Linux, HOME on macOS, FOLDERID_LocalAppData on Windows) cannot be resolved, so the tauri command aborts the process instead of returning an error.","triggerScenarios":"Invoking the cut_image tauri command on a system where dirs::cache_dir() yields None — e.g. Linux/macOS process running without $HOME set (services, systemd units, cron, some sandboxed/containerized environments) or a Windows profile missing LocalAppData.","commonSituations":"Pot app launched from a systemd service or cron job without HOME/XDG_CACHE_HOME; running inside a container with a stripped environment; corrupted/missing user profile directories; screenshot-to-clipboard/screenshot word extraction feature used in such an environment.","solutions":["Launch the app from a normal user session so HOME / XDG_CACHE_HOME (Linux/macOS) or the user profile (Windows) is set.","Verify XDG_CACHE_HOME/HOME are exported in the launch environment (env | grep -E 'HOME|XDG_CACHE').","Replace .expect with graceful handling: fall back to tauri's app_cache_dir or std::env::temp_dir when cache_dir() is None.","If running headless, create/point to a writable cache dir before invoking the command."],"exampleFix":"// before\nlet mut app_cache_dir_path = cache_dir().expect(\"Get Cache Dir Failed\");\n// after\nlet mut app_cache_dir_path = cache_dir()\n    .or_else(|| dirs::home_dir().map(|h| h.join(\".cache\")))\n    .unwrap_or_else(std::env::temp_dir);","handlingStrategy":"fallback","validationCode":"// Before invoking the command, verify a cache dir is resolvable:\n// (shell, launcher-side) test -n \"$HOME\" && mkdir -p \"${XDG_CACHE_HOME:-$HOME/.cache}\"\n// (Rust, before expect)\nif dirs::cache_dir().is_none() {\n    eprintln!(\"No cache directory available; refusing cut_image\");\n    return;\n}","typeGuard":"fn has_cache_dir() -> bool {\n    dirs::cache_dir()\n        .map(|p| p.is_dir())\n        .unwrap_or(false)\n}","tryCatchPattern":null,"preventionTips":["Never .expect/.unwrap on cache_dir() in shipped commands; degrade gracefully instead.","Ensure launchers (systemd, cron, containers) export HOME/XDG_CACHE_HOME.","Fall back to tauri's app_cache_dir or temp dir when the OS cache path is unavailable.","Make tauri commands return Result and report errors to the frontend instead of panicking."],"tags":["rust","tauri","panic","filesystem","environment"],"backgroundTag":"cache-dir-unresolved","analyzedSha":"594d32ede96acd106b0256deaa8bb440ffcdff40","analyzedAt":"2026-09-02T18:12:21.811Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}