rust-lang/cargo · error · anyhow::Error
could not compile due to {error_count} previous target resol
Error message
could not compile due to {error_count} previous target resolution error{plural} What it means
Aggregate bail emitted after per-target source-path validation (validate_target_path_as_source_file) has already printed detailed errors and incremented error_count. Each root unit whose target.src_path() resolves to a file is validated; if validation fails for any, this summary halts compilation. See src/ops/cargo_compile/mod.rs:613-632.
Source
Thrown at src/ops/cargo_compile/mod.rs:629
}
// 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 0e07a15537)
Solutions
- Read the preceding detailed error lines above this summary; they name the exact offending target and path.
- Correct the `path =` value in the target's Cargo.toml entry to point to the real .rs file.
- If the file is generated, ensure the generation step runs before compile (e.g. a build.rs / xtask) or remove the target until the file exists.
- Run `cargo build` again to confirm error_count is now 0.
Example fix
# before [[bin]] name = "cli" path = "src/cli" # directory, not a file -> resolution error # after [[bin]] name = "cli" path = "src/cli/main.rs"
Defensive patterns
Strategy: validation
Validate before calling
// Pre-check that every declared target's src path is a real .rs file.
use std::path::Path;
fn target_paths_ok(pkg: &cargo::core::Package) -> bool {
pkg.targets().iter().all(|t| {
t.src_path().path().map(|p| p.is_file()).unwrap_or(true)
})
}
// for ws in members: assert!(target_paths_ok(pkg)); Type guard
fn valid_target_src(pkg: &cargo::core::Package) -> Result<(), String> {
for t in pkg.targets() {
if let Some(p) = t.src_path().path() {
if !p.is_file() {
return Err(format!("{}: missing source {}", t.name(), p.display()));
}
}
}
Ok(())
} Try / catch
// The detailed errors are printed before the aggregate bail; capture shell output.
match ops::compile(ws, &opts) {
Err(e) if e.to_string().contains("previous target resolution error") => {
eprintln!("one or more target source paths are invalid; check lines above");
return Err(e);
}
r => r,
} Prevention
- Run `cargo check` in CI to catch bad target paths early.
- Keep `path =` in target tables in sync with the filesystem after renames.
- Ensure generated sources exist before invoking compile.
When it happens
Trigger: A target table ([[bin]], [[example]], [[test]], [[bench]]) or [lib] declares a path that does not point to a valid .rs source file (e.g. points to a directory, a non-existent file, or a file that fails the source-file checks). The loop over root_units records failures into error_count, then bails.
Common situations: Renaming/moving a source file without updating `path =` in Cargo.toml. Pointing a [[bin]] target at a directory instead of src/main.rs. A generated source file that does not yet exist at build time. Case-sensitivity mismatches on case-insensitive filesystems after a rename.
Related errors
- crate types can only be specified for libraries and example
- no library targets found in package `{}`
- crate name is empty
- dependency `{}` in package `{}` requires a `{}` artifact to
- package `{}` cannot be tested because it requires dev-depend
AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06).
Data as JSON: /data/errors/9b873ca4ac913e1d.json.
Report an issue: GitHub.