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

could not compile due to

Error message

could not compile due to {error_count} previous target resolution error{plural}

What it means

Aggregate failure thrown after the per-unit validate_target_path_as_source_file loop accumulated one or more target-resolution errors. Each individual problem was already printed; this bail converts the nonzero error_count into a single terminating message with correct pluralization.

Solutions

  1. Scroll up in the build output and read each individual target resolution error, fixing them one by one.
  2. Correct the target `path` values in Cargo.toml to point at existing source files of the right type.
  3. Regenerate any build-script-produced source files the targets depend on before building.
  4. Remove stale [[bin]]/[[example]]/[[test]] entries for deleted sources.

Example fix

# before (Cargo.toml)
[[bin]]
name = "app"
path = "src/main.rs2"
# after
[[bin]]
name = "app"
path = "src/main.rs"
Defensive patterns

Strategy: validation

Validate before calling

// Lint manifest target paths before building.
use std::path::Path;
fn target_paths_exist(manifest: &Path) -> Result<(), String> {
    // parse [[bin]]/[[example]]/[[test]]/[[bench]].path, check is_file
    Ok(())
}

Prevention

When it happens

Trigger: One or more build targets referenced a source path that failed validation (non-existent file, wrong extension, path is a directory, etc.) during unit construction. The detailed per-target causes are emitted just before this summary.

Common situations: Manifest targets pointing at moved/renamed source files; `path = "src/main.rs2"` typos in [[bin]]/[[example]] tables; targets referencing generated files that have not been generated yet; switching editions/modes that change expected paths.

Related errors


AI-assisted analysis of rust-lang/cargo@495c385d08 (2026-08-11). Data as JSON: /api/errors/9b873ca4ac913e1d. Report an issue: GitHub.

Appendix: source

Thrown at src/ops/cargo_compile/mod.rs:637

    }

    // Validate target src path for each root unit
    let mut error_count: usize = 0;
    for unit in &root_units {
        if let Some(target_src_path) = unit.target.src_path().path() {
            validate_target_path_as_source_file(
                gctx,
                target_src_path,
                unit.target.name(),
                unit.target.kind(),
                unit.pkg.manifest_path(),
                &mut error_count,
            )?
        }
    }
    if error_count > 0 {
        let plural: &str = if error_count > 1 { "s" } else { "" };
        anyhow::bail!(
            "could not compile due to {error_count} previous target resolution error{plural}"
        );
    }

    if honor_rust_version.unwrap_or(true) {
        let rustc_version = target_data.rustc.version.clone().into();

        let mut incompatible = Vec::new();
        let mut local_incompatible = false;
        for unit in unit_graph.keys() {
            let Some(pkg_msrv) = unit.pkg.rust_version() else {
                continue;
            };

            if pkg_msrv.is_compatible_with(&rustc_version) {
                continue;
            }

View on GitHub (pinned to 495c385d08)