Proc-macro: how to identify file being compiled?

I have a macro that I’d like to make behave like include_str!, which interprets the path relative to the location of the source file. Any idea how to discover this? It doesn’t seem to be exposed in the API. Could we not implement include_str! as a procedural macro?

Perhaps you might be looking for file!? Don’t forget that there is also a line! and column to help as well.

include_str and macros like it are compiler built ins, but you can use macros like file to get the current file name.

Thanks, that is helpful! Unfortunately, I probably can’t use file! to get the current filename to find out the filename within a proc-macro, right? The expansion of any file! I generate won’t be visible until after my proc-macro has been evaluated (when it’s too late to open a file relative to the source file.

What I ended up doing (which is very hokey) is to just look for the text of the current macro in all .rs files, and hope that it is unique. (See my actual code).

See also #54725 which asks for a way to obtain this information.