FuelLabs/sway · error · anyhow::Error

Cannot get commit from {}

Error message

Cannot get commit from {}

What it means

After fetching and checking out a git dependency, forc resolves HEAD via revparse_single and expects a commit object to timestamp the SourceIndex. If HEAD does not point at a commit (or the fetch produced an empty/detached repository), the object id is reported with this error.

Source

Thrown at forc-pkg/src/source/git/mod.rs:541

        repo.set_head_detached(id)?;

        // If the directory exists, remove it. Note that we already check for an existing,
        // cached checkout directory for re-use prior to reaching the `fetch` function.
        if path.exists() {
            let _ = fs::remove_dir_all(&path);
        }
        fs::create_dir_all(&path)?;

        // Checkout HEAD to the target directory.
        let mut checkout = git2::build::CheckoutBuilder::new();
        checkout.force().target_dir(&path);
        repo.checkout_head(Some(&mut checkout))?;

        // Fetch HEAD time and create an index
        let current_head = repo.revparse_single("HEAD")?;
        let head_commit = current_head
            .as_commit()
            .ok_or_else(|| anyhow!("Cannot get commit from {}", current_head.id()))?;
        let head_time = head_commit.time().seconds();
        let source_index = SourceIndex::new(
            head_time,
            pinned.source.reference.clone(),
            pinned.commit_hash.clone(),
        );

        // Write the index file
        fs::write(
            path.join(".forc_index"),
            serde_json::to_string(&source_index)?,
        )?;
        Ok(())
    })?;
    Ok(path)
}

/// Search local checkout dir for git sources, for non-branch git references tries to find the

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. Delete the cached repo/checkout directories for that dependency under ~/.forc/git and rebuild to re-fetch
  2. Verify the ref actually points to a commit: `git ls-remote <url> <branch-or-tag>`
  3. If it persists on stock forc, report it with the dependency declaration
Defensive patterns

Strategy: retry

Try / catch

match fetch_git_dep(&source) {
    Ok(m) => m,
    Err(e) if e.to_string().contains("Cannot get commit from") => {
        // corrupt cached HEAD: purge this repo's cache entries and retry once
        std::fs::remove_dir_all(&repo_cache_dir)?;
        fetch_git_dep(&source)?
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: A corrupted or truncated fetch leaving HEAD dangling; a cached repo directory tampered with; stale checkouts from incompatible forc versions.

Common situations: An interrupted forc build during fetch; third-party tools manipulating ~/.forc/git; disk corruption in the cache.

Related errors


AI-assisted analysis of FuelLabs/sway@47e5e902fa (2026-08-16). Data as JSON: /api/errors/afa19eaf9579a85a. Report an issue: GitHub.