Skip to content

Commit

Permalink
Silences recompilation warnings [#40]
Browse files Browse the repository at this point in the history
  • Loading branch information
szsoppa committed Aug 29, 2024
1 parent 0553af3 commit 163718f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Add the following to your `mix.exs` file:
```elixir
defp deps do
[
{:contexted, "~> 0.3.1"}
{:contexted, "~> 0.3.2"}
]
end
```
Expand Down Expand Up @@ -197,7 +197,7 @@ App.Account.find_user(1)

Both docs and specs are attached as metadata of module once it's compiled and saved as `.beam`. In reference to the example of `App.Account` context, it's possible that `App.Account.Users` will not be saved in `.beam` file before the `delegate_all` macro is executed. Therefore, first, all of the modules have to be compiled, and saved to `.beam` and only then we can create `@doc` and `@spec` of each delegated function.

As a workaround, in `Contexted.Tracer.after_compiler/1` all of the contexts `.beam` files are first deleted and then recompiled. This is an opt-in functionality, as it extends compilation time, and may produce warnings. If you want to enable it, set the following config value:
As a workaround, in `Contexted.Tracer.after_compiler/1` all of the contexts `.beam` files are first deleted and then recompiled. This is an opt-in functionality, as it extends compilation time. If you want to enable it, set the following config value:

```elixir
config :contexted,
Expand All @@ -206,6 +206,8 @@ config :contexted,

You may also want to enable it only for certain environments, like `dev`.

*Please also note that when this functionality is enabled, during the recompliation process, warnings are temporarly silenced to avoid logging conflicts warnings. It will still log warnings as intended, during first compilation, therefore it won't have any affect on normal compilation flow.*

Read more about `Contexted.Delegator` and its options in [docs](https://hexdocs.pm/contexted/Contexted.Delegator.html).

<br/>
Expand Down
20 changes: 19 additions & 1 deletion lib/contexted/tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ defmodule Contexted.Tracer do
beam_files_folder = extract_beam_files_folder()
file_paths = remove_context_beams_and_return_module_paths()

Kernel.ParallelCompiler.compile_to_path(file_paths, beam_files_folder)
silence_recompilation_warnings(fn ->
Kernel.ParallelCompiler.compile_to_path(file_paths, beam_files_folder)
end)

{status, diagnostics}
end
Expand Down Expand Up @@ -116,4 +118,20 @@ defmodule Contexted.Tracer do
|> Atom.to_string()
|> then(&~r/\b#{&1}\b/)
end

@spec silence_recompilation_warnings((() -> any())) :: any()
defp silence_recompilation_warnings(fun) do
original_logger_level = Logger.level()
original_compiler_options = Code.compiler_options()

Logger.configure(level: :error)
Code.compiler_options(ignore_module_conflict: true)

try do
fun.()
after
Logger.configure(level: original_logger_level)
Code.compiler_options(original_compiler_options)
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Contexted.MixProject do
app: :contexted,
description:
"Contexted is an Elixir library designed to streamline the management of complex Phoenix contexts in your projects, offering tools for module separation, subcontext creation, and auto-generating CRUD operations for improved code maintainability.",
version: "0.3.1",
version: "0.3.2",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down

0 comments on commit 163718f

Please sign in to comment.