Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaming serde via package = in Cargo.toml breaks #[derive(Deserialize, Serialize)] #1938

Closed
plaidfinch opened this issue Dec 25, 2020 · 3 comments

Comments

@plaidfinch
Copy link

plaidfinch commented Dec 25, 2020

Why this might matter

If a crate depending on serde wants to have a feature whose name itself is serde, it's necessary to rename the serde crate to avoid a feature/crate name conflict, like so:

Cargo.toml:

[package]
name = "serde-derive-bug"
version = "0.1.0"
edition = "2018"

[features]
serde = []

[dependencies]
serde-by-some-other-name = { package = "serde", version = "1", features = ["derive"] }

What goes wrong

However, this causes #[derive(Deserialize)] and #[derive(Serialize)] to break. Suppose we try:

src/lib.rs:

use serde_by_some_other_name::{Serialize, Deserialize};

#[derive(Serialize)]
#[derive(Deserialize)]
struct Something;

Although the imports of the derive macros succeed, the macros return the following compile errors:

error[E0463]: can't find crate for `serde`
 --> src/lib.rs:4:10
  |
4 | #[derive(Deserialize)]
  |          ^^^^^^^^^^^ can't find crate
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0463]: can't find crate for `serde`
 --> src/lib.rs:3:10
  |
3 | #[derive(Serialize)]
  |          ^^^^^^^^^ can't find crate
  |
  = note: this error originates in a derive macro (in Nightly builds, run with -Z macro-backtrace for more info)

The two errors cannot be triggered simultaneously because it appears compilation aborts at the first such error, even if the derive attributes are placed on totally separate structs. Commenting out one line or the other causes the remaining error to present itself.

As far as I can tell, there is no way to rename serde to another name using package = ... while preserving the ability to use #[derive(Deserialize, Serialize)]. Is there a workaround for this limitation?

@MikailBag
Copy link

I think #[serde(crate = "serde_by_some_other_name")] should work.

@plaidfinch
Copy link
Author

Thank you, @MikailBag!

@Mingun
Copy link
Contributor

Mingun commented Mar 13, 2021

@kwf, I think you may close this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants