rust-lang/rust · error · anyhow::Error

rustc metadata did not contain commit hash

Error message

rustc metadata did not contain commit hash

What it means

Returned from toolchain() when rustc_version::version_meta_for parses the `rustc +miri --version -v` output but the resulting VersionMeta has commit_hash == None. The script needs the commit hash to decide whether the installed 'miri' toolchain already matches the target commit, so it aborts.

Source

Thrown at src/tools/miri/miri-script/src/commands.rs:143

            Command::Squash => Self::squash(),
        }
    }

    fn toolchain(new_commit: Option<String>, flags: Vec<String>) -> Result<()> {
        let sh = Shell::new()?;
        sh.change_dir(miri_dir()?);
        let new_commit = match new_commit {
            Some(c) => c,
            None => sh.read_file("rust-version")?.trim().to_owned(),
        };
        let current_commit = {
            let rustc_info = cmd!(sh, "rustc +miri --version -v").read();
            if let Ok(rustc_info) = rustc_info {
                let metadata = rustc_version::version_meta_for(&rustc_info)?;
                Some(
                    metadata
                        .commit_hash
                        .ok_or_else(|| anyhow!("rustc metadata did not contain commit hash"))?,
                )
            } else {
                None
            }
        };
        // Check if we already are at that commit.
        if current_commit.as_ref() == Some(&new_commit) {
            if active_toolchain()? != "miri" {
                cmd!(sh, "rustup override set miri").run()?;
            }
            return Ok(());
        }
        // Install and setup new toolchain.
        cmd!(sh, "rustup toolchain uninstall miri").run()?;

        cmd!(sh, "rustup-toolchain-install-master -n miri -c cargo -c rust-src -c rustc-dev -c llvm-tools -c rustfmt -c clippy {flags...} -- {new_commit}")
            .run()
            .context("Failed to run rustup-toolchain-install-master. If it is not installed, run 'cargo install rustup-toolchain-install-master'.")?;

View on GitHub (pinned to 7088e4b63a)

Solutions

  1. Install a rustc that includes commit metadata (official rustup nightly, or a rust built from a clean git checkout with the metadata embedded).
  2. Pass the commit explicitly to bypass detection: `miri-script toolchain <commit>`.
  3. Ensure the `rust-version` file in the miri tree points at a commit available via rustup-toolchain-install-master.

Example fix

// before
let metadata = rustc_version::version_meta_for(&rustc_info)?;
Some(metadata.commit_hash.ok_or_else(||
    anyhow!("rustc metadata did not contain commit hash"))?,
)

// after: fall back to the file's commit when metadata is incomplete
let commit = metadata.commit_hash
    .or_else(|| std::fs::read_to_string("rust-version").ok()?.trim().to_owned().into())
    .ok_or_else(|| anyhow!(
        "rustc metadata did not contain commit hash; \
         pass one explicitly with `miri-script toolchain <commit>`"
    ))?;
Defensive patterns

Strategy: fallback

Validate before calling

// Detect missing commit hash up front and decide whether to bypass detection:
let has_hash = std::process::Command::new("rustc")
    .arg("+miri").arg("--version").arg("-v").output()
    .map(|o| String::from_utf8_lossy(&o.stdout).contains("commit-hash:")).unwrap_or(false);
if !has_hash { /* pass <commit> explicitly to miri-script toolchain */ }

Type guard

null

Try / catch

match miri_script::Command::toolchain(commit.clone(), flags) {
    Ok(()) => Ok(()),
    Err(e) if e.to_string().contains("commit hash") => {
        eprintln!("re-run with an explicit commit: miri-script toolchain <commit>");
        Err(e)
    }
    Err(e) => Err(e),
}

Prevention

When it happens

Trigger: Running `miri-script toolchain` (or any path that calls it) where rustc was built without embedding commit metadata — typically a distro or custom rustc build, or a rustc built with release-debuginfo stripped.

Common situations: Using a locally-built rustc without the commit-hash git embedding; a distro rustc package that omits verbose metadata; running against a rustc stage1 build that lacks the full version string.

Related errors


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/dba286ab3965c29a. Report an issue: GitHub.