Skip to content

Commit 92cce52

Browse files
committed
reproduce: wrong features in a build script for a shared dependency (#10452)
1 parent 4e847a2 commit 92cce52

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

tests/testsuite/artifact_dep.rs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,3 +2153,114 @@ fn build_script_output_string(p: &Project, package_name: &str) -> String {
21532153
assert_eq!(paths.len(), 1);
21542154
std::fs::read_to_string(&paths[0]).unwrap()
21552155
}
2156+
2157+
#[cargo_test]
2158+
#[ignore]
2159+
fn build_script_features_for_shared_dependency() {
2160+
// When a build script is built and run, its features should match. Here:
2161+
//
2162+
// foo
2163+
// -> artifact on d1 with target
2164+
// -> common with features f1
2165+
//
2166+
// d1
2167+
// -> common with features f2
2168+
//
2169+
// common has features f1 and f2, with a build script.
2170+
//
2171+
// When common is built as a dependency of d1, it should have features
2172+
// `f2` (for the library and the build script).
2173+
//
2174+
// When common is built as a dependency of foo, it should have features
2175+
// `f1` (for the library and the build script).
2176+
if cross_compile::disabled() {
2177+
return;
2178+
}
2179+
let target = cross_compile::alternate();
2180+
let p = project()
2181+
.file(
2182+
"Cargo.toml",
2183+
&r#"
2184+
[project]
2185+
name = "foo"
2186+
version = "0.0.1"
2187+
resolver = "2"
2188+
2189+
[dependencies]
2190+
d1 = { path = "d1", artifact = "bin", target = "$TARGET" }
2191+
common = { path = "common", features = ["f1"] }
2192+
"#
2193+
.replace("$TARGET", target),
2194+
)
2195+
.file(
2196+
"src/main.rs",
2197+
r#"
2198+
fn main() {
2199+
let _b = include_bytes!(env!("CARGO_BIN_FILE_D1"));
2200+
common::f1();
2201+
}
2202+
"#,
2203+
)
2204+
.file(
2205+
"d1/Cargo.toml",
2206+
r#"
2207+
[package]
2208+
name = "d1"
2209+
version = "0.0.1"
2210+
2211+
[dependencies]
2212+
common = { path = "../common", features = ["f2"] }
2213+
"#,
2214+
)
2215+
.file(
2216+
"d1/src/main.rs",
2217+
r#"fn main() {
2218+
common::f2();
2219+
}"#,
2220+
)
2221+
.file(
2222+
"common/Cargo.toml",
2223+
r#"
2224+
[package]
2225+
name = "common"
2226+
version = "0.0.1"
2227+
2228+
[features]
2229+
f1 = []
2230+
f2 = []
2231+
"#,
2232+
)
2233+
.file(
2234+
"common/src/lib.rs",
2235+
r#"
2236+
#[cfg(feature = "f1")]
2237+
pub fn f1() {}
2238+
2239+
#[cfg(feature = "f2")]
2240+
pub fn f2() {}
2241+
"#,
2242+
)
2243+
.file(
2244+
"common/build.rs",
2245+
&r#"
2246+
use std::env::var_os;
2247+
fn main() {
2248+
assert_eq!(var_os("CARGO_FEATURE_F1").is_some(), cfg!(feature="f1"));
2249+
assert_eq!(var_os("CARGO_FEATURE_F2").is_some(), cfg!(feature="f2"));
2250+
if std::env::var("TARGET").unwrap() == "$TARGET" {
2251+
assert!(var_os("CARGO_FEATURE_F1").is_none());
2252+
assert!(var_os("CARGO_FEATURE_F2").is_some());
2253+
} else {
2254+
assert!(var_os("CARGO_FEATURE_F1").is_some());
2255+
assert!(var_os("CARGO_FEATURE_F2").is_none());
2256+
}
2257+
}
2258+
"#
2259+
.replace("$TARGET", target),
2260+
)
2261+
.build();
2262+
2263+
p.cargo("build -Z bindeps -v")
2264+
.masquerade_as_nightly_cargo()
2265+
.run();
2266+
}

0 commit comments

Comments
 (0)