Skip to content

Commit 74beb7f

Browse files
Add iex option to automatically purge recompiled modules (#13919)
1 parent 90fae5d commit 74beb7f

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

lib/iex/lib/iex.ex

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ defmodule IEx do
357357
13
358358
359359
It is possible to load another file by configuring the `iex` application's `dot_iex`
360-
value (`config :iex, dot_iex: "PATH"` or `IEx.Config.configure(dot_iex: "PATH")`)
360+
value (`config :iex, dot_iex: "PATH"` or `IEx.configure(dot_iex: "PATH")`)
361361
or supplying the `--dot-iex` option to IEx. See `iex --help`.
362362
363363
In case of remote nodes, the location of the `.iex.exs` files are taken
@@ -488,6 +488,9 @@ defmodule IEx do
488488
* `:alive_continuation_prompt` - used when `Node.alive?/0` returns
489489
`true` and more input is expected
490490
491+
* `:auto_reload` - when set to `true`, automatically purges in-memory
492+
modules when they get invalidated by a concurrent compilation
493+
491494
The following values in the prompt string will be replaced appropriately:
492495
493496
* `%counter` - the index of the history

lib/iex/lib/iex/config.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ defmodule IEx.Config do
1414
:alive_continuation_prompt,
1515
:width,
1616
:parser,
17-
:dot_iex
17+
:dot_iex,
18+
:auto_reload
1819
]
1920

2021
# Read API
@@ -93,6 +94,10 @@ defmodule IEx.Config do
9394
Application.get_env(:iex, :dot_iex)
9495
end
9596

97+
def auto_reload?() do
98+
Application.fetch_env!(:iex, :auto_reload)
99+
end
100+
96101
# Used by default on evaluation cycle
97102
defp default_color(:eval_interrupt), do: [:yellow]
98103
defp default_color(:eval_result), do: [:yellow]
@@ -199,6 +204,7 @@ defmodule IEx.Config do
199204
defp validate_option({:width, new}) when is_integer(new), do: :ok
200205
defp validate_option({:parser, tuple}) when tuple_size(tuple) == 3, do: :ok
201206
defp validate_option({:dot_iex, path}) when is_binary(path), do: :ok
207+
defp validate_option({:auto_reload, enabled}) when is_boolean(enabled), do: :ok
202208

203209
defp validate_option(option) do
204210
raise ArgumentError, "invalid configuration #{inspect(option)}"

lib/iex/lib/iex/mix_listener.ex

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@ defmodule IEx.MixListener do
2525

2626
@impl true
2727
def handle_call(:purge, _from, state) do
28-
for module <- state.to_purge do
29-
:code.purge(module)
30-
:code.delete(module)
31-
end
32-
28+
purge_modules(state.to_purge)
3329
status = if Enum.empty?(state.to_purge), do: :noop, else: :ok
34-
3530
{:reply, status, %{state | to_purge: MapSet.new()}}
3631
end
3732

@@ -43,13 +38,27 @@ defmodule IEx.MixListener do
4338
{:noreply, state}
4439
else
4540
%{changed: changed, removed: removed} = info.modules_diff
46-
state = update_in(state.to_purge, &Enum.into(changed, &1))
47-
state = update_in(state.to_purge, &Enum.into(removed, &1))
48-
{:noreply, state}
41+
42+
if IEx.Config.auto_reload?() do
43+
purge_modules(changed)
44+
purge_modules(removed)
45+
{:noreply, state}
46+
else
47+
state = update_in(state.to_purge, &Enum.into(changed, &1))
48+
state = update_in(state.to_purge, &Enum.into(removed, &1))
49+
{:noreply, state}
50+
end
4951
end
5052
end
5153

5254
def handle_info(_message, state) do
5355
{:noreply, state}
5456
end
57+
58+
defp purge_modules(modules) do
59+
for module <- modules do
60+
:code.purge(module)
61+
:code.delete(module)
62+
end
63+
end
5564
end

lib/iex/mix.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ defmodule IEx.MixProject do
1818
inspect: [pretty: true],
1919
history_size: 20,
2020
default_prompt: "%prefix(%counter)>",
21-
alive_prompt: "%prefix(%node)%counter>"
21+
alive_prompt: "%prefix(%node)%counter>",
22+
auto_reload: false
2223
]
2324
]
2425
end

0 commit comments

Comments
 (0)