jlcodes99/cockpit-tools · critical

无法获取用户目录

Error message

无法获取用户目录

What it means

get_accounts_storage_path builds the multi-account index path under ~/.antigravity_cockpit/. When account::get_data_dir() returns None it falls back to dirs::home_dir().expect("无法获取用户目录"), panicking if the OS home directory cannot be resolved. Returns the codex_accounts.json index path.

Source

Thrown at crates/cockpit-core/src/modules/codex_account_core_tokens.rs:7

// cockpit-core Codex 账号:Token identity, refresh state, account index and lifecycle。
// 通过 include! 保持原模块作用域和凭据调用路径。
/// 获取我们的多账号存储路径(统一使用 ~/.antigravity_cockpit/)
fn get_accounts_storage_path() -> PathBuf {
    let data_dir = account::get_data_dir().unwrap_or_else(|_| {
        dirs::home_dir()
            .expect("无法获取用户目录")
            .join(".antigravity_cockpit")
    });
    fs::create_dir_all(&data_dir).ok();
    migrate_codex_data_if_needed(&data_dir);
    data_dir.join("codex_accounts.json")
}

/// 获取账号详情存储目录(统一使用 ~/.antigravity_cockpit/codex_accounts/)
fn get_accounts_dir() -> PathBuf {
    let data_dir = account::get_data_dir().unwrap_or_else(|_| {
        dirs::home_dir()
            .expect("无法获取用户目录")
            .join(".antigravity_cockpit")
    });
    let accounts_dir = data_dir.join("codex_accounts");
    fs::create_dir_all(&accounts_dir).ok();
    accounts_dir
}

View on GitHub (pinned to 1ed8b77992)

Solutions

  1. Set HOME (Unix) or USERPROFILE (Windows) to a writable directory before running.
  2. Configure the data-dir override that account::get_data_dir() honors so the home fallback is never taken.
  3. Pre-check the environment and fail fast with a clear message instead of letting the panic surface mid-operation.
  4. Run import/index operations only in a proper user session.

Example fix

// before
let path = get_accounts_storage_path(); // panics if no home

// after
if std::env::var_os("HOME").is_none()
    && std::env::var_os("USERPROFILE").is_none()
{
    anyhow::bail!("home directory not set; cannot resolve account storage");
}
let path = get_accounts_storage_path();
Defensive patterns

Strategy: validation

Validate before calling

fn account_storage_env_ok() -> bool {
    std::env::var_os("HOME").is_some() || std::env::var_os("USERPROFILE").is_some()
}

Try / catch

std::panic::catch_unwind(get_accounts_storage_path).map_err(|_| anyhow::anyhow!("无法获取用户目录"))

Prevention

When it happens

Trigger: Calling get_accounts_storage_path (via load_account_index, save_account_index, ensure_storage_writable_for_import, etc.) when get_data_dir() yields None and dirs::home_dir() is None — no resolvable home directory.

Common situations: CI/test runs without HOME set (note test_env_guard_isolates_and_restores_cockpit_data_dir is a listed caller); service accounts without user profiles; containers with minimal env.

Related errors


AI-assisted analysis of jlcodes99/cockpit-tools@1ed8b77992 (2026-09-05). Data as JSON: /api/errors/761012b05217a8e6. Report an issue: GitHub.