rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.75k stars 2.42k forks source link

`#[cfg(not(test))]` does not work #14768

Open xmh0511 opened 5 days ago

xmh0511 commented 5 days ago

Problem

In the lib.rs file

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

This does not work when running cargo test. It still emits the compile error in this case.

Steps

No response

Possible Solution(s)

No response

Notes

No response

Version

No response

ehuss commented 5 days ago

Thanks for the report! This is intended behavior when running cargo test. What happens is that the library is built three times. Once as a unittest, and once as a library, and once as a doctest. Only the unittest copy gets test set. The others do not, as they are intended to be built as-if they are part of a normal build.