Can you please explain why you need to know the execution mode. The fact a library can compile different code depending if it is compile, test, or something else is not something we should encourage.
On Thu, Dec 9, 2021 at 8:37 PM Ivan Rublev <ivan.rublev.busin...@gmail.com> wrote: > 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 > end > > Currently, 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)) > end > > Is 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. > > -- > You received this message because you are subscribed to the Google Groups > "elixir-lang-core" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to elixir-lang-core+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elixir-lang-core/cb990143-1498-4ccc-b982-73515656f91an%40googlegroups.com > <https://groups.google.com/d/msgid/elixir-lang-core/cb990143-1498-4ccc-b982-73515656f91an%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "elixir-lang-core" group. To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4L-0zi%2BL3ntHYnb%2BUP4DS_8mcNiNyjXC2smT_PVQpOLuQ%40mail.gmail.com.