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

Does not work correctly in procedural macros called from examples #14

Open
DCNick3 opened this issue Dec 18, 2021 · 5 comments
Open

Does not work correctly in procedural macros called from examples #14

DCNick3 opened this issue Dec 18, 2021 · 5 comments

Comments

@DCNick3
Copy link

DCNick3 commented Dec 18, 2021

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 rather use 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

@DCNick3
Copy link
Author

DCNick3 commented Dec 18, 2021

Seems quite similar to the problem in #10, but there doesn't seem to be the same magic env var like CARGO_TARGET_TMPDIR described at https://doc.rust-lang.org/cargo/reference/environment-variables.html

@bkchr
Copy link
Owner

bkchr commented Dec 20, 2021

Seems quite similar to the problem in #10, but there doesn't seem to be the same magic env var like CARGO_TARGET_TMPDIR described at https://doc.rust-lang.org/cargo/reference/environment-variables.html

Maybe there exists something similar.

@ModProg
Copy link

ModProg commented Sep 7, 2023

You can use CARGO_CRATE_NAME.

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, rust-analyzer currently has a bug: rust-lang/rust-analyzer#15572

@ModProg
Copy link

ModProg commented Oct 1, 2023

So it turns out this workaroud doesn't work for doctests, because those compile with the same crate name.

@ModProg
Copy link

ModProg commented Oct 1, 2023

I found a solution, that basically allows using ::crate_name inside your crate in place of crate:

You can use extern crate self as my_crate_name; to make ::my_crate_name work even inside your own crate.

by bjorn3 on zulip

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

No branches or pull requests

3 participants