Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 389
Mark Karavan Avatar asked Feb 28 '17 08:02

Mark Karavan


2 Answers

The reason is that *.exs files are for scripting and they won't be compiled and *.ex files will be compiled by elixir.

If you have no mix project and the user.ex file only then try elixirc user.ex and after this start iex and type h User.

If you have a mix project then start iex like so from the command line: iex -S mix This will load your project and compiles all *.ex files. Now type h User.

I tried both ways by myself and both work.

See also:

  • Scripted Mode
  • Question.
like image 116
guitarman Avatar answered Oct 02 '22 16:10

guitarman


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

like image 31
Dogbert Avatar answered Oct 02 '22 17:10

Dogbert