pkgxdev/pkgx · error · io::Error (NotFound)

Could not determine cache directory

Error message

Could not determine cache directory

What it means

`Config::new` resolves the pantry directory: `PKGX_PANTRY_DIR` env var first, else the OS local-data directory (`dirs_next::data_local_dir()`). If neither is available — no env var set and the platform provides no local data dir (rare; typically headless/service contexts where HOME is unset) — it returns a NotFound io::Error 'Could not determine cache directory'. Config construction then fails for every downstream operation.

Solutions

  1. Set PKGX_PANTRY_DIR to an absolute path (e.g. export PKGX_PANTRY_DIR=$HOME/.pkgx/pantry)
  2. Set HOME (or XDG_DATA_HOME) so dirs_next::data_local_dir() can resolve — e.g. in the systemd unit or Dockerfile set ENV HOME=/root
  3. Run pkgx as a user with a valid passwd/home entry instead of a bare service account

Example fix

// before (systemd unit)
[Service]
ExecStart=/usr/local/bin/pkgx install deno.land
// after
[Service]
Environment=HOME=/root
Environment=PKGX_PANTRY_DIR=/var/lib/pkgx/pantry
ExecStart=/usr/local/bin/pkgx install deno.land
Defensive patterns

Strategy: fallback

Validate before calling

fn pantry_dir_resolvable() -> bool {
    std::env::var("PKGX_PANTRY_DIR").is_ok()
        || dirs_next::data_local_dir().is_some()
}

Try / catch

match Config::new() {
    Err(e) if e.to_string() == "Could not determine cache directory" => {
        std::env::set_var("PKGX_PANTRY_DIR", "/var/lib/pkgx/pantry");
        Config::new()? // retry with explicit dir
    }
    other => other,
}

Prevention

When it happens

Trigger: Creating `Config::new()` on a system where `dirs_next::data_local_dir()` returns None (XDG_DATA_HOME unset and no resolvable home dir, e.g. systemd service with empty environment, cron job, container running as a user without /etc/passwd entry) and PKGX_PANTRY_DIR is not set.

Common situations: Running pkgx from a daemon/cron with no HOME, Docker containers with a minimal environment, CI steps that scrub environment variables, or users on unusual platforms where the dirs crate has no data-local-dir mapping.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of pkgxdev/pkgx@6de1d7e953 (2026-09-10). Data as JSON: /api/errors/f39c1b183a46229a. Report an issue: GitHub.

Appendix: source

Thrown at crates/lib/src/config.rs:57

        if path.is_absolute() {
            Some(path)
        } else if let Ok(cwd) = env::current_dir() {
            Some(cwd.join(path))
        } else {
            None
        }
    } else {
        None
    }
}

fn get_pantry_dir() -> io::Result<PathBuf> {
    if let Some(path) = get_PKGX_PANTRY_DIR() {
        Ok(path)
    } else if let Some(path) = dirs_next::data_local_dir() {
        Ok(path.join("pkgx/pantry"))
    } else {
        Err(io::Error::new(
            io::ErrorKind::NotFound,
            "Could not determine cache directory",
        ))
    }
}

fn get_pkgx_dir() -> io::Result<PathBuf> {
    if let Ok(path) = env::var("PKGX_DIR") {
        let path = PathBuf::from(path);
        if path.is_absolute() {
            return Ok(path);
        }
    }

    // Use sudoer home directory if it is set.
    if let Ok(path) = env::var("SUDO_HOME") {
        let path = PathBuf::from(path).join(".pkgx");
        if path.is_absolute() {

View on GitHub (pinned to 6de1d7e953)