sinelaw/fresh · critical · io::Error (NotFound)

Could not determine data directory

Error message

Could not determine data directory

What it means

DirectoryContext::from_system fails when the `dirs` crate cannot locate the OS user data directory (e.g. XDG_DATA_HOME / ~/Library/Application Support / %APPDATA%). The library throws this in main()-time initialization because it cannot build its data directory path. It indicates an environment problem, not a code bug.

Solutions

  1. Set XDG_DATA_HOME (or $HOME) in the environment before launching the editor.
  2. For services, use Environment=HOME=/home/user or an EnvFile in the systemd unit.
  3. Ensure the user has a valid passwd entry (check `getent passwd $(whoami)`).

Example fix

// before
DirectoryContext::from_system()?;
// after
std::env::set_var("XDG_DATA_HOME", "/tmp/app-data"); // or ensure HOME is set in the service unit
DirectoryContext::from_system()?;
Defensive patterns

Strategy: fallback

Validate before calling

if std::env::var("XDG_DATA_HOME").is_err() && std::env::var("HOME").is_err() {
    eprintln!("HOME/XDG_DATA_HOME unset; data directory cannot be resolved");
}

Try / catch

match DirectoryContext::from_system() {
    Ok(ctx) => ctx,
    Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
        std::env::set_var("XDG_DATA_HOME", "/tmp/fresh-data");
        DirectoryContext::from_system().expect("fallback data dir")
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling DirectoryContext::from_system() on a system where dirs::data_dir() returns None: missing XDG_DATA_HOME with no resolvable home directory, or running under a stripped environment (cron, systemd service, container) where $HOME is unset.

Common situations: Running the editor as a system service without HOME set; launching from a minimal Docker image; SSH into a system with no passwd entry for the user.

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 sinelaw/fresh@67894ca546 (2026-09-13). Data as JSON: /api/errors/b1ef195b32884d49. Report an issue: GitHub.

Appendix: source

Thrown at crates/fresh-editor-core/src/config_io.rs:1058

    pub config_dir: std::path::PathBuf,

    /// User's home directory (for file open dialog shortcuts)
    pub home_dir: Option<std::path::PathBuf>,

    /// User's documents directory (for file open dialog shortcuts)
    pub documents_dir: Option<std::path::PathBuf>,

    /// User's downloads directory (for file open dialog shortcuts)
    pub downloads_dir: Option<std::path::PathBuf>,
}

impl DirectoryContext {
    /// Create a DirectoryContext from the system directories
    /// This should ONLY be called from main()
    pub fn from_system() -> std::io::Result<Self> {
        let data_dir = dirs::data_dir()
            .ok_or_else(|| {
                std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "Could not determine data directory",
                )
            })?
            .join("fresh");

        let config_dir = Self::default_config_dir().ok_or_else(|| {
            std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "Could not determine config directory",
            )
        })?;

        Ok(Self {
            data_dir,
            config_dir,
            home_dir: dirs::home_dir(),
            documents_dir: dirs::document_dir(),

View on GitHub (pinned to 67894ca546)