Unresolved import for a crate on crates.io

I have published a crate (serial_traits), and I want to use it for my other projects.
However, if I do use serial_traits, I get red underlines with this hover popup:

unresolved import `serial_traits`
if you wanted to use a crate named `serial_traits`, use `cargo add serial_traits` to add it to your `Cargo.toml`

The problem is that this error message is contradicting Cargo.toml:

[package]
name = "common"
version = "0.1.0"
edition = "2024"

[dependencies]
glam = "0.30.4"
serial_traits = "1.1.0"

This is a workplace, and the common crate with its dependencies was initialized with the following commands:

cargo new common --lib
cargo add glam
cargo add serial_traits

An extra thing I have done is that I added net_structs.rs (where I am trying to use serial_traits in), which I have let VSCode know the existence of by putting pub mod net_structs; in lib.rs.

I saw that others have had the same or similar problem and solved it (like this one) and I tried their solutions.
Their suggestions did not work.

Something I have noticed is that occasionally, after changing edition (trying other editions), rust-analyzer would temporarily accept that serial_traits exists.
And yet, cargo test and cargo build would still give this output:

error[E0432]: unresolved import `serial_traits`
>cargo test
   Compiling glam v0.30.4
   Compiling serial_traits v1.1.0
   Compiling common v0.1.0 (C:\Users\[me]\Projects\Git Repos\wasm_2dtestmpgame\common)
error[E0432]: unresolved import `serial_traits`
 --> common\src\net_structs.rs:2:5
  |
2 | use serial_traits::Serializable;
  |     ^^^^^^^^^^^^^ use of unresolved module or unlinked crate `serial_traits`
  |
  = help: if you wanted to use a crate named `serial_traits`, use `cargo add serial_traits` to add it to your `Cargo.toml`

For more information about this error, try `rustc --explain E0432`.
error: could not compile `common` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...

The cargo build and cargo test commands were ran after cargo cleaning, and it shows that serial_traits successfully compiles.

Other things I have tried out, repeatedly, in arbitrary orders:

  • Restarting VSCode
  • Closing and reopening Command Prompt
  • cargo clean
  • Removing and readding serial_traits
  • rustup update
  • Directly copy and pasting the crate name from and to several places in case I wrote it wrong

Is there something I am missing? Maybe during the publishing process?
I have no idea what other options even exist, and I don't want to clone copies of my crate into each project I want to use it in.

when you do that and run cargo build you should also get a warning:

warning: The package `serial_traits` provides no linkable target. The compiler might raise an error while compiling `common`. Consider adding 'dylib' or 'rlib' to key `crate-type` in `serial_traits`'s Cargo.toml. This warning might turn into a hard error in the future.

That's because in your code you defined it as a crate-type = ["cdylib"]:

I don't know why you did that, but you probably just want ["lib"] there. Or leave it off completely.

1 Like

The published source code for your library contains:

[lib]
crate-type = ["cdylib"]

This makes it ineligible for use as a Rust dependency, and only usable as a dynamically-loaded library for non-Rust programs. You should probably remove this line. (Why did you add it?)

I recommend that you should always test a library is usable as a dependency before publishing it — the easiest way to do it is with Cargo “integration tests” which are placed in the tests/ directory:

Cargo.toml
src/
    lib.rs
tests/
    my-test.rs

In tests/my-test.rs, write #[test] functions that make use of your library. (I see you have some test functions already; it may be wise to move them to tests/ to get more coverage, presuming they don't use any private items.) Then run cargo test to run those tests. This will prove that the library is in fact usable from a dependent crate.

You can also use documentation tests for the same purpose, providing examples in your library documentation that are proven to work by running them.

"cdylib" was the problem and it works after I changed it! thank you very much!
Though, I have no idea why I made it a cdylib ¯\_(ツ)_/¯