ianrumford

ianrumford

Hello all. I’d appreciate some input on this.

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?

Thanks in advance.

btw I’m using 1.6.3

Showing Posts 1 to 6

bbense

bbense

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.
ianrumford

ianrumford OP

Hi.

Thanks for taking the time to respond.

The example modules were/are in separate files.

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.

bbense

bbense

Yes, you’re right. I missed that part. compiled != BEAM file in path.

It’s pretty straightforward to check that the BEAM file for the module is in the load_path, but I’m not sure how you’d force a compile if it is not.

:code.where_is_file('Elixir.ModuleA.beam')

ianrumford

ianrumford OP

Hi,

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.

Really appreciate your time helping on this.

bbense

bbense

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.

ianrumford

ianrumford OP

Hi,

Your time is appreciated :slight_smile:

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?

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews