To explain consider these two modules ModuleA and ModuleB
defmodule ModuleA do
@doc "ModuleA fun1"
def fun1(x) do
x
end
end
defmodule ModuleB do
require ModuleA
:functions |> ModuleA.__info__ |> IO.inspect
ModuleA |> Code.get_docs(:all) |> IO.inspect
end
If both modules are being compiled together, the get_docs in ModuleB returns nil. Only if ModuleA has been compiled previously does the get_docs in ModuleB succeed.
The crux of my question is how do I wait during the compilation of ModuleB for the beam file for ModuleA to exist?
First the modules need to be in separate files. There is an ensure_compiled function that you can use in ModuleB to be sure that ModuleA is available.
iex(1)> h Code.ensure_compiled
def ensure_compiled(module)
@spec ensure_compiled(module()) ::
{:module, module()}
| {:error, :embedded | :badfile | :nofile | :on_load_failure}
Ensures the given module is compiled and loaded.
If the module is already loaded, it works as no-op. If the module was not
loaded yet, it checks if it needs to be compiled first and then tries to load
it.
If it succeeds in loading the module, it returns {:module, module}. If not,
returns {:error, reason} with the error reason.
Check ensure_loaded/1 for more information on module loading and when to use
ensure_loaded/1 or ensure_compiled/1.
Re ensure_compiled/1 and ensure_loaded, I had already tried them and they do not guarantee the beam file exists, just that the module is loaded/compiled so that e.g. the ModuleA.__info__ call works.
Sorry I didn’t want to go “full monte” in the original post and clutter up my question.
I have already tried to find the beam file (using :code.which(ModuleA)) and although I get a path back, the path does not exists.
I’ve even tried (horror!) sleeping in ModuleB for a really (really!) long time but still can’t get the path / docs.
There is something about the way the ParallelCompiler works I don’t understand. Even though its code suggests ModuleA's beam file is written before the ModuleB's dependency is fulfilled (the compiled message is sent back to its compiler process), the beam file still seems to be not available.
I don’t have time to dig into this right now, but one thing to keep in mind is that one way to think of Elixir is that it’s a scripting language for running a compiler. You can put valid Elixir code outside the module def and that code will run at compile time. Something awful like
case :code.where_is_file('Elixir.ModuleA.beam') do
:not_existing -> loop_some_how
_ -> true
end
defmodule ModuleB do
require ModuleA
:functions |> ModuleA.__info__ |> IO.inspect
ModuleA |> Code.get_docs(:all) |> IO.inspect
end
I doubt that will work, but something like that is the solution.
I’d already tried sleeping inside the defmodule but not outside. (Since the parallel compile works with files not modules, and there is only one module in the file, the inside/outside distinction is moot in this case?).
But I tried outside: I put a Process.sleep(:infinity) outside the defmodule in ModuleB, touched both .ex files and did a mix compile. As you’d expect the mix just hung.
While the mix was hanging, I had a look at ebin and looked for the beam file for ModuleA which was surely compiled by then. There was no file.
It suggests that the parallelcompiler only writes the beam files late in the compilation, after (all?) the spawned compiler processes have completed.
Which begs the question as to the use case for Code.get_docs i.e. when can you use it?