diff --git a/lib/ex_doc/formatter/html/autolink.ex b/lib/ex_doc/formatter/html/autolink.ex
index 5fad0c2df..7d6bded0d 100644
--- a/lib/ex_doc/formatter/html/autolink.ex
+++ b/lib/ex_doc/formatter/html/autolink.ex
@@ -542,27 +542,38 @@ defmodule ExDoc.Formatter.HTML.Autolink do
case Application.fetch_env(:ex_doc, :elixir_lib_dirs) do
{:ok, lib_dirs} ->
lib_dirs
- :error ->
- lib_dir =
- case :code.where_is_file('Elixir.Kernel.beam') do
- :non_existing ->
- [0]
- path ->
- path
- |> Path.dirname()
- |> Path.dirname()
- |> Path.dirname()
- end
+ :error ->
lib_dirs =
for app <- ~w(elixir eex iex logger mix ex_unit) do
- {lib_dir <> "/" <> app <> "/ebin", @elixir_docs <> app <> "/"}
+ {elixir_lib_dir(app), @elixir_docs <> app <> "/"}
end
+
Application.put_env(:ex_doc, :elixir_lib_dirs, lib_dirs)
lib_dirs
end
end
+ defp elixir_lib_dir(app) do
+ path =
+ case :code.where_is_file('Elixir.Kernel.beam') do
+ :non_existing -> ""
+ path -> List.to_string(path)
+ end
+
+ if File.exists?(path) do
+ path =
+ path
+ |> Path.dirname()
+ |> Path.dirname()
+ |> Path.dirname()
+ path <> "/" <> app <> "/ebin"
+ else
+ # if beam file doesn't exists it's likely an escript
+ Path.dirname(path)
+ end
+ end
+
defp erlang_lib_dirs do
case Application.fetch_env(:ex_doc, :erlang_lib_dirs) do
{:ok, lib_dirs} ->
diff --git a/test/ex_doc/formatter/html/autolink_test.exs b/test/ex_doc/formatter/html/autolink_test.exs
index a48ee3cc3..c36fb9f3d 100644
--- a/test/ex_doc/formatter/html/autolink_test.exs
+++ b/test/ex_doc/formatter/html/autolink_test.exs
@@ -34,6 +34,7 @@ defmodule ExDoc.Formatter.HTML.AutolinkTest do
# links to types without arity don't work
assert Autolink.local_doc("`t:my_type`", ["t:my_type/0"]) == "`t:my_type`"
end
+
test "autolink to basic and built-in types" do
assert Autolink.local_doc("`t:atom/0`", []) ==
"[`atom/0`](#{@elixir_docs}elixir/typespecs.html#basic-types)"