Skip to content

Commit ef227e4

Browse files
committed
error message for target specification that couldn't be queried from rustc
1 parent d5e43bb commit ef227e4

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,10 +938,22 @@ impl<'cfg> RustcTargetData<'cfg> {
938938
}
939939

940940
/// Information about the given target platform, learned by querying rustc.
941+
///
942+
/// # Panics
943+
///
944+
/// Panics, if the target platform described by `kind` can't be found.
945+
/// See [`get_info`](Self::get_info) for a non-panicking alternative.
941946
pub fn info(&self, kind: CompileKind) -> &TargetInfo {
947+
self.get_info(kind).unwrap()
948+
}
949+
950+
/// Information about the given target platform, learned by querying rustc.
951+
///
952+
/// Returns `None` if the target platform described by `kind` can't be found.
953+
pub fn get_info(&self, kind: CompileKind) -> Option<&TargetInfo> {
942954
match kind {
943-
CompileKind::Host => &self.host_info,
944-
CompileKind::Target(s) => &self.target_info[&s],
955+
CompileKind::Host => Some(&self.host_info),
956+
CompileKind::Target(s) => self.target_info.get(&s),
945957
}
946958
}
947959

src/cargo/core/compiler/compilation.rs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,7 @@ impl<'cfg> Compilation<'cfg> {
129129
.info(CompileKind::Host)
130130
.sysroot_host_libdir
131131
.clone(),
132-
sysroot_target_libdir: bcx
133-
.all_kinds
134-
.iter()
135-
.map(|&kind| {
136-
(
137-
kind,
138-
bcx.target_data.info(kind).sysroot_target_libdir.clone(),
139-
)
140-
})
141-
.collect(),
132+
sysroot_target_libdir: get_sysroot_target_libdir(bcx)?,
142133
tests: Vec::new(),
143134
binaries: Vec::new(),
144135
cdylibs: Vec::new(),
@@ -384,6 +375,48 @@ fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
384375
cmd
385376
}
386377

378+
fn get_sysroot_target_libdir(
379+
bcx: &BuildContext<'_, '_>,
380+
) -> CargoResult<HashMap<CompileKind, PathBuf>> {
381+
bcx.all_kinds
382+
.iter()
383+
.map(|&kind| {
384+
Ok((
385+
kind,
386+
bcx.target_data
387+
.get_info(kind)
388+
.ok_or_else(|| {
389+
let target = match kind {
390+
CompileKind::Host => "host".to_owned(),
391+
CompileKind::Target(s) => s.short_name().to_owned(),
392+
};
393+
394+
let dependency = bcx
395+
.unit_graph
396+
.iter()
397+
.find_map(|(k, _)| {
398+
if k.kind == kind {
399+
let summary = k.pkg.manifest().summary();
400+
401+
Some(format!("{} v{}", summary.name(), summary.version()))
402+
} else {
403+
None
404+
}
405+
})
406+
.unwrap();
407+
408+
anyhow::anyhow!(
409+
"could not find specification for target \"{target}\".\n \
410+
Dependency `{dependency}` requires to build for target \"{target}\"."
411+
)
412+
})?
413+
.sysroot_target_libdir
414+
.clone(),
415+
))
416+
})
417+
.collect::<CargoResult<HashMap<_, _>>>()
418+
}
419+
387420
fn target_runner(
388421
bcx: &BuildContext<'_, '_>,
389422
kind: CompileKind,

tests/testsuite/artifact_dep.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,7 +2880,7 @@ fn check_transitive_artifact_dependency_with_different_target() {
28802880
version = "0.0.0"
28812881
28822882
[dependencies]
2883-
baz = { path = "baz/", artifact = "bin", target = "custom" }
2883+
baz = { path = "baz/", artifact = "bin", target = "custom-target" }
28842884
"#,
28852885
)
28862886
.file("bar/src/lib.rs", "")
@@ -2899,7 +2899,10 @@ fn check_transitive_artifact_dependency_with_different_target() {
28992899

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

0 commit comments

Comments
 (0)