【问题标题】:Elixir -- Module was not compiled with docsElixir -- 模块未使用文档编译
【发布时间】:2017-07-19 03:46:00
【问题描述】:

我昨天才开始学习灵药。我有一个文件 User.exs。它看起来像这样:

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

end

当我运行 iex 时,当我尝试查看文档时会发生以下情况:

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

有什么想法吗?

【问题讨论】:

  • 将文件的扩展名改为.ex
  • @mudasobwa 问题依然存在,没有什么不同。
  • 通常不会编译 .exs(它是一个脚本文件)。所以@mudasobwa 的建议是一个很好的建议。但如果你不使用混合文件,那么我建议你添加一个。

标签: documentation elixir elixir-iex


【解决方案1】:

c("user.exs") 在内存中编译文件并且不将字节码(.beam 文件)写入磁盘,而h/1 当前需要(详细信息如下)beam 文件存在于磁盘上才能工作。您可以让c 将生成的字节码存储在当前目录中,这将使h/1c("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 依赖于Code.get_docs/2 来获取在模块上调用:code.get_object_code/1 的文档。 :code.get_object_code/1 根据its docs, "在代码路径中搜索模块Module的目标代码。成功返回{Module, Binary, Filename},否则返回error。"

【讨论】:

    【解决方案2】:

    原因是*.exs 文件用于编写脚本,它们不会被编译,*.ex 文件将由 elixir 编译。

    如果您没有混合项目且只有user.ex 文件,请尝试elixirc user.ex,然后启动iex 并输入h User

    如果你有一个混合项目,那么从命令行启动 iex:iex -S mix 这将加载您的项目并编译所有*.ex 文件。现在输入h User

    我自己尝试了这两种方法,并且都有效。

    另见:

    【讨论】:

    • 请注意,扩展名与elixirc 无关。 elixirc user.exs 也可以。
    猜你喜欢
    • 2020-02-23
    • 2017-10-31
    • 2016-11-16
    • 1970-01-01
    • 2021-04-23
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    相关资源
    最近更新 更多