Elixir -- Module was not compiled with docs
Asked Answered
T

2

12

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?

Timid answered 28/2, 2017 at 8:59 Comment(3)
Change the extension of the file to be .ex.Variegated
@mudasobwa Problem remains, no different.Timid
Normally a .exs is not compiled (it's a script file). So @mudasobwa's suggestion is a good one. But if you're not using a mix file then I'd suggest you add one.Owens
D
20

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

Dufrene answered 28/2, 2017 at 15:28 Comment(0)
R
11

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:

Reservist answered 28/2, 2017 at 9:29 Comment(1)
Note that the extension doesn't matter with elixirc. elixirc user.exs also works.Dufrene

© 2022 - 2024 — McMap. All rights reserved.