xai-org/grok-build · error · anyhow::Error

failed to load git index: {e}

Error message

failed to load git index: {e}

What it means

count_tracked_files loads the git index via `index_or_load_from_head()` after discovery succeeds; failure to load or parse the index is mapped to this error. Since it falls back to the HEAD tree's index, this usually indicates a corrupt or unreadable index.

Source

Thrown at crates/codegen/xai-fast-worktree/src/lib.rs:113

}
pub fn local_clean_artifacts(_dest: &std::path::Path) -> anyhow::Result<CleanArtifactsReply> {
    anyhow::bail!("not available in this build")
}
pub use sync::{SourceDirtyState, SyncReport, WorktreeSync, collect_source_dirty_state};
#[cfg(target_os = "linux")]
pub use worktree::execute::cleanup_snapshot_git_state;
pub use worktree::{STRATEGY_GROVE_FUSE, STRATEGY_GROVE_NFS, STRATEGY_NFS, is_grove_strategy};
/// Count the number of tracked files in a git repository's index.
///
/// Reads the index header via `gix`, which contains the entry count — this
/// is an O(1) read (no directory walk). Useful for deciding whether a repo
/// is large enough to benefit from worktree pooling.
pub fn count_tracked_files(repo_path: &std::path::Path) -> anyhow::Result<usize> {
    let repo = gix::discover(repo_path)
        .map_err(|e| anyhow::anyhow!("failed to discover git repo: {e}"))?;
    let index = repo
        .index_or_load_from_head()
        .map_err(|e| anyhow::anyhow!("failed to load git index: {e}"))?;
    Ok(index.entries().len())
}

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Run `git status` or `git read-tree HEAD` to verify/repair the index
  2. Delete `.git/index` and restore it with `git reset --mixed HEAD` (regenerates from HEAD)
  3. Ensure the repository has at least one commit or a valid index file
  4. Check for concurrent writers locking the index (remove stale `.git/index.lock` if no git process is running)

Example fix

// shell repair
rm .git/index && git reset --mixed HEAD
// then retry
count_tracked_files(&repo_path)?;
Defensive patterns

Strategy: retry

Validate before calling

fn index_loadable(path: &Path) -> bool {
    std::process::Command::new("git").arg("-C").arg(path)
        .args(["rev-parse", "--verify", "HEAD"])
        .output().map(|o| o.status.success()).unwrap_or(false)
}

Try / catch

let count = loop {
    match count_tracked_files(repo_path) {
        Ok(n) => break n,
        Err(e) if e.to_string().contains("failed to load git index") && retries < 2 => {
            retries += 1;
            std::thread::sleep(Duration::from_millis(100)); // wait out concurrent index writer
        }
        Err(e) => return Err(e),
    }
};

Prevention

When it happens

Trigger: Calling count_tracked_files on a repo whose `.git/index` is corrupt, truncated, or has an unsupported version, or where HEAD cannot be resolved to build the fallback index (e.g. unborn HEAD with no index file).

Common situations: Crash or power loss mid `git add` leaving a truncated index; concurrent index writers; empty freshly-`init`-ed repos with no commits and no index; index version newer than the reader supports.

Related errors


AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31). Data as JSON: /api/errors/cc0cc850ac26d2ed. Report an issue: GitHub.