Proc-macro referencing self module


I have written a proc macro for a project of mine. The generated code by this proc-macro uses the full path to identify an item (eg. Path::To::My::Item instead of hoping that Item is in scope). However, if Path is the name of the current crate, I cannot use this proc macro! Code in the crate Path can only refer to it via crate. However, I cannot change Path to crate because I also intend to use this proc-macro in other crates.
As a workaround I added use crate as Path to every file in the Path crate that uses my proc-macro, but this is terrible.

I guess there is an easy and obvious solution, but I could not find anything.

1 Like

There's an open issue about this:

There's also a partial workaround suggested in this proc-macro-crate issue:

extern crate self as Path would be a more "global" workaround than doing use crate as Path at each namespace.

Your XY problem, I think is that you want to test your proc-macro crate within the frontend crate that exports the item(s). And in order to perform those tests, extern crate self as public_name; is indeed the usual workaround.

1 Like

Minor correction: This should be extern crate self as public_name;

1 Like