Get the path to the file import file at compile time

If I have a file that should be required for other files, is it possible to get the absolute path to the file that requires it?

So, if there lib_file.cris a macro that should be called app_file.crthat imported it, can this macro detect the file path app_file.crat compile time?

I tried things like this:

macro tester
  puts __DIR__
  puts __FILE__
end

But when called from a file that fulfills the request, it gives nothing at compile time, and this is at runtime:

?
expanded macro: app_file

If I changed the macro to this:

macro tester
  {{puts __DIR__}}
  {{puts __FILE__}}
end

This gives me this at compile time:

"/ home / user / Documents / crystal_test / lib_file"
"/home/user/Documents/crystal_test/lib_file.cr"

, app_file.cr lib_file.cr ?

+4
1

__FILE__, , .

macro tester(file = __FILE__)
  {{puts file}} # Print at compile time
  puts {{file}} # Print at runtime
end
+6

All Articles