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

artifact deps should works when target field specified coexists with optional = true #11434

Merged
merged 5 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ impl<'a, 'cfg> State<'a, 'cfg> {
// If this is an optional dependency, and the new feature resolver
// did not enable it, don't include it.
if dep.is_optional() {
let features_for = unit_for.map_to_features_for(dep.artifact());
let features_for = unit_for.map_to_features_for(None);
weihanglo marked this conversation as resolved.
Show resolved Hide resolved
if !self.is_dep_activated(pkg_id, features_for, dep.name_in_toml()) {
return false;
}
Expand Down
10 changes: 6 additions & 4 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,14 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
ArtifactTarget::BuildDependencyAssumeTarget => self
.requested_targets
.iter()
.filter_map(|kind| match kind {
CompileKind::Host => None,
CompileKind::Target(target) => {
Some(FeaturesFor::ArtifactDep(*target))
.map(|kind| match kind {
CompileKind::Host => {
let host_triple = self.target_data.rustc.host;
CompileTarget::new(&host_triple).unwrap()
}
CompileKind::Target(target) => *target,
})
.map(FeaturesFor::ArtifactDep)
.collect(),
}),
)
Expand Down
105 changes: 105 additions & 0 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2342,3 +2342,108 @@ fn calc_bin_artifact_fingerprint() {
)
.run();
}

#[cargo_test]
fn with_target_and_optional() {
// See rust-lang/cargo#10526
if cross_compile::disabled() {
return;
}
let target = cross_compile::alternate();
let p = project()
.file(
"Cargo.toml",
&r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2021"
[dependencies]
d1 = { path = "d1", artifact = "bin", optional = true, target = "$TARGET" }
"#
.replace("$TARGET", target),
)
.file(
"src/main.rs",
r#"
fn main() {
let _b = include_bytes!(env!("CARGO_BIN_FILE_D1"));
}
"#,
)
.file(
"d1/Cargo.toml",
r#"
[package]
name = "d1"
version = "0.0.1"
edition = "2021"
"#,
)
.file("d1/src/main.rs", "fn main() {}")
.build();

p.cargo("check -Z bindeps -F d1 -v")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr(
"\
[COMPILING] d1 v0.0.1 [..]
[RUNNING] `rustc --crate-name d1 [..]--crate-type bin[..]
[CHECKING] foo v0.0.1 [..]
[RUNNING] `rustc --crate-name foo [..]--cfg[..]d1[..]
[FINISHED] dev [..]
",
)
.run();
}

#[cargo_test]
fn with_assumed_host_target_and_optional_build_dep() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2021"
[build-dependencies]
d1 = { path = "d1", artifact = "bin", optional = true, target = "target" }
"#,
)
.file("src/main.rs", "fn main() {}")
.file(
"build.rs",
r#"
fn main() {
std::env::var("CARGO_BIN_FILE_D1").unwrap();
}
"#,
)
.file(
"d1/Cargo.toml",
r#"
[package]
name = "d1"
version = "0.0.1"
edition = "2021"
"#,
)
.file("d1/src/main.rs", "fn main() {}")
.build();

p.cargo("check -Z bindeps -F d1 -v")
.masquerade_as_nightly_cargo(&["bindeps"])
.with_stderr_unordered(
"\
[COMPILING] foo v0.0.1 ([CWD])
[COMPILING] d1 v0.0.1 ([CWD]/d1)
[RUNNING] `rustc --crate-name build_script_build [..]--crate-type bin[..]
[RUNNING] `rustc --crate-name d1 [..]--crate-type bin[..]
[RUNNING] `[CWD]/target/debug/build/foo-[..]/build-script-build`
[RUNNING] `rustc --crate-name foo [..]--cfg[..]d1[..]
[FINISHED] dev [..]
",
)
.run();
}