4

I build the project without any visible errors but when compiling it I encounter the error E0432 that tells me that serde and serde_json are not found when they have been declared in the Cargo.toml. I tried rebuilding the project from scratch, cargo check and cargo build multiple times and I'm still stuck

The error(s):

error[E0432]: unresolved import `serde`
 --> main.rs:1:5
  |
1 | use serde::{Deserialize, Serialize};
  |     ^^^^^ maybe a missing crate `serde`?

error[E0432]: unresolved import `serde_json`
 --> main.rs:2:5
  |
2 | use serde_json::Result;
  |     ^^^^^^^^^^ maybe a missing crate `serde_json`?

error: cannot determine resolution for the derive macro `Serialize`
 --> main.rs:4:10
  |
4 | #[derive(Serialize, Deserialize)]
  |          ^^^^^^^^^
  |
  = note: import resolution is stuck, try simplifying macro imports

error: cannot determine resolution for the derive macro `Deserialize`
 --> main.rs:4:21
  |
4 | #[derive(Serialize, Deserialize)]
  |                     ^^^^^^^^^^^
  |
  = note: import resolution is stuck, try simplifying macro imports

error[E0433]: failed to resolve: use of undeclared crate or module `serde_json`
  --> main.rs:14:32
   |
14 |     let config: ConfigStruct = serde_json::from_str(&config_data).unwrap();
   |                                ^^^^^^^^^^ use of undeclared crate or module `serde_json`

error: aborting due to 5 previous errors

Some errors have detailed explanations: E0432, E0433.
For more information about an error, try `rustc --explain E0432`.

main.rs file:

use serde::{Deserialize, Serialize};
use serde_json::Result;

#[derive(Serialize, Deserialize)]
struct ConfigStruct {
    playlist_name: String,
    replace_cover: bool,
    delete_songs: bool,
    songs_ids: Vec<String>,
}

fn get_config() -> Result<ConfigStruct> {
    let config_data = std::fs::read_to_string("../config.json").unwrap();
    let config: ConfigStruct = serde_json::from_str(&config_data).unwrap();
    Ok(config)
}

fn main() {
    let config = get_config().unwrap();

    println!("{}", config.playlist_name);
}

Cargo.toml file:

[package]
name = "grrs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = {version = "1.0.144", features = ["derive"] }
serde_json = "1.0.85"

Cargo.lock file:

# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3

[[package]]
name = "grrs"
version = "0.1.0"
dependencies = [
 "serde",
 "serde_json",
]

[[package]]
name = "itoa"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754"

[[package]]
name = "proc-macro2"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab"
dependencies = [
 "unicode-ident",
]

[[package]]
name = "quote"
version = "1.0.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179"
dependencies = [
 "proc-macro2",
]

[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"

[[package]]
name = "serde"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
dependencies = [
 "serde_derive",
]

[[package]]
name = "serde_derive"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
 "proc-macro2",
 "quote",
 "syn",
]

[[package]]
name = "serde_json"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44"
dependencies = [
 "itoa",
 "ryu",
 "serde",
]

[[package]]
name = "syn"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13"
dependencies = [
 "proc-macro2",
 "quote",
 "unicode-ident",
]

[[package]]
name = "unicode-ident"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd"
E_net4
  • 27,810
  • 13
  • 101
  • 139
La-lo-go
  • 150
  • 11
  • I just tried your code and it compiles just fine. What version of the compiler and plarform are you using? – rodrigo Sep 16 '22 at 11:03
  • the rustc version is 1.63.0 and i'm in Windows 10 – La-lo-go Sep 16 '22 at 11:20
  • [Works fine in playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f8d5b645baf83e08bebbcf2a18eb292d) (same crate versions) and also locally for me (same rustc version), so the problem must be in something other than what you've posted. – trent Sep 16 '22 at 11:34
  • You can try deleting the whole `target` subdirectory and rebuilding. – rodrigo Sep 16 '22 at 12:34
  • I think this is one of those situations where we need your entire project. Usually I would discourage this, but would you mind deleting the "target" folder and then putting the entire crate folder in a zip/rar file and posting a link? Together with the exact command you use to build? I'm pretty sure there is some minor detail wrong with your workflow that requires the entire project to reproduce. So far everything you posted is as it should be. – Finomnis Sep 16 '22 at 12:36
  • This problem might even require an interactive chat – Finomnis Sep 16 '22 at 12:40
  • Did you use other creates before that worked in `Cargo.toml`? – Finomnis Sep 16 '22 at 12:42
  • You can download the entire project [here](https://wetransfer.com/downloads/2ade38e767f4d8570ddb7c24dbb9ab2620220916130259/d23569) – La-lo-go Sep 16 '22 at 13:04
  • 2
    You wrote "I build the project without any visible errors but when compiling it..." What *exactly* do you mean by "compiling" as opposed to "building" here? (If you mean running `rustc main.rs` — that's the problem.) – Kevin Reid Sep 16 '22 at 14:18
  • @KevinReid oh, I completely skipped over that line. Yes, definitely. La-lo-go, are you using `rustc` directly? – Finomnis Sep 16 '22 at 14:51
  • @La-lo-go does `cargo build` give you the error you show, or is it a different command? – Finomnis Sep 16 '22 at 14:52
  • That was it! I run `rustc main.rs` and not `cargo run` it's my first day with Rust, sorry everyone and thanks to @KevinReid for pointing it out :D – La-lo-go Sep 16 '22 at 15:29
  • @La-lo-go If you are confused about where your binaries are, you can run `cargo build` or `cargo build --release` and then your executable binaries are in `target/debug` or `target/release`. Note that `cargo build`/`cargo run` internally use `rustc` for building, you will never have to execute `rustc` manually. – Finomnis Sep 16 '22 at 17:44
  • @La-lo-go Also, if you are new to Rust, first welcome to the community :) Then I would recommend reading the [Rust book](https://doc.rust-lang.org/stable/book/). Most of this (like what `rustup`/`rustc`/`cargo` is) is already detailed in its first chapter. This book is more or less mandatory for all Rust beginners :) – Finomnis Sep 16 '22 at 17:47

1 Answers1

2

You need to enable derive feature from serde crate.

Your cargo.toml file should look something like this:

[package]
name = "grrs"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0.144", features = ["derive"] }
serde_json = "1.0.85"
rspotify = "0.11.5"

Notice the feature key in the serde dependency decleration.

  • yeah i already did that and still the same. I have updated the question. – La-lo-go Sep 16 '22 at 11:19
  • It is always a good idea to specify your features explicitly, but as it happens, this project worked without it because [`rspotify` already requires that feature](https://github.com/ramsayleung/rspotify/blob/2aca7c67ff94da3ffc3fb346d93b7c9ebc911cc3/rspotify-model/Cargo.toml#L20). – rodrigo Sep 16 '22 at 12:34
  • @rodrigo agree, as I don't think it's considered a breaking change if a crate removes one of its dependencies' features. – Finomnis Sep 16 '22 at 14:55