Skip to content

fix(bindeps): do not propagate artifact dependency to proc macro or build deps #15788

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 7 additions & 1 deletion src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1177,12 +1177,18 @@ impl UnitFor {
} else {
self.panic_setting
};
let artifact_target_for_features =
if dep_target.proc_macro() || parent.target.is_custom_build() {
None
} else {
self.artifact_target_for_features
};
UnitFor {
host: self.host || dep_for_host,
host_features,
panic_setting,
root_compile_kind,
artifact_target_for_features: self.artifact_target_for_features,
artifact_target_for_features,
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,11 +906,11 @@ impl<'a, 'gctx> FeatureResolver<'a, 'gctx> {
// All this may result in a dependency being built multiple times
// for various targets which are either specified in the manifest
// or on the cargo command-line.
let lib_fk = if fk == FeaturesFor::default() {
(self.track_for_host
&& (dep.is_build() || self.has_proc_macro_lib(dep_id)))
.then(|| FeaturesFor::HostDep)
.unwrap_or_default()
let lib_fk = if fk != FeaturesFor::HostDep
&& self.track_for_host
&& (dep.is_build() || self.has_proc_macro_lib(dep_id))
{
FeaturesFor::HostDep
} else {
fk
};
Expand Down
176 changes: 176 additions & 0 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3394,3 +3394,179 @@ staticlib present: true
"#]],
);
}

#[cargo_test]
fn artifact_dep_target_does_not_propagate_to_deps_of_build_script() {
if cross_compile_disabled() {
return;
}
let bindeps_target = cross_compile::alternate();
let native_target = cross_compile::native();

let p = project()
.file(
"Cargo.toml",
&r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
resolver = "2"

[dependencies.artifact]
path = "artifact"
artifact = "bin"
target = "$TARGET"
"#
.replace("$TARGET", bindeps_target),
)
.file(
"src/main.rs",
r#"
fn main() {
let _b = include_bytes!(env!("CARGO_BIN_FILE_ARTIFACT"));
}
"#,
)
.file(
"artifact/Cargo.toml",
r#"
[package]
name = "artifact"
version = "0.0.1"
edition = "2015"

[build-dependencies]
builder = { path = "../builder" }
"#,
)
.file("artifact/src/main.rs", "fn main() { }")
.file(
"artifact/build.rs",
r#"
extern crate builder;
fn main() {
let _ = builder::add(1, 2);
}
"#,
)
.file(
"builder/Cargo.toml",
&r#"
[package]
name = "builder"
version = "0.0.1"
edition = "2015"

[target.'$TARGET'.dependencies]
arch = { path = "../arch" }
"#
.replace("$TARGET", native_target),
)
.file(
"builder/src/lib.rs",
r#"
extern crate arch;
pub fn add(a: i32, b: i32) -> i32 { arch::add(a, b) }
"#,
)
.file(
"arch/Cargo.toml",
r#"
[package]
name = "arch"
version = "0.0.1"
edition = "2015"
"#,
)
.file(
"arch/src/lib.rs",
r#"pub fn add(a: i32, b: i32) -> i32 { a + b }"#,
)
.build();
p.cargo("test -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.run();
}

#[cargo_test]
fn artifact_dep_target_does_not_propagate_to_proc_macro() {
if cross_compile_disabled() {
return;
}
let bindeps_target = cross_compile::alternate();
let native_target = cross_compile::native();

let p = project()
.file(
"Cargo.toml",
&r#"
[package]
name = "foo"
version = "0.0.1"
edition = "2015"
resolver = "2"

[dependencies.artifact]
path = "artifact"
artifact = "bin"
target = "$TARGET"
"#
.replace("$TARGET", bindeps_target),
)
.file(
"src/main.rs",
r#"
fn main() {
let _b = include_bytes!(env!("CARGO_BIN_FILE_ARTIFACT"));
}
"#,
)
.file(
"artifact/Cargo.toml",
r#"
[package]
name = "artifact"
version = "0.0.1"
edition = "2015"

[dependencies]
macro = { path = "../macro" }
"#,
)
.file("artifact/src/main.rs", "fn main() { }")
.file(
"macro/Cargo.toml",
&r#"
[package]
name = "macro"
version = "0.0.1"
edition = "2015"

[lib]
proc-macro = true

[target.'$TARGET'.dependencies]
arch = { path = "../arch" }
"#
.replace("$TARGET", native_target),
)
.file("macro/src/lib.rs", "")
.file(
"arch/Cargo.toml",
r#"
[package]
name = "arch"
version = "0.0.1"
edition = "2015"
"#,
)
.file(
"arch/src/lib.rs",
"pub fn add(a: i32, b: i32) -> i32 { a + b }",
)
.build();
p.cargo("test -Z bindeps")
.masquerade_as_nightly_cargo(&["bindeps"])
.run();
}