Search code examples
documentationelixirelixir-iex

Elixir -- Module was not compiled with docs


I just started learning elixir yesterday. I have a file User.exs. It looks like this:

defmodule User do
    @moduledoc """ 
    Defines the user struct and functions to handle users.
    """
    # functions and stuff go here...

end

When I run iex, this is what happens when I try to see the docs:

iex(1)> c "user.exs"
[User]
iex(2)> h User
User was not compiled with docs

Any ideas?


Solution

  • c("user.exs") compiles the file in memory and does not write the bytecode (.beam file) to disk while h/1 currently requires (details below) the beam file to be present on disk to work. You can make c store the generated bytecode in the current directory which will make h/1 work with c("user.exs", "."):

    $ ls
    user.exs
    $ cat user.exs
    defmodule User do
      @moduledoc """
      Defines the user struct and functions to handle users.
      """
    end
    $ iex
    Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
    
    Interactive Elixir (1.4.2) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> c("user.exs", ".")
    [User]
    iex(2)> h User
    
                                          User
    
    Defines the user struct and functions to handle users.
    
    iex(3)>
    BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
           (v)ersion (k)ill (D)b-tables (d)istribution
    ^C
    $ ls
    Elixir.User.beam user.exs
    

    h/1 relies on Code.get_docs/2 to fetch the documentation which calls :code.get_object_code/1 on the module. :code.get_object_code/1 according to its docs, "Searches the code path for the object code of module Module. Returns {Module, Binary, Filename} if successful, otherwise error."