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

How to indicate that a feature is only valid in combination with specific others #264

Open
ilyvion opened this issue Dec 31, 2024 · 6 comments

Comments

@ilyvion
Copy link

ilyvion commented Dec 31, 2024

Suppose we have three features: feature_a, feature_b, and feature_only_if_a_or_b. The third feature, feature_only_if_a_or_b, is meaningful only when at least one of feature_a or feature_b is enabled. Otherwise, it does nothing and shouldn't be tested.

In terms of combinations, I want to test the following setups:

  • feature_a
  • feature_b
  • feature_a + feature_only_if_a_or_b
  • feature_b + feature_only_if_a_or_b
  • feature_a + feature_b + feature_only_if_a_or_b

I understand --group-features can enforce specific groupings, like:

--group-features feature_a,feature_only_if_a_or_b --group-features feature_b,feature_only_if_a_or_b

However, this approach excludes feature_a and feature_b from running without feature_only_if_a_or_b. It doesn't allow single features to run independently.

Likewise, no combination of --exclude-features or --include-features achieves the desired behavior. For example, --exclude-features feature_only_if_a_or_b prevents it from running solo but also removes it from any valid combination.

When a feature is a strict sub-feature of another, there's an easy fix, you just make the sub-feature enable the parent feature. This is, however, not possible when a feature is orthogonally integrated with others, like in this case.

@taiki-e
Copy link
Owner

taiki-e commented Dec 31, 2024

Perhaps it would work if split into the following two commands.

--features feature_only_if_a_or_b --at-least-one-of feature_a,feature_b
--exclude-features feature_only_if_a_or_b

@ilyvion
Copy link
Author

ilyvion commented Dec 31, 2024

You'll get

error: feature `feature_only_if_a_or_b` specified by both --exclude-features and --features

@taiki-e
Copy link
Owner

taiki-e commented Dec 31, 2024

You have to run two commands, not in the single command.

cargo hack .. --features feature_only_if_a_or_b --at-least-one-of feature_a,feature_b
cargo hack .. --exclude-features feature_only_if_a_or_b

@ilyvion
Copy link
Author

ilyvion commented Dec 31, 2024

I'm wondering if I'm seeing a bug here...

The docs for --at-least-one-of say

Space or comma separated list of features. Skips sets of features that don't enable any of the features listed.

But when I run this command, I get these executions:

❯ cargo hack --feature-powerset clippy --all-targets --features feature_only_if_a_or_b --at-least-one-of feature_a,feature_b -- -D warnings
info: running `cargo clippy --all-targets --no-default-features --features feature_only_if_a_or_b,feature_b,feature_c -- -D warnings` on crate_name (1/6)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.52s

info: running `cargo clippy --all-targets --no-default-features --features feature_only_if_a_or_b,feature_b -- -D warnings` on crate_name (2/6)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.45s

info: running `cargo clippy --all-targets --no-default-features --features feature_only_if_a_or_b,default -- -D warnings` on crate_name (3/6)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.20s

info: running `cargo clippy --all-targets --no-default-features --features feature_only_if_a_or_b,feature_a -- -D warnings` on crate_name (4/6)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.44s

info: running `cargo clippy --all-targets --no-default-features --features feature_only_if_a_or_b,feature_b,feature_a -- -D warnings` on crate_name (5/6)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.44s

info: running `cargo clippy --all-targets --no-default-features --features feature_only_if_a_or_b,feature_c -- -D warnings` on crate_name (6/6)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.48s

Two of which (3 and 6) have neither of the --at-least-one-of features. Is that expected?

Basically the only one the two separate commands actually removes from the original powerset is the one with feature_only_if_a_or_b only by itself. So I guess I'm down to one fewer invocations now. 😅

@taiki-e
Copy link
Owner

taiki-e commented Dec 31, 2024

Two of which (3 and 6) have neither of the --at-least-one-of features. Is that expected?

There is no information about default and feature_c in the issue description or your comments, so without information about those features I cannot determine if they are bugs or not. If default enables feature_a & feature_b & feature_c, and feature_c enables feature_a, the log in your comment is correct1.

Footnotes

  1. since feature_c & feature_c,feature_a, and default & default,feature_a & default,feature_a,feature_b & default,feature_a,feature_c & default,feature_a,feature_b,feature_c are equivalent so omitted

@ilyvion
Copy link
Author

ilyvion commented Dec 31, 2024

Ah, you're right. I failed to keep in mind that although feature_c is unrelated to feature_only_if_a_or_b, it does in fact enable feature_a, so yeah, I guess it is, in fact, expected. Thanks, this makes sense now. Thanks for the help. 😊

If there's nothing actionable to do for you regarding "dependent features" as far as implementing anything goes, feel free to close this issue.

The only thing I could think of would be to have a way to specify something like --dependent-feature feature_only_if_a_or_b,feature_a or whatever, and then cargo-hack could pull off

cargo hack .. --features feature_only_if_a_or_b --at-least-one-of feature_a,feature_b
cargo hack .. --exclude-features feature_only_if_a_or_b

all in a single invocation, but given how easy it is to just run the two invocations manually, I'm not sure it's even worth considering.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants