pot-app/pot-desktop · critical

Get Cache Dir Failed

Error message

Get Cache Dir Failed

What it means

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.

Source

Thrown at src-tauri/src/cmd.rs:28

#[tauri::command]
pub fn get_text(state: tauri::State<StringWrapper>) -> String {
    return state.0.lock().unwrap().to_string();
}

#[tauri::command]
pub fn reload_store() {
    let state = APP.get().unwrap().state::<StoreWrapper>();
    let mut store = state.0.lock().unwrap();
    store.load().unwrap();
}

#[tauri::command]
pub fn cut_image(left: u32, top: u32, width: u32, height: u32, app_handle: tauri::AppHandle) {
    use dirs::cache_dir;
    use image::GenericImage;
    info!("Cut image: {}x{}+{}+{}", width, height, left, top);
    let mut app_cache_dir_path = cache_dir().expect("Get Cache Dir Failed");
    app_cache_dir_path.push(&app_handle.config().tauri.bundle.identifier);
    app_cache_dir_path.push("pot_screenshot.png");
    if !app_cache_dir_path.exists() {
        return;
    }
    let mut img = match image::open(&app_cache_dir_path) {
        Ok(v) => v,
        Err(e) => {
            error!("{:?}", e.to_string());
            return;
        }
    };
    let img2 = img.sub_image(left, top, width, height);
    app_cache_dir_path.pop();
    app_cache_dir_path.push("pot_screenshot_cut.png");
    match img2.to_image().save(&app_cache_dir_path) {
        Ok(_) => {}
        Err(e) => {

View on GitHub (pinned to 594d32ede9)

Solutions

  1. Launch the app from a normal user session so HOME / XDG_CACHE_HOME (Linux/macOS) or the user profile (Windows) is set.
  2. Verify XDG_CACHE_HOME/HOME are exported in the launch environment (env | grep -E 'HOME|XDG_CACHE').
  3. Replace .expect with graceful handling: fall back to tauri's app_cache_dir or std::env::temp_dir when cache_dir() is None.
  4. If running headless, create/point to a writable cache dir before invoking the command.

Example fix

// before
let mut app_cache_dir_path = cache_dir().expect("Get Cache Dir Failed");
// after
let mut app_cache_dir_path = cache_dir()
    .or_else(|| dirs::home_dir().map(|h| h.join(".cache")))
    .unwrap_or_else(std::env::temp_dir);
Defensive patterns

Strategy: fallback

Validate before calling

// Before invoking the command, verify a cache dir is resolvable:
// (shell, launcher-side) test -n "$HOME" && mkdir -p "${XDG_CACHE_HOME:-$HOME/.cache}"
// (Rust, before expect)
if dirs::cache_dir().is_none() {
    eprintln!("No cache directory available; refusing cut_image");
    return;
}

Type guard

fn has_cache_dir() -> bool {
    dirs::cache_dir()
        .map(|p| p.is_dir())
        .unwrap_or(false)
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.


AI-assisted analysis of pot-app/pot-desktop@594d32ede9 (2026-09-02). Data as JSON: /api/errors/11c051b8ec4b817c. Report an issue: GitHub.