cargo: Suggest splitting a crate to lib.rs and main.rs if an integration test can't find the crate because it's a binary
Describe the problem you are trying to solve
It might be a very frustrating experience for a newbie to read about integration tests, placing tests under tests/integration.rs, trying to import the main crate, only to find a cryptic message that the “crate is not found”.
Here’s an example:
error[E0463]: can't find crate for `example_binary`
--> tests/integration.rs:1:1
|
1 | extern crate example_binary;
| ^^^^^^^^^^^^^^^^^^^^ can't find crate
error: aborting due to previous error
Describe the solution you’d like
In the spirit of Rust’s helpful error messages, it would be good to have something in the vein of “help: actually, the crate example_binary exists, but it’s a binary crate, so it can’t be imported. If you want to test its functionality by calling it from Rust code, try to separate the APIs you want to test into a separate library, (a link to documentation that explains main.rs and lib.rs), with the relevant functions and types exported with pub.”
Notes
Of course, even more desirable thing here would be if it Simply Worked. For example, I kind of expected that you could call functions with pub even from a binary main.rs.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 13
- Comments: 15 (1 by maintainers)
I’ve just spent 4hrs reading docs and shuffling files in a project with no luck, until I’ve found this post… Also building
cdylib, which is technically a lib… anyways… Thanks guys for hints.What I ended up doing and that works is keeping only my
main.rs, having my “tests” folder undersrcbut adding#[cfg(tests)]abovemod tests;inmain.rs. All integration tests work and the cargo build doesn’t check errors and warns under that folder.I also ran into a similar issue today. I was building a
crate-type = ["cdylib"]and tried to useextern crate my_packagebut got the same error and couldn’t figure out why. Now that I know the fix, it totally makes sense, but the error messages left a little confused.For now, I’ll just comment that line out when I run integration tests.