Why `#[cfg(not(test))]` does not work with `compile_error!{...}`?

This is the minimum example:

#[cfg(not(test))]
compile_error!{"some error"}

I expect that running cargo test won't have that compile error. However, this condition compilation does not work. I suppose the condition #[cfg(not(test))] was not supported, however

#[cfg(not(test))]
pub fn add(){}

when running cargo doc --open, the documentation does have this item while #[cfg(test)] makes the item hidden in the documentation, this result witnesses this condition compilation should work. Why does it not work for the compile_error!{"some error"} item?

When you are testing a library, the library is still also compiled without cfg(test), for use in its integration test targets (tests/*.rs) and dependent packages. (cfg(test) is only active when producing a test binary that runs #[test] functions, not in any other case. That test binary is not a library.)

I’d hope that’s skipped if you have none of those, but perhaps you do, or perhaps Cargo isn’t that clever.

The cargo book also lists the targets that cargo test builds by default (section Target Selection). These include:

  • lib — used to link with binaries, examples, integration tests, and doc tests
  • lib as a unit test
  • ...

In particular, a library is built twice and only the "lib as a unit test" enables the test compilation flag, as @kpreid alluded to.