Skip to content

Commit 816772b

Browse files
committed
Auto merge of #11643 - jofas:11260-fix, r=weihanglo
Error message for transitive artifact dependencies with targets the package doesn't directly interact with Address #11260. Produces an error message like described by `@weihanglo` [here](#11260 (comment)): ``` error: could not find specification for target "x86_64-windows-msvc" Dependency `bar v0.1.0` requires to build for target "x86_64-windows-msvc". ``` Note that this is not a complete fix for #11260.
2 parents 65cab34 + 4bdface commit 816772b

File tree

3 files changed

+169
-87
lines changed

3 files changed

+169
-87
lines changed

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -984,10 +984,22 @@ impl<'cfg> RustcTargetData<'cfg> {
984984
}
985985

986986
/// Information about the given target platform, learned by querying rustc.
987+
///
988+
/// # Panics
989+
///
990+
/// Panics, if the target platform described by `kind` can't be found.
991+
/// See [`get_info`](Self::get_info) for a non-panicking alternative.
987992
pub fn info(&self, kind: CompileKind) -> &TargetInfo {
993+
self.get_info(kind).unwrap()
994+
}
995+
996+
/// Information about the given target platform, learned by querying rustc.
997+
///
998+
/// Returns `None` if the target platform described by `kind` can't be found.
999+
pub fn get_info(&self, kind: CompileKind) -> Option<&TargetInfo> {
9881000
match kind {
989-
CompileKind::Host => &self.host_info,
990-
CompileKind::Target(s) => &self.target_info[&s],
1001+
CompileKind::Host => Some(&self.host_info),
1002+
CompileKind::Target(s) => self.target_info.get(&s),
9911003
}
9921004
}
9931005

src/cargo/core/compiler/compilation.rs

+30-10
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,7 @@ impl<'cfg> Compilation<'cfg> {
130130
.info(CompileKind::Host)
131131
.sysroot_host_libdir
132132
.clone(),
133-
sysroot_target_libdir: bcx
134-
.all_kinds
135-
.iter()
136-
.map(|&kind| {
137-
(
138-
kind,
139-
bcx.target_data.info(kind).sysroot_target_libdir.clone(),
140-
)
141-
})
142-
.collect(),
133+
sysroot_target_libdir: get_sysroot_target_libdir(bcx)?,
143134
tests: Vec::new(),
144135
binaries: Vec::new(),
145136
cdylibs: Vec::new(),
@@ -385,6 +376,35 @@ fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
385376
cmd
386377
}
387378

379+
fn get_sysroot_target_libdir(
380+
bcx: &BuildContext<'_, '_>,
381+
) -> CargoResult<HashMap<CompileKind, PathBuf>> {
382+
bcx.all_kinds
383+
.iter()
384+
.map(|&kind| {
385+
let Some(info) = bcx.target_data.get_info(kind) else {
386+
let target = match kind {
387+
CompileKind::Host => "host".to_owned(),
388+
CompileKind::Target(s) => s.short_name().to_owned(),
389+
};
390+
391+
let dependency = bcx
392+
.unit_graph
393+
.iter()
394+
.find_map(|(u, _)| (u.kind == kind).then_some(u.pkg.summary().package_id()))
395+
.unwrap();
396+
397+
anyhow::bail!(
398+
"could not find specification for target `{target}`.\n \
399+
Dependency `{dependency}` requires to build for target `{target}`."
400+
)
401+
};
402+
403+
Ok((kind, info.sysroot_target_libdir.clone()))
404+
})
405+
.collect()
406+
}
407+
388408
fn target_runner(
389409
bcx: &BuildContext<'_, '_>,
390410
kind: CompileKind,

0 commit comments

Comments
 (0)