jdx/mise · error

apt-cache policy failed: {}

Error message

apt-cache policy failed: {}

What it means

The apt backend calls `apt-cache policy` to check whether a package is installable (available()). Unknown package names are reported on stderr without failing the command, so a nonzero exit status means apt-cache itself failed to run, and src/system/packages/apt.rs:50 bails with the trimmed stderr.

Source

Thrown at src/system/packages/apt.rs:50

    /// holding an arch-qualified name must query it alone.
    async fn policy_installable(&self, args: &[&str]) -> Result<std::collections::HashSet<String>> {
        debug!("$ apt-cache policy {}", args.join(" "));
        let output = tokio::process::Command::new("apt-cache")
            .arg("policy")
            .args(args)
            // apt translates the stanza labels this parses, so pin the locale
            // rather than reading "Kandidat:" as an unavailable package
            .env("LC_ALL", "C")
            .env("LANGUAGE", "C")
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .output()
            .await?;
        // unknown names are reported on stderr and do not fail the command,
        // so a nonzero status means apt-cache itself could not run
        if !output.status.success() {
            bail!(
                "apt-cache policy failed: {}",
                String::from_utf8_lossy(&output.stderr).trim()
            );
        }
        Ok(parse_apt_cache_policy(&String::from_utf8_lossy(
            &output.stdout,
        )))
    }

    fn update(&self, opts: &InstallOpts) -> Result<()> {
        let args = vec!["update".to_string()];
        if opts.dry_run {
            miseprintln!(
                "{}",
                sudo::argv_with_env("apt-get", &args, &debian_frontend()).join(" ")
            );
            return Ok(());
        }

View on GitHub (pinned to afd2eddd3a)

Solutions

  1. Read the stderr detail in the message and fix the apt issue it reports (e.g. rebuild lists with `apt-get update`)
  2. Run `apt-cache policy <package>` manually to reproduce and see the full error
  3. Repair apt configuration: check /etc/apt/sources.list* validity and run `apt-get -f install` / `dpkg --configure -a` if dpkg state is broken
  4. Ensure apt/dpkg are correctly installed before using the apt package manager (skip it on non-Debian systems)
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure apt-cache works before using the apt manager
command -v apt-cache >/dev/null || echo "apt-cache missing"
apt-cache policy >/dev/null 2>&1 || echo "apt-cache broken — run apt-get update"

Try / catch

match result {
    Err(e) if e.to_string().starts_with("apt-cache policy failed:") => {
        // treat apt availability as unknown; repair apt (apt-get update) or skip apt packages
    }
    r => r?,
}

Prevention

When it happens

Trigger: Calling available()/policy_installable() for apt packages when `apt-cache policy <pkgs>` exits nonzero: apt-cache missing or broken, corrupted apt lists, or a dpkg/apt configuration error preventing apt-cache from starting.

Common situations: Running on a Debian/Ubuntu system with a broken or never-populated /var/lib/apt/lists; a misconfigured apt sources file; running inside a minimal container where apt is installed but its cache was cleaned; PATH or sandboxing issues preventing apt-cache from executing properly.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of jdx/mise@afd2eddd3a (2026-09-09). Data as JSON: /api/errors/2b6122e159fe440a. Report an issue: GitHub.