Skip to content

Commit cc0a320

Browse files
committed
Auto merge of #11434 - weihanglo:issue/10526, r=ehuss
artifact deps should works when target field specified coexists with `optional = true`
2 parents 06178d7 + eca3e23 commit cc0a320

File tree

3 files changed

+115
-5
lines changed

3 files changed

+115
-5
lines changed

src/cargo/core/compiler/unit_dependencies.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,10 @@ impl<'a, 'cfg> State<'a, 'cfg> {
11031103
// If this is an optional dependency, and the new feature resolver
11041104
// did not enable it, don't include it.
11051105
if dep.is_optional() {
1106-
let features_for = unit_for.map_to_features_for(dep.artifact());
1106+
// This `unit_for` is from parent dep and *SHOULD* contains its own
1107+
// artifact dep infomration inside `artifact_target_for_features`.
1108+
// So, no need to map any artifact info from an incorrect `dep.artifact()`.
1109+
let features_for = unit_for.map_to_features_for(IS_NO_ARTIFACT_DEP);
11071110
if !self.is_dep_activated(pkg_id, features_for, dep.name_in_toml()) {
11081111
return false;
11091112
}

src/cargo/core/resolver/features.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -853,12 +853,14 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
853853
ArtifactTarget::BuildDependencyAssumeTarget => self
854854
.requested_targets
855855
.iter()
856-
.filter_map(|kind| match kind {
857-
CompileKind::Host => None,
858-
CompileKind::Target(target) => {
859-
Some(FeaturesFor::ArtifactDep(*target))
856+
.map(|kind| match kind {
857+
CompileKind::Host => {
858+
let host_triple = self.target_data.rustc.host;
859+
CompileTarget::new(&host_triple).unwrap()
860860
}
861+
CompileKind::Target(target) => *target,
861862
})
863+
.map(FeaturesFor::ArtifactDep)
862864
.collect(),
863865
}),
864866
)

tests/testsuite/artifact_dep.rs

+105
Original file line numberDiff line numberDiff line change
@@ -2342,3 +2342,108 @@ fn calc_bin_artifact_fingerprint() {
23422342
)
23432343
.run();
23442344
}
2345+
2346+
#[cargo_test]
2347+
fn with_target_and_optional() {
2348+
// See rust-lang/cargo#10526
2349+
if cross_compile::disabled() {
2350+
return;
2351+
}
2352+
let target = cross_compile::alternate();
2353+
let p = project()
2354+
.file(
2355+
"Cargo.toml",
2356+
&r#"
2357+
[package]
2358+
name = "foo"
2359+
version = "0.0.1"
2360+
edition = "2021"
2361+
[dependencies]
2362+
d1 = { path = "d1", artifact = "bin", optional = true, target = "$TARGET" }
2363+
"#
2364+
.replace("$TARGET", target),
2365+
)
2366+
.file(
2367+
"src/main.rs",
2368+
r#"
2369+
fn main() {
2370+
let _b = include_bytes!(env!("CARGO_BIN_FILE_D1"));
2371+
}
2372+
"#,
2373+
)
2374+
.file(
2375+
"d1/Cargo.toml",
2376+
r#"
2377+
[package]
2378+
name = "d1"
2379+
version = "0.0.1"
2380+
edition = "2021"
2381+
"#,
2382+
)
2383+
.file("d1/src/main.rs", "fn main() {}")
2384+
.build();
2385+
2386+
p.cargo("check -Z bindeps -F d1 -v")
2387+
.masquerade_as_nightly_cargo(&["bindeps"])
2388+
.with_stderr(
2389+
"\
2390+
[COMPILING] d1 v0.0.1 [..]
2391+
[RUNNING] `rustc --crate-name d1 [..]--crate-type bin[..]
2392+
[CHECKING] foo v0.0.1 [..]
2393+
[RUNNING] `rustc --crate-name foo [..]--cfg[..]d1[..]
2394+
[FINISHED] dev [..]
2395+
",
2396+
)
2397+
.run();
2398+
}
2399+
2400+
#[cargo_test]
2401+
fn with_assumed_host_target_and_optional_build_dep() {
2402+
let p = project()
2403+
.file(
2404+
"Cargo.toml",
2405+
r#"
2406+
[package]
2407+
name = "foo"
2408+
version = "0.0.1"
2409+
edition = "2021"
2410+
[build-dependencies]
2411+
d1 = { path = "d1", artifact = "bin", optional = true, target = "target" }
2412+
"#,
2413+
)
2414+
.file("src/main.rs", "fn main() {}")
2415+
.file(
2416+
"build.rs",
2417+
r#"
2418+
fn main() {
2419+
std::env::var("CARGO_BIN_FILE_D1").unwrap();
2420+
}
2421+
"#,
2422+
)
2423+
.file(
2424+
"d1/Cargo.toml",
2425+
r#"
2426+
[package]
2427+
name = "d1"
2428+
version = "0.0.1"
2429+
edition = "2021"
2430+
"#,
2431+
)
2432+
.file("d1/src/main.rs", "fn main() {}")
2433+
.build();
2434+
2435+
p.cargo("check -Z bindeps -F d1 -v")
2436+
.masquerade_as_nightly_cargo(&["bindeps"])
2437+
.with_stderr_unordered(
2438+
"\
2439+
[COMPILING] foo v0.0.1 ([CWD])
2440+
[COMPILING] d1 v0.0.1 ([CWD]/d1)
2441+
[RUNNING] `rustc --crate-name build_script_build [..]--crate-type bin[..]
2442+
[RUNNING] `rustc --crate-name d1 [..]--crate-type bin[..]
2443+
[RUNNING] `[CWD]/target/debug/build/foo-[..]/build-script-build`
2444+
[RUNNING] `rustc --crate-name foo [..]--cfg[..]d1[..]
2445+
[FINISHED] dev [..]
2446+
",
2447+
)
2448+
.run();
2449+
}

0 commit comments

Comments
 (0)