Hi folks,
After delivering the library generating code from type specs, I realised that I'm missing the explicit indication of whether the function/macro is executed within the mix compile task, the mix test task, or interactively (iex / live book) during the compile-time.
Here is the code to illustrate the use case:
defmacro __using__(opts) do
...
cond do
in_mix_compile? ->
# plan the code generation when mix compile finishes to resolve type dependencies between BEAM files.
in_mix_test? ->
# raise an error and suggest compiling the module as an .ex file for the test environment.
true ->
# we're in iex / live book, generate code immediately because type dependencies are resolved during the sequential definition of the modules.
end
endCurrently, I use the following hacks to get to know the execution mode:
def in_mix_compile?(module_env) do
tracers = Map.get(module_env || %{}, :tracers, [])
Enum.member?(tracers, Mix.Compilers.ApplicationTracer)
end def in_mix_test?(_module_env) do
not is_nil(GenServer.whereis(ExUnit.Server))
endIs it an absurd idea to add the explicit field indicating execution mode like the following?:
%Module.Env{
execution_mode: :compile | :test | :interactive
}
that can be returned by __CALLER__ in a macro or by __EVN__ in a function.
Warm regards,
Ivan.