# Using derive proc-macros in main crate

**URL:** <https://users.rust-lang.org/t/using-derive-proc-macros-in-main-crate/92607>\
**Category:** help\
**Created:** [April 16, 2023, 9:52am UTC](https://users.rust-lang.org/t/using-derive-proc-macros-in-main-crate/92607 "2023-04-16T09:52:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jthulhu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jthulhu/32/18771_2.png) [@jthulhu](https://users.rust-lang.org/u/jthulhu)\
**Post date:** [April 16, 2023, 9:52am UTC](https://users.rust-lang.org/t/using-derive-proc-macros-in-main-crate/92607/1 "2023-04-16T09:52:35Z")

</div>

I have a problem with the layout of proc macro crates. I have a library crate (let's call it `foo` ), which exports a trait `Bar` . Ideally, it also provides a derive macro but, since `foo` also uses declarative macros, I have to create a separate crate for this (let's call it `foo-derive` ).  
Now, the trait `Bar` is also implemented within `foo` , so I would like `foo` to depend on `foo-derive` so that I can use `#[derive(Bar)]` in `foo` . On the other hand, `foo-derive` uses fully-qualified path names for the types and traits it uses in foo, so for instance it would `impl ::foo::Bar for ... { ... }` .  
This works in any library that depends on `foo` , but not in `foo` itself, for which the path should be `crate::Bar` . Also, I can't make `foo-derive` publicly re-export the trait `Bar` in `foo` so that I can use `::foo_derive::Bar` instead, because then I would have a circular dependency between `foo` and `foo-derive` . Is there a way to solve this (ie. provide derive macros for everyone)?

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [April 16, 2023, 10:17am UTC](https://users.rust-lang.org/t/using-derive-proc-macros-in-main-crate/92607/2 "2023-04-16T10:17:52Z")

</div>

Here is an answer to your question that I've written in a different topic, hope that helps you:

> [@How to express crate path in procedural macros?](https://users.rust-lang.org/t/how-to-express-crate-path-in-procedural-macros/91274/17):
>
> Ah, I see. Yes, a workaround like use crate as my\_crate; would work. Or you could use an attribute, like I suggested. For example: use my\_crate\_derive::DeriveFoo; #[derive(DeriveFoo)] #[derive\_foo(crate = "crate")] struct Foo; I assume this has something to do with [extern crate](https://doc.rust-lang.org/reference/items/extern-crates.html), which you had to call in Rust 2015 explicitly in order to import crates. Since your current crate is not really an external crate, there is no implicit extern crate self as my\_crate; in your [prelude](https://doc.rust-lang.org/reference/names/preludes.html#extern-prelude), I'd say. Theref…

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [April 16, 2023, 10:33am UTC](https://users.rust-lang.org/t/using-derive-proc-macros-in-main-crate/92607/3 "2023-04-16T10:33:48Z")

</div>

also, see the discussion in [this issue](https://github.com/rust-lang/rust/issues/54363)

---

<div class="post-metadata">

**Author:** ![jthulhu](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jthulhu/32/18771_2.png) [@jthulhu](https://users.rust-lang.org/u/jthulhu)\
**Post date:** [April 16, 2023, 12:03pm UTC](https://users.rust-lang.org/t/using-derive-proc-macros-in-main-crate/92607/4 "2023-04-16T12:03:36Z")

</div>

> [@jofas](#):
>
> Here is an answer to your question that I've written in a different topic, hope that helps you:

The `extern crate self as foo` Just Works for me, however it's:

- a bit hacky: it's not entirely clear to someone who reads the code why is that even useful in the first place;
- a bit fragile: I've realized (I knew that but it didn't occur to me) that people can rename the dependency `foo` (or even not have it at all), and it will completely break the code.

However, while reading the discussion linked by @nerditation, I found a reference to the [proc-macro-crate](https://crates.io/crates/proc-macro-crate) crate, which solves all these issues. Given the name of the crate `foo`, it will fetch in `Cargo.toml` whether this crate is a dependency (or is the current crate), and in this case, provide the actual name (ie. after renaming) or `crate` if it's the current crate. This doesn't seem to be reliable if there is no `Cargo.toml` but, at this point, there is nothing more I can do I think.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [July 15, 2023, 12:04pm UTC](https://users.rust-lang.org/t/using-derive-proc-macros-in-main-crate/92607/5 "2023-07-15T12:04:03Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
