How do I 'pass down' feature flags to subdependencies in Cargo?
Asked Answered
K

3

27

I'm writing a library in Cargo. If this library depends on another library like libc, which exposes a feature (in this case, use_std), how do I make a feature I expose enable or disable that feature in my dependency?

Looking at the cargo documentation, it looks like there's no official way specified to do this.

Keener answered 13/10, 2016 at 12:51 Comment(1)
This question has a better answer: How to pass compiler flags to a sub crate in Rust?Fiche
M
28

From the documentation you linked to:

# Features can be used to reexport features of other packages. The `session`
# feature of package `awesome` will ensure that the `session` feature of the
# package `cookie` is also enabled.
session = ["cookie/session"]

Is that sufficient?

Mho answered 13/10, 2016 at 12:59 Comment(3)
Hmm, what if I have a dependency like ascii, which exposes a no_std flag - which I want to disable if my use_std flag is enabled?Keener
@Keener an interesting question, you could probably ask a separate top-level question for that! It might even be a feature request. Specifically for no_std though, I feel like I heard that the right thing to do is to have a default feature that enables using things from std, so perhaps the ascii crate is just backwards...Mho
Yeah, I've heard that features are meant to be additive, not subtractive, so I think my best bet would be to make a pull request to them.Keener
B
0

Just to augment other answers, I found that when I was including my own projects from a different path, I had to also include default-features = false in my dependency when i was compiling with my flags. Like this:

[package]
name = "A"
version = "0.1.0"
edition.workspace = true

[features]
stage = ["B/stage"]

[dependencies]
eyre.workspace = true
tokio.workspace = true

[dependencies.B]
path = ".."
default-features = false  # Without this, I couldn't pass the `stage` feature down to my dependency
Braces answered 8/4, 2024 at 0:7 Comment(1)
This isn't right; any default features should not prevent you from enabling other features. The only way I could imagine this appearing as such is if the crate was not handling features in a purely additive manner (e.g. some default feature somehow "overrides" items exposed by the stage feature). But even if so, that is not how cargo features are intended to be used.Remediless
T
0

This worked for me:

# Cargo.toml
[features]
default = ["prettyprint"]
prettyprint = ["arrow-cast/prettyprint"]

The funny thing is that I did not have arrow-cast in my deps, so I needed to add it. Because my original dep (just arrow) was using the feature of a another crate (in this case arrow-cast). Just check the Cargo.toml of the crate that you are using to tie up the loose ends. default is convenient so that it is always enabled, you can remove it down the road if you don't need it on by default.

Thickwitted answered 6/6, 2024 at 22:13 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.