Get file attribute for amodule

I have the following minimal module (the smallest possible module generated by Elixir):

-file("lib/darwin/ex_to_erl.ex", 0).

-module('Elixir.MyModule').

-compile([no_auto_import]).

-export(['__info__'/1]).

-spec '__info__'(attributes | compile | functions |
                 macros | md5 | module | deprecated) -> any().

'__info__'(module) -> 'Elixir.MyModule';
'__info__'(functions) -> [];
'__info__'(macros) -> [];
'__info__'(Key = attributes) ->
    erlang:get_module_info('Elixir.MyModule', Key);
'__info__'(Key = compile) ->
    erlang:get_module_info('Elixir.MyModule', Key);
'__info__'(Key = md5) ->
    erlang:get_module_info('Elixir.MyModule', Key);
'__info__'(deprecated) -> [].

I don’t understand why :erlang.get_module_info/2 works the way it does:

iex> :erlang.get_module_info(MyModule, :attributes)
[vsn: [147295737053399631997724789219256487707]]

I was expecting a list that included the :file attribute. Why does it only return the module version?

Because :file isn’t really a module attribute. It’s more of a compiler meta data.

2 Likes

Hm… Ok. Is there a recommended way of getting the file attribute from a BEAM file? Maybe the debug chunk?

Poking a random module with :erlang.get_module_info(...) suggests you might find what you’re looking for under :compile - in particular, the source key.

Beware of :beam_lib.strip, the documentation mentions that it can be used to remove segments from BEAM files that aren’t needed, and compile is one of those.

1 Like