Skip to content

Commit

Permalink
load dependencies in application start callback (#151)
Browse files Browse the repository at this point in the history
When using the new PLUG_EDITOR integration, there was a problem where
not all dependencies were available when the code reloader recompiled
the code.
This lead to missing dependencies when the live reloader joined the
channel.
This change ensures that all we initially load the deps when the
application starts, so that later recompiles don't affect the deps list.
  • Loading branch information
SteffenDE committed Mar 7, 2024
1 parent e23ad33 commit 5d51a11
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
13 changes: 13 additions & 0 deletions lib/phoenix_live_reload/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ defmodule Phoenix.LiveReloader.Application do
# be started in dev via user's `only: :dev` entry.
WebConsoleLogger.attach_logger()

# the deps paths are read by the channel when getting the full_path for
# opening the configured PLUG_EDITOR
:persistent_term.put(:phoenix_live_reload_deps_paths, deps_paths())

children = [
WebConsoleLogger,
%{id: __MODULE__, start: {__MODULE__, :start_link, []}}
Expand Down Expand Up @@ -48,4 +52,13 @@ defmodule Phoenix.LiveReloader.Application do
other
end
end

defp deps_paths do
# TODO: Use `Code.loaded?` on Elixir v1.15+
if :erlang.module_loaded(Mix.Project) do
for {app, path} <- Mix.Project.deps_paths(), into: %{}, do: {to_string(app), path}
else
%{}
end
end
end
12 changes: 1 addition & 11 deletions lib/phoenix_live_reload/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ defmodule Phoenix.LiveReloader.Channel do
|> assign(:patterns, config[:patterns] || [])
|> assign(:debounce, config[:debounce] || 0)
|> assign(:notify_patterns, config[:notify] || [])
|> assign(:deps_paths, deps_paths())

{:ok, join_info(), socket}
else
Expand Down Expand Up @@ -76,7 +75,7 @@ defmodule Phoenix.LiveReloader.Channel do
end

def handle_in("full_path", %{"rel_path" => rel_path, "app" => app}, socket) do
case socket.assigns.deps_paths do
case :persistent_term.get(:phoenix_live_reload_deps_paths) do
%{^app => dep_path} ->
{:reply, {:ok, %{full_path: Path.join(dep_path, rel_path)}}, socket}

Expand Down Expand Up @@ -130,13 +129,4 @@ defmodule Phoenix.LiveReloader.Channel do
%{}
end
end

defp deps_paths do
# TODO: Use `Code.loaded?` on Elixir v1.15+
if :erlang.module_loaded(Mix.Project) do
for {app, path} <- Mix.Project.deps_paths(), into: %{}, do: {to_string(app), path}
else
%{}
end
end
end

0 comments on commit 5d51a11

Please sign in to comment.