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

Could not determine data directory

Error message

Could not determine data directory

What it means

`get_pantry_db_file` locates the pantry SQLite database: `PKGX_PANTRY_DIR` env var first, else `dirs_next::cache_dir()` joined with pkgx/pantry.2.db. When neither is resolvable it returns a NotFound io::Error 'Could not determine data directory'. Note the message mismatch (it checks the *cache* dir but reports the *data* directory), which is cosmetic — the failure mode is identical to the pantry-dir case.

Solutions

  1. Set PKGX_PANTRY_DIR to an absolute path so the env-var branch is used
  2. Set HOME or XDG_CACHE_HOME so dirs_next::cache_dir() resolves
  3. Ensure the process runs as a user with a valid home directory entry

Example fix

// before (CI step)
- run: pkgx install +openssl.org
// after
- run: |
    export XDG_CACHE_HOME=$PWD/.cache
    pkgx install +openssl.org
Defensive patterns

Strategy: fallback

Validate before calling

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

Try / catch

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

Prevention

When it happens

Trigger: `Config::new()` on a system where `dirs_next::cache_dir()` returns None (no HOME / XDG_CACHE_HOME, headless service or container without a resolvable user cache dir) and PKGX_PANTRY_DIR is unset.

Common situations: Same as the cache-directory error: cron jobs, systemd services, Docker containers, or CI runners with a scrubbed environment; also macOS/Windows edge cases where the dirs crate cannot locate the cache dir.

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/411c3ef79790b0d6. Report an issue: GitHub.

Appendix: source

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

    let default = dirs_next::home_dir().map(|x| x.join(".pkgx"));

    if default.clone().is_some_and(|x| x.exists()) {
        Ok(default.unwrap())
    } else if let Ok(xdg) = env::var("XDG_DATA_HOME") {
        Ok(PathBuf::from(xdg).join("pkgx"))
    } else {
        Ok(default.unwrap())
    }
}

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

View on GitHub (pinned to 6de1d7e953)