Skip to content

Commit

Permalink
Do not print diagnostics twice to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Apr 29, 2023
1 parent 7547df0 commit ca5435f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
28 changes: 24 additions & 4 deletions lib/phoenix/code_reloader/proxy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 8,26 @@ defmodule Phoenix.CodeReloader.Proxy do
GenServer.start(__MODULE__, :ok)
end

def diagnostics(proxy, diagnostics) do
GenServer.cast(proxy, {:diagnostics, diagnostics})
end

def stop(proxy) do
GenServer.call(proxy, :stop)
GenServer.call(proxy, :stop, :infinity)
end

## Callbacks

def init(:ok) do
{:ok, ""}
{:ok, []}
end

def handle_cast({:diagnostics, diagnostics}, output) do
{:noreply, diagnostics |> Enum.map(&diagnostic_to_chars/1) |> Enum.reverse(output)}
end

def handle_call(:stop, _from, output) do
{:stop, :normal, output, output}
{:stop, :normal, Enum.reverse(output), output}
end

def handle_info(msg, output) do
Expand Down Expand Up @@ -47,6 55,18 @@ defmodule Phoenix.CodeReloader.Proxy do

defp put_chars(from, reply, chars, output) do
send(Process.group_leader(), {:io_request, from, reply, {:put_chars, chars}})
{:noreply, output <> IO.chardata_to_string(chars)}
{:noreply, [chars | output]}
end

defp diagnostic_to_chars(%{severity: :error, message: "**" <> _ = message}) do
"\n#{message}\n"
end

defp diagnostic_to_chars(%{severity: severity, message: message, file: file, position: position}) do
"\n#{severity}: #{message}\n #{Path.relative_to_cwd(file)}#{position(position)}\n"
end

defp position({line, col}), do: ":#{line}:#{col}"
defp position(line) when is_integer(line) and line > 0, do: ":#{line}"
defp position(_), do: ""
end
16 changes: 2 additions & 14 deletions lib/phoenix/code_reloader/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 302,8 @@ defmodule Phoenix.CodeReloader.Server do
defp run_compilers([compiler | compilers], args, acc) do
with {status, diagnostics} <- Mix.Task.run("compile.#{compiler}", args) do
# Diagnostics are written to stderr and therefore not captured,
# so we print them to the group leader here
Enum.each(diagnostics, &print_diagnostic/1)
# so we send them to the group leader here
Proxy.diagnostics(Process.group_leader(), diagnostics)
{status, diagnostics}
end
|> case do
Expand All @@ -320,16 320,4 @@ defmodule Phoenix.CodeReloader.Server do
:noop
end
end

defp print_diagnostic(%{severity: :error, message: "**" <> _ = message}) do
IO.write("\n#{message}\n")
end

defp print_diagnostic(%{severity: severity, message: message, file: file, position: position}) do
IO.write("\n#{severity}: #{message}\n #{Path.relative_to_cwd(file)}#{position(position)}\n")
end

defp position({line, col}), do: ":#{line}:#{col}"
defp position(line) when is_integer(line) and line > 0, do: ":#{line}"
defp position(_), do: ""
end

0 comments on commit ca5435f

Please sign in to comment.