How do I recompile an Elixir project and reload it from within iex?

February 26, 2017:

To hot load components in a running elixir system with the lowest chance of something going wrong use:

case c(filename_ex, :in_memory) do
    [] -> :ignore
    [mod|_] -> r(mod)
end

Original answer:

In elixir 1.3.0 recompile does not restart the application anymore. So the correct way to check if any source changed and hotload is:

iex> recompile()

NOTE: I want to add that due to issues with removing modules during the recompile you will most likely crash processes while the recompile is occurring if you have in flight messages like a gen_statem with a state_timeout.

NOTE2: Using recompile/0 if you make an error in a source file, the project will compile with that source file missing and unloaded.


You can use the IEx.Helpers.recompile/0 function.

Recompiles the current Mix application.

This helper only works when IEx is started with a Mix project, for example, iex -S mix. Before compiling the code, it will stop the current application, and start it again afterwards. Stopping applications are required so processes in the supervision tree won't crash when code is upgraded multiple times without going through the proper hot-code swapping mechanism.

Changes to mix.exs or configuration files won't be picked up by this helper, only changes to sources. Restarting the shell and Mix is required in such cases.

If you want to reload a single module, consider using r ModuleName instead.

NOTE: This feature is experimental and may be removed in upcoming releases.

From https://github.com/elixir-lang/elixir/blob/v1.2.4/lib/iex/lib/iex/helpers.ex#L56-L93

Tags:

Elixir

Mix