Is is possible to control the order in which modules are compiled?

I ran into a problem when working on a new package designed to inspect Ecto schemas: inspecto | Hex

I suppose this is “obvious” in retrospect, but when a module is being compiled and the @moduledoc block is being evaluated, not all of an app’s modules are available. It appears that modules are compiled in a sorted order (alphabetically?). My observation is that if I inspect the output of :code.all_available() inside my @moduledoc, the results are NOT consistent depending on which module I put it in. That is to say, the list of available modules changes as things are compiled (which sounds quite logical).

Are there ways to control the order in which modules are compiled? Or is there something akin to Application.ensure_started/1 that can make a module wait until the other modules are loaded before it attempts to read them? Or is this entirely the wrong approach?

Thanks for any pointers!

If you require or import a module, it will ensure it compiles before the current module. There’s not much more control beyond that.

2 Likes