rust-lang/cargo · error

artifact present

Error message

artifact present

What it means

`match_artifacts_kind_with_targets` iterates artifact dependency kinds via `artifact_dep.artifact().expect("artifact present")`. The function is only called for dependencies that Cargo has already classified as artifact dependencies, so `.artifact()` must return `Some`. A panic here means the dependency was treated as an artifact dependency in one place but not in another — an internal inconsistency in dependency classification.

Source

Thrown at src/compiler/artifact.rs:107

            invalid => unreachable!("BUG: artifacts cannot be of type {:?}", invalid),
        },
        TargetKind::Bin => "BIN",
        invalid => unreachable!("BUG: artifacts cannot be of type {:?}", invalid),
    }
}

/// Given a dependency with an artifact `artifact_dep` and a set of available `targets`
/// of its package, find a target for each kind of artifacts that are to be built.
///
/// Failure to match any target results in an error mentioning the parent manifests
/// `parent_package` name.
pub(crate) fn match_artifacts_kind_with_targets<'t, 'd>(
    artifact_dep: &'d Dependency,
    targets: &'t [Target],
    parent_package: &str,
) -> CargoResult<HashSet<(&'d ArtifactKind, &'t Target)>> {
    let mut out = HashSet::default();
    let artifact_requirements = artifact_dep.artifact().expect("artifact present");
    for artifact_kind in artifact_requirements.kinds() {
        let mut extend = |kind, filter: &dyn Fn(&&Target) -> bool| {
            let mut iter = targets.iter().filter(filter).peekable();
            let found = iter.peek().is_some();
            out.extend(std::iter::repeat(kind).zip(iter));
            found
        };
        let found = match artifact_kind {
            ArtifactKind::Cdylib => extend(artifact_kind, &|t| t.is_cdylib()),
            ArtifactKind::Staticlib => extend(artifact_kind, &|t| t.is_staticlib()),
            ArtifactKind::AllBinaries => extend(artifact_kind, &|t| t.is_bin()),
            ArtifactKind::SelectedBinary(bin_name) => extend(artifact_kind, &|t| {
                t.is_bin() && t.name() == bin_name.as_str()
            }),
        };
        if !found {
            anyhow::bail!(
                "dependency `{}` in package `{}` requires a `{}` artifact to be present.",

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Regenerate `Cargo.lock` (`cargo update`) so dependency metadata is consistent with the installed Cargo.
  2. Update to a matching nightly/stable Cargo that supports the artifact-dependency syntax you use.
  3. Remove and re-add the artifact dependency in `Cargo.toml` to ensure it is declared canonically.

Example fix

// before
let artifact_requirements = artifact_dep.artifact().expect("artifact present");
// after
let artifact_requirements = artifact_dep.artifact().ok_or_else(|| {
    anyhow::anyhow!(
        "dependency `{}` was classified as an artifact dep but carries no artifact requirements",
        artifact_dep.name_in_toml()
    )
})?;
Defensive patterns

Strategy: validation

Validate before calling

// (cargo-internal) before classify, confirm dependency has artifact requirements:
// if dep.is_artifact() { assert!(dep.artifact().is_some()); }

Prevention

When it happens

Trigger: A dependency recorded with an artifact kind in the resolver but whose `Dependency::artifact()` returns `None` (e.g. a manifest edited/parsed inconsistently, or a custom resolver path that sets `artifact` only partially). Reachable with malformed `[artifactDependencies]` / `-Zbindeps` manifests on a Cargo version with a classification bug.

Common situations: Experimenting with artifact dependencies (`-Zbindeps`) on nightly; upgrading Cargo across a change to the artifact-dependency data model; hand-editing `Cargo.lock` or manifests.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/d15c018cd20d7534.json. Report an issue: GitHub.