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 work when target field specified coexists with optional = true #11183

Merged
merged 4 commits into from
Oct 28, 2022
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
20 changes: 8 additions & 12 deletions src/cargo/core/compiler/unit_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,7 @@ impl<'a, 'cfg> State<'a, 'cfg> {
}
}

/// See [`ResolvedFeatures::activated_features`].
fn activated_features(
&self,
pkg_id: PackageId,
Expand All @@ -1049,14 +1050,9 @@ impl<'a, 'cfg> State<'a, 'cfg> {
features.activated_features(pkg_id, features_for)
}

fn is_dep_activated(
&self,
pkg_id: PackageId,
features_for: FeaturesFor,
dep_name: InternedString,
) -> bool {
self.features()
.is_dep_activated(pkg_id, features_for, dep_name)
/// See [`ResolvedFeatures::is_activated`].
fn is_activated(&self, pkg_id: PackageId, features_for: FeaturesFor) -> bool {
self.features().is_activated(pkg_id, features_for)
}

fn get(&self, id: PackageId) -> &'a Package {
Expand All @@ -1071,7 +1067,7 @@ impl<'a, 'cfg> State<'a, 'cfg> {
let kind = unit.kind;
self.resolve()
.deps(pkg_id)
.filter_map(|(id, deps)| {
.filter_map(|(dep_id, deps)| {
assert!(!deps.is_empty());
let deps: Vec<_> = deps
.iter()
Expand Down Expand Up @@ -1103,8 +1099,8 @@ 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());
if !self.is_dep_activated(pkg_id, features_for, dep.name_in_toml()) {
let dep_features_for = unit_for.map_to_features_for(dep.artifact());
if !self.is_activated(dep_id, dep_features_for) {
return false;
}
}
Expand All @@ -1117,7 +1113,7 @@ impl<'a, 'cfg> State<'a, 'cfg> {
if deps.is_empty() {
None
} else {
Some((id, deps))
Some((dep_id, deps))
}
})
.collect()
Expand Down
53 changes: 28 additions & 25 deletions src/cargo/core/resolver/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ type ActivateMap = HashMap<PackageFeaturesKey, BTreeSet<InternedString>>;

/// Set of all activated features for all packages in the resolve graph.
pub struct ResolvedFeatures {
activated_features: ActivateMap,
/// Optional dependencies that should be built.
/// Map of features activated for each package.
///
/// The value is the `name_in_toml` of the dependencies.
activated_dependencies: ActivateMap,
/// The presence of each key also means the package itself is activated,
/// even its associated set contains no features.
activated_features: ActivateMap,
/// Options that change how the feature resolver operates.
opts: FeatureOpts,
}

Expand Down Expand Up @@ -321,21 +322,14 @@ impl ResolvedFeatures {
.expect("activated_features for invalid package")
Copy link
Member Author

Choose a reason for hiding this comment

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

I feel like we are not on the same page, @ehuss 😆

Maybe I miss some contexts, but it seems that we can switch to unwrap_or_default nowadays even if we stop inserting empty set from now on.

I'm not sure I follow. With this change, it must insert an empty set, so skipping that step is no longer an option?

activated_features_int returns a Result so unwrap_or_default can easily recover from it with a default. There is no need of the presence of an empty set, no?

fn activated_features_int(
&self,
pkg_id: PackageId,
features_for: FeaturesFor,
) -> CargoResult<Vec<InternedString>> {
let fk = features_for.apply_opts(&self.opts);
if let Some(fs) = self.activated_features.get(&(pkg_id, fk)) {
Ok(fs.iter().cloned().collect())
} else {
bail!("features did not find {:?} {:?}", pkg_id, fk)
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I was conflating "switch to unwrap_or_default" and "remove inserting the empty set", as I was thinking of them as a single action that would be done together. I suppose it could switch to unwrap_or_default, but I like the validation that expect has been giving.

}

/// Returns if the given dependency should be included.
/// Asks the resolved features if the given package should be included.
///
/// This handles dependencies disabled via `cfg` expressions and optional
/// dependencies which are not enabled.
pub fn is_dep_activated(
&self,
pkg_id: PackageId,
features_for: FeaturesFor,
dep_name: InternedString,
) -> bool {
let key = features_for.apply_opts(&self.opts);
self.activated_dependencies
.get(&(pkg_id, key))
.map(|deps| deps.contains(&dep_name))
.unwrap_or(false)
/// One scenario to use this function is to deteremine a optional dependency
/// should be built or not.
pub fn is_activated(&self, pkg_id: PackageId, features_for: FeaturesFor) -> bool {
log::trace!("is_activated {} {features_for}", pkg_id.name());
self.activated_features_unverified(pkg_id, features_for.apply_opts(&self.opts))
.is_some()
}

/// Variant of `activated_features` that returns `None` if this is
Expand Down Expand Up @@ -415,8 +409,14 @@ pub struct FeatureResolver<'a, 'cfg> {
/// Options that change how the feature resolver operates.
opts: FeatureOpts,
/// Map of features activated for each package.
///
/// The presence of each key also means the package itself is activated,
/// even its associated set contains no features.
activated_features: ActivateMap,
/// Map of optional dependencies activated for each package.
///
/// The key is the package having their dependencies activated.
/// The value comes from `dep_name` part of the feature syntax `dep:dep_name`.
activated_dependencies: ActivateMap,
/// Keeps track of which packages have had its dependencies processed.
/// Used to avoid cycles, and to speed up processing.
Expand Down Expand Up @@ -475,7 +475,6 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
}
Ok(ResolvedFeatures {
activated_features: r.activated_features,
activated_dependencies: r.activated_dependencies,
opts: r.opts,
})
}
Expand Down Expand Up @@ -507,20 +506,24 @@ impl<'a, 'cfg> FeatureResolver<'a, 'cfg> {
Ok(())
}

/// Activates [`FeatureValue`]s on the given package.
/// Activates a list of [`FeatureValue`] for a given package.
///
/// This is the main entrance into the recursion of feature activation
/// for a package.
/// This is the main entrance into the recursion of feature activation for a package.
/// Other `activate_*` functions would be called inside this function accordingly.
fn activate_pkg(
&mut self,
pkg_id: PackageId,
fk: FeaturesFor,
fvs: &[FeatureValue],
) -> CargoResult<()> {
log::trace!("activate_pkg {} {}", pkg_id.name(), fk);
// Add an empty entry to ensure everything is covered. This is intended for
// finding bugs where the resolver missed something it should have visited.
// Remove this in the future if `activated_features` uses an empty default.
// Cargo must insert an empty set here as the presence of an (empty) set
// also means that the dependency is activated.
// This `is_activated` behavior for dependencies was previously depends on field
// `activated_dependencies`, which is less useful after rust-lang/cargo#11183.
//
// That is, we may keep or remove `activated_dependencies` in the future
// if figuring out it can completely be replaced with `activated_features`.
self.activated_features
.entry((pkg_id, fk.apply_opts(&self.opts)))
.or_insert_with(BTreeSet::new);
Expand Down
6 changes: 1 addition & 5 deletions src/cargo/ops/tree/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,7 @@ fn add_pkg(
if dep.is_optional() {
// If the new feature resolver does not enable this
// optional dep, then don't use it.
if !resolved_features.is_dep_activated(
package_id,
features_for,
dep.name_in_toml(),
) {
if !resolved_features.is_activated(dep_id, features_for) {
return false;
}
}
Expand Down
58 changes: 57 additions & 1 deletion tests/testsuite/artifact_dep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn check_with_invalid_artifact_dependency() {
version = "0.0.0"
authors = []
resolver = "2"

[dependencies]
bar = { path = "bar/", artifact = "unknown" }
"#,
Expand Down Expand Up @@ -2263,3 +2263,59 @@ fn build_script_features_for_shared_dependency() {
.masquerade_as_nightly_cargo(&["bindeps"])
.run();
}

#[cargo_test]
fn build_with_target_and_optional() {
// This is a incorrect behaviour got to be fixed.
// 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("build -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[..]
[COMPILING] foo v0.0.1 [..]
[RUNNING] `rustc --crate-name foo [..]--cfg[..]d1[..]
[FINISHED] dev [..]
",
)
.run();
}