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

Add default features for each [[bin]] #10409

Closed
Icelk opened this issue Feb 22, 2022 · 7 comments
Closed

Add default features for each [[bin]] #10409

Icelk opened this issue Feb 22, 2022 · 7 comments
Labels
A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-features Area: features — conditional compilation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`

Comments

@Icelk
Copy link

Icelk commented Feb 22, 2022

Problem

When adding [[bin]] entries to a Cargo.toml, you can specify required dependencies, but not additional "recommended", default features.

Proposed Solution

Add a key, namely default-features to the [[bin]] sections in Cargo.toml.
Both the dependencies.default and this new key would be applied. If --no-default-features is passed, both declarations are ignored.

Notes

No response

@Icelk Icelk added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Feb 22, 2022
@weihanglo
Copy link
Member

Sorry, I don't get it. Where can I specify required dependencies for a [[bin]] target? Cannot find any relevant doc in 1.58.1 Cargo Guide. Only found an RFC still in discussion.

Presumably you want to build the binary without specifying features in CLI, there is a similar feature request you might be interested in, which proposes to enable required features automatically: #4663

@Icelk
Copy link
Author

Icelk commented Feb 23, 2022

That issue is about enabling required dependencies. I’m talking about “recommended”, default dependencies, as those in dependencies.default. You can currently supply required dependencies per bin in <bin name>.required.features.
See the guide on the Cargo.toml format.

@weihanglo
Copy link
Member

Sorry @Icelk, I've searched the key dependencies.default and still couldn't find such key in either 1.58.1 Cargo Guide or the link you provided. I might miss something important. Can you provide a screenshot or else to point out where it is?

Also, I am a bit confused. Is this issue concerning features or dependencies? They are not interchangeable terms in Cargo.toml. AFAIK, a target like [[bin]] only has a required-features field at this moment.


Here is my bold guess about what you mean by "recommended". Correct me if I am wrong.

# ...
[[bin]]
name = "foo"
default-features = ["serde", "log"]

In Cargo.toml, there is a bin target foo with the proposed default-features field, so when one tries to cargo build --bin foo, the serde and log features are on by default unless --no-default-features is also passed.

Is this what in your mind?

@Icelk
Copy link
Author

Icelk commented Feb 23, 2022

Precisely!
I’m sorry, I of course mean features.default.

As you mentioned, one could specify both dependencies and other features in the <bin>.default-features field, just as is possible in features.default.

@weihanglo weihanglo added A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-features Area: features — conditional compilation labels Feb 25, 2022
@weihanglo
Copy link
Member

Discussion in #1982 cover some of scenarios here. rust-lang/rfcs#3374 is an attempt to resolve this as well.

@weihanglo
Copy link
Member

weihanglo commented May 23, 2023

Triage: after a re-read. This is kinda a duplicate of #4663, and is covered by #1982 and rust-lang/rfcs#3374, especially the latter that automatically enabling features for target requested. Let's continue discussing there. Thank you for your report :)

@weihanglo weihanglo closed this as not planned Won't fix, can't repro, duplicate, stale May 23, 2023
@Icelk
Copy link
Author

Icelk commented May 23, 2023

Thanks for the links!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-cargo-targets Area: selection and definition of targets (lib, bins, examples, tests, benches) A-features Area: features — conditional compilation C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Projects
None yet
Development

No branches or pull requests

2 participants