rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.61k stars 12.62k forks source link

#![cfg_attr(not(test), no_std)] doesn't work when building multiple crate-types #48665

Open glandium opened 6 years ago

glandium commented 6 years ago
$ cargo create --lib foo
$ cd foo
$ cat >> Cargo.toml <<EOF
[lib]
crate-type = ["rlib", "staticlib"]
[profile.dev]
panic="abort"
EOF
$ cat > src/lib.rs <<EOF
#![cfg_attr(not(test), no_std)]
#![feature(lang_items)]

#[cfg(not(test))]
#[lang = "panic_fmt"]
#[no_mangle]
pub fn panic_fmt(_: core::fmt::Arguments, _: &'static str, _: u32, _: u32) -> ! {
    loop {}
}
EOF
$ cargo +nightly build
   Compiling foo v0.1.0 (file:///tmp/foo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.13 secs
$ cargo +nightly test
   Compiling foo v0.1.0 (file:///tmp/foo)
error: language item required, but not found: `eh_personality`

error: aborting due to previous error

That is an error that is expected with no_std, but the code is supposed to be essentially empty in the test configuration.

It works when crate-type contains only one value (either one).

tcz717 commented 4 years ago

I have the same issue even after 2 years (2020)

harrier-lcc commented 3 years ago

just hit the same issue today. any follow-up on this?

XiangYyang commented 2 years ago

I have the same issue https://github.com/rust-lang/rust/issues/100766.

It doesn't seem to be well resolved.

However, I found some options in Cargo test :

I noted that I can use the --lib to special some libraries what I want to link. So I tried to special linking the test library:

cargo test --lib test

And in lib.rs:

-  #![cfg_attr(not(test), no_std)]
+ // specify no_std does not mean that the std crate cannot be linked.
+ // https://doc.rust-lang.org/reference/names/preludes.html#the-no_std-attribute
+ #![no_std]

It is now ready to run tests for the time being.