Skip to content

Commit b546338

Browse files
committed
fix(bindeps): do not propagate artifact dependency to proc macro or build deps
As reproduced in the 2 fixed test cases of this commit, cargo panicked when - a package `foo` has an artifact dependency `artifact` with a specified target TARGET_ARTIFACT different from host TARGET_HOST, - `artifact` depends on proc macro `macro`, - `macro` conditionally depends on `arch` on TARGET_HOST, with the follwing message ``` did not find features for (PackageId { name: "arch", version: "0.0.1", source: "/Users/lencerf/Developer/cargo/target/tmp/cit/t0/foo/arch" }, ArtifactDep(CompileTarget { name: "x86_64-apple-darwin" })) within activated_features: [ ( PackageId { name: "foo", version: "0.0.1", source: "/Users/lencerf/Developer/cargo/target/tmp/cit/t0/foo", }, NormalOrDev, ), ( PackageId { name: "macro", version: "0.0.1", source: "/Users/lencerf/Developer/cargo/target/tmp/cit/t0/foo/macro", }, ArtifactDep( CompileTarget { name: "x86_64-apple-darwin", }, ), ), ( PackageId { name: "artifact", version: "0.0.1", source: "/Users/lencerf/Developer/cargo/target/tmp/cit/t0/foo/artifact", }, ArtifactDep( CompileTarget { name: "x86_64-apple-darwin", }, ), ), ] ``` From the above message, it is clear that proc macro `macro` was wrongly associated with `FeaturesFor::ArtifactDep` instead of `FeaturesFor::HostDep`. Package `arch` was later ignored because cargo thought `macro` should be built for TARGET_ARTIFACT (x86_64-apple-darwin), while `arch` was conditionally needed on TARGET_HOST (aarch64-apple-darwin). Similar analyses apply to the other test case. This commit fixes 2 paths: - when resolving features, if we encounter build dependencies or proc macros, always associate them with `FeaturesFor::HostDep`. - when deriving UnitFor for dependencies, stop propagating artifact_target_for_features if the the dependency is a build dependency or a proc macro. Signed-off-by: Changyuan Lyu <changyuan.lv@gmail.com>
1 parent 7225a8e commit b546338

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/cargo/core/profiles.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,12 +1177,18 @@ impl UnitFor {
11771177
} else {
11781178
self.panic_setting
11791179
};
1180+
let artifact_target_for_features =
1181+
if dep_target.proc_macro() || parent.target.is_custom_build() {
1182+
None
1183+
} else {
1184+
self.artifact_target_for_features
1185+
};
11801186
UnitFor {
11811187
host: self.host || dep_for_host,
11821188
host_features,
11831189
panic_setting,
11841190
root_compile_kind,
1185-
artifact_target_for_features: self.artifact_target_for_features,
1191+
artifact_target_for_features,
11861192
}
11871193
}
11881194

src/cargo/core/resolver/features.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -906,11 +906,11 @@ impl<'a, 'gctx> FeatureResolver<'a, 'gctx> {
906906
// All this may result in a dependency being built multiple times
907907
// for various targets which are either specified in the manifest
908908
// or on the cargo command-line.
909-
let lib_fk = if fk == FeaturesFor::default() {
910-
(self.track_for_host
911-
&& (dep.is_build() || self.has_proc_macro_lib(dep_id)))
912-
.then(|| FeaturesFor::HostDep)
913-
.unwrap_or_default()
909+
let lib_fk = if fk != FeaturesFor::HostDep
910+
&& self.track_for_host
911+
&& (dep.is_build() || self.has_proc_macro_lib(dep_id))
912+
{
913+
FeaturesFor::HostDep
914914
} else {
915915
fk
916916
};

tests/testsuite/artifact_dep.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3395,7 +3395,6 @@ staticlib present: true
33953395
);
33963396
}
33973397

3398-
#[should_panic]
33993398
#[cargo_test]
34003399
fn artifact_dep_target_does_not_propagate_to_deps_of_build_script() {
34013400
if cross_compile_disabled() {
@@ -3490,7 +3489,6 @@ fn artifact_dep_target_does_not_propagate_to_deps_of_build_script() {
34903489
.run();
34913490
}
34923491

3493-
#[should_panic]
34943492
#[cargo_test]
34953493
fn artifact_dep_target_does_not_propagate_to_proc_macro() {
34963494
if cross_compile_disabled() {

0 commit comments

Comments
 (0)