just like this question
https://github.com/serde-rs/serde/issues/856#issuecomment-330878525
My use case is slightly different. I have a project set up as a Cargo workspace, and I am thinking about setting up an internal common crate to handle external dependencies shared by multiple internal crates. serde and serde_derive are some of those crates I'd like to share.
With the proc_macro feature, I can easily put the Deserialize/Serialize macros in scope wherever I need them. However, if I do so outside the common crate, I get the error in the issue title.
I haven't looked into the internal workings of these macros at all, but I'm guessing they are looking for ::serde (or maybe $crate::serde) in scope. However, in my case serde will be at ::common::serde. Is there any way I can work around this?
the common crate
//lib.rs
#[macro_use]
pub extern crate serde;
pub extern crate serde_json;
pub use serde::{Serialize,Deserialize};//for reexport macro
```toml
[dependencies]
serde = { version = "1.*" ,features = ["derive"]}
the main crate who want to use serde from common ccrate
```rust
//main.rs
#[macro_use]
extern crate common;
#[derive(Serialize)]
struct Test {
}
fn main() {
println!("Hello, world!");
}
```toml
[dependencies]
common = { path = "../common" }
it should be work but always report error like
```bash
error[E0463]: can't find crate for `_serde`
--> src\main.rs:5:10
|
5 | #[derive(Serialize)]
| ^^^^^^^^^ can't find crate
how should i do that?
There is no supported way to do this currently. The downstream crate will need to add its own dependency on serde.
If it is an absolute requirement that this work, then you could fork serde_derive and modify it to emit paths that refer to common::serde everywhere instead of serde.
I am confusing why it is impossible. could you please give me some explain or I am lose some extra information?
The code emitted by serde_derive would look something like:
impl serde::Serialize for Test {...
But if there is no such thing as serde in the main crate, then this code does not compile.
so we need some way to make serde been visible in the main crate. is that exactly what use should do?
All that's needed is for the main crate to add a dependency on Serde.
[dependencies]
serde = "1.0"
From there, the derived code will find it.
i know. yes, of course, i could do that, but i think it should not do. i am confusing why we have no ability to do. it seems rust could not use other crate's depended crate, right?
I have a very large repo/workspace at work. Many crates have to have their own serde line in the Cargo.toml now and it's going to potentially be a bit of a chore someday in the future. We already have a utils crate that re-exports common cli and logging setup. We have another for grouping common dependencies for certain tasks.
It would be very helpful for Serde to work correctly when re-exported as well.
this is painful for us, we have several repos with a few dozen crates and every time we want to do something with the serde dependencies it is a tedious hunt to go find all the places it is being used
@woodgear why did you close this, it seems unresolved?
Sorry to bother, is there any update on this?
My usecase is the same as OP
I have the same issue as the last few posters.
Based on https://github.com/yewstack/yew/issues/1454 looks like this is fixable now with crate::* on stable.
Most helpful comment
I have a very large repo/workspace at work. Many crates have to have their own serde line in the Cargo.toml now and it's going to potentially be a bit of a chore someday in the future. We already have a utils crate that re-exports common cli and logging setup. We have another for grouping common dependencies for certain tasks.
It would be very helpful for Serde to work correctly when re-exported as well.