Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message for transitive artifact dependencies with targets the package doesn't directly interact with #11643

Merged
merged 7 commits into from
Feb 25, 2023
Prev Previous commit
Next Next commit
error message for target specification that couldn't be queried from …
…rustc
  • Loading branch information
jofas committed Jan 28, 2023
commit ef227e401de5941baf5c8524b2858a1246a604dc
16 changes: 14 additions & 2 deletions src/cargo/core/compiler/build_context/target_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -938,10 +938,22 @@ impl<'cfg> RustcTargetData<'cfg> {
}

/// Information about the given target platform, learned by querying rustc.
///
/// # Panics
///
/// Panics, if the target platform described by `kind` can't be found.
/// See [`get_info`](Self::get_info) for a non-panicking alternative.
pub fn info(&self, kind: CompileKind) -> &TargetInfo {
self.get_info(kind).unwrap()
}

/// Information about the given target platform, learned by querying rustc.
///
/// Returns `None` if the target platform described by `kind` can't be found.
pub fn get_info(&self, kind: CompileKind) -> Option<&TargetInfo> {
match kind {
CompileKind::Host => &self.host_info,
CompileKind::Target(s) => &self.target_info[&s],
CompileKind::Host => Some(&self.host_info),
CompileKind::Target(s) => self.target_info.get(&s),
}
}

Expand Down
53 changes: 43 additions & 10 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,7 @@ impl<'cfg> Compilation<'cfg> {
.info(CompileKind::Host)
.sysroot_host_libdir
.clone(),
sysroot_target_libdir: bcx
.all_kinds
.iter()
.map(|&kind| {
(
kind,
bcx.target_data.info(kind).sysroot_target_libdir.clone(),
)
})
.collect(),
sysroot_target_libdir: get_sysroot_target_libdir(bcx)?,
tests: Vec::new(),
binaries: Vec::new(),
cdylibs: Vec::new(),
Expand Down Expand Up @@ -384,6 +375,48 @@ fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
cmd
}

fn get_sysroot_target_libdir(
bcx: &BuildContext<'_, '_>,
) -> CargoResult<HashMap<CompileKind, PathBuf>> {
bcx.all_kinds
.iter()
.map(|&kind| {
jofas marked this conversation as resolved.
Show resolved Hide resolved
Ok((
kind,
bcx.target_data
.get_info(kind)
.ok_or_else(|| {
let target = match kind {
CompileKind::Host => "host".to_owned(),
CompileKind::Target(s) => s.short_name().to_owned(),
};

let dependency = bcx
.unit_graph
.iter()
.find_map(|(k, _)| {
if k.kind == kind {
let summary = k.pkg.manifest().summary();

Some(format!("{} v{}", summary.name(), summary.version()))
} else {
None
}
})
jofas marked this conversation as resolved.
Show resolved Hide resolved
.unwrap();

anyhow::anyhow!(
"could not find specification for target \"{target}\".\n \
Dependency `{dependency}` requires to build for target \"{target}\"."
jofas marked this conversation as resolved.
Show resolved Hide resolved
)
})?
.sysroot_target_libdir
.clone(),
))
})
.collect::<CargoResult<HashMap<_, _>>>()
jofas marked this conversation as resolved.
Show resolved Hide resolved
}

fn target_runner(
bcx: &BuildContext<'_, '_>,
kind: CompileKind,
Expand Down
7 changes: 5 additions & 2 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2880,7 +2880,7 @@ fn check_transitive_artifact_dependency_with_different_target() {
version = "0.0.0"

[dependencies]
baz = { path = "baz/", artifact = "bin", target = "custom" }
baz = { path = "baz/", artifact = "bin", target = "custom-target" }
"#,
)
.file("bar/src/lib.rs", "")
Expand All @@ -2899,7 +2899,10 @@ fn check_transitive_artifact_dependency_with_different_target() {

p.cargo("check -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr_contains(r#"thread 'main' panicked at 'no entry found for key'[..]"#)
.with_stderr_contains(
"error: could not find specification for target \"custom-target\".\n \
Dependency `baz v0.0.0` requires to build for target \"custom-target\".",
)
.with_status(101)
.run();
}