From a972a4fdbb5e1bc21f1ef6a511cac83d17e4e205 Mon Sep 17 00:00:00 2001 From: Zach Allaun Date: Fri, 11 Aug 2023 11:39:35 -0400 Subject: [PATCH] fix: improve error handling for compiler diagnostics --- lib/next_ls/extensions/elixir_extension.ex | 2 +- lib/next_ls/runtime.ex | 27 ++++++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/next_ls/extensions/elixir_extension.ex b/lib/next_ls/extensions/elixir_extension.ex index fd36b30a..76a9ed35 100644 --- a/lib/next_ls/extensions/elixir_extension.ex +++ b/lib/next_ls/extensions/elixir_extension.ex @@ -24,7 +24,7 @@ defmodule NextLS.ElixirExtension do end @impl GenServer - def handle_info({:compiler, diagnostics}, state) do + def handle_info({:compiler, diagnostics}, state) when is_list(diagnostics) do DiagnosticCache.clear(state.cache, :elixir) for d <- diagnostics do diff --git a/lib/next_ls/runtime.ex b/lib/next_ls/runtime.ex index d38cdc33..9b878a42 100644 --- a/lib/next_ls/runtime.ex +++ b/lib/next_ls/runtime.ex @@ -120,7 +120,7 @@ defmodule NextLS.Runtime do NextLS.Logger.log(logger, "The runtime for #{name} has successfully shutdown.") reason -> - NextLS.Logger.error(logger, "The runtime for #{name} has crashed with reason: #{reason}.") + NextLS.Logger.error(logger, "The runtime for #{name} has crashed with reason: #{inspect(reason)}.") end end end) @@ -136,8 +136,8 @@ defmodule NextLS.Runtime do |> Path.join("monkey/_next_ls_private_compiler.ex") |> then(&:rpc.call(node, Code, :compile_file, [&1])) |> tap(fn - {:badrpc, :EXIT, {error, _}} -> - NextLS.Logger.error(logger, error) + {:badrpc, error} -> + NextLS.Logger.error(logger, {:badrpc, error}) _ -> :ok @@ -192,15 +192,22 @@ defmodule NextLS.Runtime do def handle_call(:compile, from, %{node: node} = state) do task = Task.Supervisor.async_nolink(state.task_supervisor, fn -> - {_, errors} = :rpc.call(node, :_next_ls_private_compiler, :compile, []) + case :rpc.call(node, :_next_ls_private_compiler, :compile, []) do + {:badrpc, error} -> + NextLS.Logger.error(state.logger, {:badrpc, error}) + [] - Registry.dispatch(state.registry, :extensions, fn entries -> - for {pid, _} <- entries do - send(pid, {:compiler, errors}) - end - end) + {_, diagnostics} when is_list(diagnostics) -> + Registry.dispatch(state.registry, :extensions, fn entries -> + for {pid, _} <- entries, do: send(pid, {:compiler, diagnostics}) + end) - errors + diagnostics + + unknown -> + NextLS.Logger.warning(state.logger, "Unexpected compiler response: #{inspect(unknown)}") + [] + end end) {:noreply, %{state | compiler_ref: %{task.ref => from}}}