rust-lang / rust

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

Unhelpful error when trying to import from a crate without 'lib' crate-type #96039

Open jplatte opened 2 years ago

jplatte commented 2 years ago

When working on a crate using crate-type = ["cdylib"] or crate-type = ["staticlib"]¹, one may want to also write unit tests (or examples) for the public functions in that crate. This "simply" requires adding the default "lib" crate-type back into the Cargo.toml.

However, discovering that isn't exactly straight-forward. Cargo doesn't complain when there's tests or examples in one such crate but once you try to import something from the crate you want to test, you get:

error[E0432]: unresolved import `the_crate`
 --> tests/foo.rs:1:5
  |
1 | use the_crate::SomeType;
  |     ^^^^^^^^^ use of undeclared crate or module `the_crate`
  |

Given Rust's excellent diagnostics elsewhere, I would expect either

¹ plugins for applications, or libraries meant to be used in codebases written in a different language than Rust

WilliamForsdal commented 2 years ago

Just ran into this as well. I'm building a cdylib and I noticed that the final dll size is increased by about 300kB when adding "lib", so it's a tradeoff.