0

I have a proc macro that needs to read files relative to the caller location:

// main_crate/src/lib.rd
use other_crate::my_proc_macro;

my_proc_macro!(file1, file2)

This would mean the proc macto needs to read main_crate/some_dir/file1.txt and main_crate/some_dir/file2.txt. When To do this properly so it can be used as a dependency, I need to know the absolute path to main_crate when running my_proc_macro to be able to generate constants.

I don't think I can use the #[track_caller] attribute because that wouldn't give me an absolute path and would be run after the proc macro has finished. I also can't use include_str! because that's expanded after the proc macro is finished. Same issue with file!.

Thanks in advance

nxe
  • 241
  • 2
  • 9

1 Answers1

0

I'm going to use environment variables for this. Because the proc macro crate is under the main crate, I'm just using env!("CARGO_MANIFEST_DIR") and getting relative paths from that.

If you didn't have the proc macro crate always relative to the crate using it, then I'd recommend using environment variables in the build script for the main crate that the proc macro runs on.

nxe
  • 241
  • 2
  • 9