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
The presence of a test or example on a non-lib crate to raise a warning in addition to this error (this would probably be a simple Cargo change, but might also be annoying to some projects)
The error to include a hint about the_crate existing, but not being available due to its crate-type (this might require cargo passing more info to rustc first?)
¹ plugins for applications, or libraries meant to be used in codebases written in a different language than Rust
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.
When working on a crate using
crate-type = ["cdylib"]
orcrate-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 theCargo.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:
Given Rust's excellent diagnostics elsewhere, I would expect either
lib
crate to raise a warning in addition to this error (this would probably be a simple Cargo change, but might also be annoying to some projects)the_crate
existing, but not being available due to itscrate-type
(this might require cargo passing more info to rustc first?)¹ plugins for applications, or libraries meant to be used in codebases written in a different language than Rust