Skip to content
Merged
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
9 changes: 8 additions & 1 deletion src/cargo/core/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,12 +1180,19 @@ impl UnitFor {
} else {
self.panic_setting
};
let artifact_target_for_features =
// build.rs and proc-macros are always for host.
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
196 changes: 196 additions & 0 deletions tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3394,3 +3394,199 @@ 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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding with_stderr_data(str![[""]]) to ensure that we really build arch? Ditto for proc macro test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'll need to set env SNAPSHOTS=overwrite to regenerate test snapshot locally and commit it btw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @Lencerf!

https://github.com/rust-lang/cargo/actions/runs/19524318318/job/55893849063?pr=15788#step:15:4772

I know it is annoying, but we also need to append [EXE] for binary paths for windows compatibility

- [RUNNING] unittests src/main.rs (target/debug/deps/foo-[HASH])
+ [RUNNING] unittests src/main.rs (target/debug/deps/foo-[HASH][EXE])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! CI now passes!

.with_stderr_data(str![[r#"
[LOCKING] 3 packages to latest compatible versions
[COMPILING] arch v0.0.1 ([ROOT]/foo/arch)
[COMPILING] builder v0.0.1 ([ROOT]/foo/builder)
[COMPILING] artifact v0.0.1 ([ROOT]/foo/artifact)
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] unittests src/main.rs (target/debug/deps/foo-[HASH][EXE])

"#]])
.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")
.with_stderr_data(str![[r#"
[LOCKING] 3 packages to latest compatible versions
[COMPILING] arch v0.0.1 ([ROOT]/foo/arch)
[COMPILING] macro v0.0.1 ([ROOT]/foo/macro)
[COMPILING] artifact v0.0.1 ([ROOT]/foo/artifact)
[COMPILING] foo v0.0.1 ([ROOT]/foo)
[FINISHED] `test` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
[RUNNING] unittests src/main.rs (target/debug/deps/foo-[HASH][EXE])

"#]])
.masquerade_as_nightly_cargo(&["bindeps"])
.run();
}