Require or force self-crate feature in dev dependencies

I have an optional dependencies, but I want to force it in dev-dependencies. However, it seems that doing this doesn't enable the feature. Is this a bug, or am I missing something?

My Cargo.toml (where cfg-if can be any dependency, but cfg-if is small and standalone so I chose that for simplicity):

[dependencies]
cfg-if = { version = "0.1", optional = true }

[dev-dependencies]
cfg-if = { version = "0.1" }

My lib.rs:

#[cfg(feature = "cfg-if")]
pub fn foo() {
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_foo() {
        foo();
    }
}

Running with cargo test:

error[E0425]: cannot find function `foo` in this scope
  --> src/lib.rs:11:9
   |
11 |         foo();
   |         ^^^ not found in this scope

Running with cargo test --features cfg-if:

running 1 test
test tests::test_foo ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

   Doc-tests bar

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

The main thing here is that I believe it should not require me to write --features cfg-if because the dependency is mandatory in the [dev-dependencies], used by cargo test.


Something else that would help me work around this is, is there any way for me to specify a "self-crate feature" in the [dev-dependencies], so that a feature is always forcibly enabled when running in dev mode?

Mandatory dependencies never set feature flags, so I'm not really surprised.

You could use test in your condition:

#[cfg(any(feature = "cfg-if", test))]
pub fn foo() {
}
1 Like

That's clever, any didn't even come to my head. Thanks!

So there's no way to force a feature to be enabled en development mode?

I don't know any way to force a feature in development mode.

I should also note, using cfg(test) will only help you for inline tests, since that basically recompiles your whole library in test mode. Stuff in tests/, examples/, benches/, and even doc-tests will use the library as it was normally compiled.

For the stuff in tests/, examples/, and benches/, you can add a [[test]] or [[example]] or [[bench]] section to Cargo.toml, and use the required-features field to tell Cargo that it needs a specific feature enabled.

For example, if you have tests/foo.rs then you can do:

[[test]]
name = "foo"
required-features = ["cfg-if"]

However, this doesn't quite do what you want. Rather then auto-enabling the feature when building this test, it will instead disable the test unless this feature is manually enabled.

1 Like

Oh, that's a good point, I would have to move the tests to be inline (which isn't a problem for now).

Thanks for the link, I hadn't seen that as I had only checked pages related to just testing. It's still better than just failing to compile, at least for my case I believe. So that's different in that it disables the test instead of not compiling it as opposed to putting a #[cfg(feature = "cfg-if")] on top of the #[test] fn which would prevent it from compiling? Not clear what it means by "skipped".

As a side note I'm sure it's been said a lot but the responses here to my only two topics so far have been so welcoming. Thank you all for spending the time into helping people here in the forums! The levels of patience and tolerance here are wonderful. I really look up to all the people just being awesome in general.

3 Likes

When the required-features are not enabled, Cargo will not compile the given test/bench/example file.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.