Search code examples
rustrust-cargo

Can I disable a dev-dependency feature for release mode?


Recently, I investigated my rust project build times using cargo build --release --timings and found out that a dev-dependency feature takes up a lot of build time despite being in release mode.

In particular, I use sea-orm with the following features, whereas the sqlite feature is only used for testing:

[dependencies.sea-orm]
version = "0.11"
features = ["runtime-tokio-rustls", "sqlx-mysql", "macros"]
default-features = false

[dev-dependencies]
sea-orm = { version = "0.11", features = ["sqlx-sqlite"]}

Naturally, cargo resolves all these features together in the cargo.lock. However, building the sqlite sys library takes up a big chunk of the build time, even in release mode.

While not critical, it would be nice to exclude this feature while building the release profile. Is there a way to achieve this?


Solution

  • This is how the original feature resolver for Cargo works; feature flags are unified across everything.

    Fortunately, there was a feature resolver version 2 introduced in Rust 2021 that splits these up a bit more like you'd expect (separate "buckets" for build-dependencies, dev-dependencies, and platform specific flags) but at the potential cost of compiling some crates multiple times (if they have different feature flags enabled in different "buckets").

    There's a few reasons why you might not be using the new feature resolver:

    • If your crate uses edition = "2018" or earlier, it will use the original resolver by default. You can either update to edition = "2021" or specify resolver = "2" in your Cargo.toml's [package].
    • If you are using a workspace with a root package, it only uses what the root Cargo.toml is configured for, so if that uses a prior edition use the steps above.
    • If you are using a workspace without a root package - members only (i.e. a virtual manifest) then it will use the original resolver by default. You will need to specify resolver = "2" in your [workspace].

    See also: