FuelLabs/sway · error · anyhow::Error

Unable to fetch pkg {:?} from {:?} in offline mode

Error message

Unable to fetch pkg {:?} from  {:?} in offline mode

What it means

With offline mode enabled, the git source's pin() refuses network access and instead searches local checkouts (search_source_locally) to find an existing commit. If no cached checkout of that repo/reference exists under the git checkouts directory, pinning cannot determine a commit hash and this error names the package and repo URL.

Source

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

            Reference::Rev(s) => resolve_rev(repo, s),
        }
    }
}

impl Pinned {
    pub const PREFIX: &'static str = "git";
}

impl source::Pin for Source {
    type Pinned = Pinned;
    fn pin(&self, ctx: source::PinCtx) -> Result<(Self::Pinned, PathBuf)> {
        // If the git source directly specifies a full commit hash, we should check
        // to see if we have a local copy. Otherwise we cannot know what commit we should pin
        // to without fetching the repo into a temporary directory.
        let pinned = if ctx.offline() {
            let (_local_path, commit_hash) =
                search_source_locally(ctx.name(), self)?.ok_or_else(|| {
                    anyhow!(
                        "Unable to fetch pkg {:?} from  {:?} in offline mode",
                        ctx.name(),
                        self.repo
                    )
                })?;
            Pinned {
                source: self.clone(),
                commit_hash,
            }
        } else if let Reference::DefaultBranch | Reference::Branch(_) = self.reference {
            // If the reference is to a branch or to the default branch we need to fetch
            // from remote even though we may have it locally. Because remote may contain a
            // newer commit.
            pin(ctx.fetch_id(), ctx.name(), self.clone())?
        } else {
            // If we are in online mode and the reference is to a specific commit (tag or
            // rev) we can first search it locally and re-use it.
            match search_source_locally(ctx.name(), self) {

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. Run the same command once without --offline to populate the git checkouts cache, then go offline
  2. Pre-populate the cache (copy ~/.forc/git/checkouts from a machine that built successfully, or pre-fetch in a Docker build layer)
  3. Pin the git dependency to a rev/branch you have already fetched so search_source_locally can match it
  4. Remove --offline if network access is actually available
Defensive patterns

Strategy: fallback

Validate before calling

use std::path::Path;

fn git_dep_cached(checkouts: &Path, pkg_name: &str) -> bool {
    std::fs::read_dir(checkouts)
        .map(|entries| {
            entries.filter_map(Result::ok)
                .any(|e| e.file_name().to_string_lossy().starts_with(pkg_name))
        })
        .unwrap_or(false)
}
// before running with offline=true, require a warm cache:
// assert git_dep_cached(&home.join(".forc/git/checkouts"), dep_name);

Prevention

When it happens

Trigger: Running any forc command with --offline (offline=true in PinCtx) for the first time on a machine whose git dependencies were never fetched, or after the cache was cleaned.

Common situations: Air-gapped or CI machines requiring warm caches; new clones combined with a --offline habit; forc clean removing the checkouts directory.

Related errors


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