-
Notifications
You must be signed in to change notification settings - Fork 17
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
Does not work correctly in procedural macros called from examples #14
Comments
Seems quite similar to the problem in #10, but there doesn't seem to be the same magic env var like |
Maybe there exists something similar. |
You can use I'm using this and it works as long as you don't name any of your examples the same as your crate: let htmx = match (
proc_macro_crate::crate_name("htmx"),
std::env::var("CARGO_CRATE_NAME").as_deref(),
) {
(Ok(FoundCrate::Itself), Ok("htmx")) => quote!(crate),
(Ok(FoundCrate::Name(name)), _) => {
let ident = Ident::new(&name, Span::call_site());
quote!(::#ident)
}
_ => quote!(::htmx),
}; Only issue is, |
So it turns out this workaroud doesn't work for doctests, because those compile with the same crate name. |
I found a solution, that basically allows using
|
Suppose we have a library crate
smol-lib
.When the library is called in context of an example for this library (i.e.
smol-lib/examples/smol.rs
) trying to look up the name of the same library (smol-lib
), this library resolves that the crate is itself.This makes sense, but, unfortunately, doesn't correspond to the way you would use the crate from an example: you would not write
use crate::SmolStruct
as inside the library code, but ratheruse smol_lib::SmolStruct
, the same way as you would in a downstream crate.This is problematic, because it doesn't allow to write examples for the repo using macros and, at the same time, using macros both inside and outside of
smol-lib
.Probably some logic should be added to handle the examples case, not sure how it could be done though
The text was updated successfully, but these errors were encountered: