Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UndefinedFunctionError on custom allowlist modules #2

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Dev

### Bug fixes

- Fix `UndefinedFunctionError` when using external modules in a custom allowlist

## v0.3.3 (2023-08-13)

### Bug fixes
Expand Down
25 changes: 25 additions & 0 deletions lib/dune/allowlist.ex
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ defmodule Dune.Allowlist do
quote do
unquote(def_spec(spec))
unquote(def_fun_status(spec))

@on_load :ensure_alias_atoms

# Aliases like Foo.Bar are represented on the AST level as {:alias, _, [:Foo, :Bar]}
# We need to force the creation of these atoms, which might otherwise not be
# available when we parse due to atom encoding logic.
def ensure_alias_atoms do
# do nothing and returns :ok
Enum.each(unquote(alias_atoms(spec)), & &1)
end
end
end

Expand Down Expand Up @@ -236,4 +246,19 @@ defmodule Dune.Allowlist do
:ok
end
end

defp alias_atoms(spec) do
spec.modules
|> Enum.flat_map(fn {mod, _} ->
try do
Module.split(mod)
rescue
ArgumentError ->
# "expected an Elixir module" error -> erlang module
[]
end
end)
|> Enum.map(&String.to_atom/1)
|> Enum.uniq()
end
end
11 changes: 2 additions & 9 deletions lib/dune/parser/atom_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,9 @@ defmodule Dune.Parser.AtomEncoder do
WithClauseError
]

# TODO add
@erlang_modules [:math]

@elixir_reprs @elixir_modules
@module_reprs @elixir_modules
|> Enum.flat_map(&Module.split/1)
|> Enum.map(&{&1, String.to_existing_atom(&1)})

@erlang_reprs Enum.map(@erlang_modules, &{Atom.to_string(&1), &1})

@module_reprs Map.new(@elixir_reprs ++ @erlang_reprs)
|> Map.new(&{&1, String.to_existing_atom(&1)})

@spec load_atom_mapping(AtomMapping.t() | nil) :: :ok
def load_atom_mapping(nil), do: :ok
Expand Down
Loading