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

Run with --all-features first, run with biggest feature combination early (first or second) if --all-features case is skipped #247

Merged
merged 3 commits into from
Apr 2, 2024
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
24 changes: 1 addition & 23 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,8 @@ impl Args {

// https://github.com/taiki-e/cargo-hack/issues/42
// https://github.com/rust-lang/cargo/pull/8799
let namespaced_features = has_z_flag(&cargo_args, "namespaced-features");
exclude_no_default_features |= !include_features.is_empty();
exclude_all_features |= !include_features.is_empty()
|| !exclude_features.is_empty()
|| (feature_powerset && !namespaced_features && depth.is_none());
exclude_all_features |= !include_features.is_empty() || !exclude_features.is_empty();
exclude_features.extend_from_slice(&features);

term::verbose::set(verbose != 0);
Expand Down Expand Up @@ -668,25 +665,6 @@ fn parse_grouped_features(
Ok(group_features)
}

fn has_z_flag(args: &[String], name: &str) -> bool {
let mut iter = args.iter().map(String::as_str);
while let Some(mut arg) = iter.next() {
if arg == "-Z" {
arg = iter.next().unwrap();
} else if let Some(a) = arg.strip_prefix("-Z") {
arg = a;
} else {
continue;
}
if let Some(rest) = arg.strip_prefix(name) {
if rest.is_empty() || rest.starts_with('=') {
return true;
}
}
}
false
}

// (short flag, long flag, value name, short descriptions, additional descriptions)
type HelpText<'a> = (&'a str, &'a str, &'a str, &'a str, &'a [&'a str]);

Expand Down
66 changes: 52 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,13 @@ fn determine_kind<'a>(
let kind = Kind::Normal;
Some(PackageRuns { id, kind, feature_count })
} else {
// See exec_on_package
let feature_count = features.len()
+ usize::from(!cx.exclude_no_default_features)
+ usize::from(
!cx.exclude_all_features
&& pkg_features.normal().len() + pkg_features.optional_deps().len() > 1,
!(cx.exclude_all_features
|| pkg_features.optional_deps().is_empty()
&& pkg_features.normal().len() <= 1),
);
let kind = Kind::Each { features };
Some(PackageRuns { id, kind, feature_count })
Expand All @@ -290,11 +292,17 @@ fn determine_kind<'a>(
let kind = Kind::Normal;
Some(PackageRuns { id, kind, feature_count })
} else {
// See exec_on_package
let feature_count = features.len()
+ usize::from(!cx.exclude_no_default_features)
+ usize::from(
!cx.exclude_all_features
&& pkg_features.normal().len() + pkg_features.optional_deps().len() > 1,
!(cx.exclude_all_features
|| (pkg_features.optional_deps().is_empty()
|| match &cx.optional_deps {
// Skip when all optional deps are already included in powerset
Some(opt_deps) => opt_deps.is_empty(),
None => false,
})),
);
let kind = Kind::Powerset { features };
Some(PackageRuns { id, kind, feature_count })
Expand Down Expand Up @@ -477,6 +485,34 @@ fn exec_on_package(
Kind::Each { .. } | Kind::Powerset { .. } => {}
}

// Run with --all-features first: https://github.com/taiki-e/cargo-hack/issues/246
// https://github.com/taiki-e/cargo-hack/issues/42
// https://github.com/rust-lang/cargo/pull/8799
// > --all-features will now enable features for inactive optional dependencies.
let pkg_features = cx.pkg_features(id);
let exclude_all_features = cx.exclude_all_features
|| match kind {
Kind::Each { .. } => {
pkg_features.optional_deps().is_empty() && pkg_features.normal().len() <= 1
}
Kind::Powerset { .. } => {
pkg_features.optional_deps().is_empty()
|| match &cx.optional_deps {
// Skip when all optional deps are already included in powerset
Some(opt_deps) => opt_deps.is_empty() && cx.depth.is_none(),
None => false,
}
}
Kind::Normal => unreachable!(),
};
if !exclude_all_features {
let mut line = line.clone();
// run with all features
// https://github.com/taiki-e/cargo-hack/issues/42
line.arg("--all-features");
exec_cargo(cx, id, &mut line, progress, keep_going)?;
}

if !cx.no_default_features {
line.arg("--no-default-features");
}
Expand All @@ -498,23 +534,25 @@ fn exec_on_package(
}
}
Kind::Powerset { features } => {
let features =
if exclude_all_features && features.len() > 1 && cx.depth.unwrap_or(usize::MAX) > 1
{
// If --all-features case is skipped, run with the biggest feature combination early (first or second): https://github.com/taiki-e/cargo-hack/issues/246
// TODO: The last combination is usually the biggest feature combination,
// but in some cases this is not the case due to deduplication of the powerset.
let last = features.last().unwrap();
exec_cargo_with_features(cx, id, &line, progress, keep_going, last)?;
&features[..features.len() - 1]
} else {
features
};
for f in features {
exec_cargo_with_features(cx, id, &line, progress, keep_going, f)?;
}
}
Kind::Normal => unreachable!(),
}

let pkg_features = cx.pkg_features(id);
if !cx.exclude_all_features
&& pkg_features.normal().len() + pkg_features.optional_deps().len() > 1
{
// run with all features
// https://github.com/taiki-e/cargo-hack/issues/42
line.arg("--all-features");
exec_cargo(cx, id, &mut line, progress, keep_going)?;
}

Ok(())
}

Expand Down
Loading