Skip to content

Commit c191134

Browse files
committed
extract load_mix_config_with_diagnostics
1 parent 24b5509 commit c191134

File tree

1 file changed

+58
-54
lines changed
  • apps/language_server/lib/language_server

1 file changed

+58
-54
lines changed

apps/language_server/lib/language_server/build.ex

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -348,61 +348,10 @@ defmodule ElixirLS.LanguageServer.Build do
348348
Code.put_compiler_option(:no_warn_undefined, old_undefined)
349349

350350
if mixfile_status == :ok do
351-
# The project may override our logger config, so we reset it after loading their config
352-
# store log config
353-
logger_config = Application.get_all_env(:logger)
351+
# mixfile compiled successfully, we may attempt to load config
352+
{config_result, config_diagnostics} = load_mix_config_with_diagnostics(root_path)
354353

355-
{config_result, config_raw_diagnostics} =
356-
with_diagnostics([log: true], fn ->
357-
try do
358-
Mix.Task.run("loadconfig")
359-
:ok
360-
catch
361-
kind, err ->
362-
{payload, stacktrace} = Exception.blame(kind, err, __STACKTRACE__)
363-
{:error, kind, payload, stacktrace}
364-
after
365-
# reset log config
366-
Application.put_all_env(logger: logger_config)
367-
368-
if Version.match?(System.version(), ">= 1.15.0-dev") do
369-
# remove all log handlers and restore our
370-
for handler_id <- :logger.get_handler_ids(),
371-
handler_id != Logger.Backends.JsonRpc do
372-
:ok = :logger.remove_handler(handler_id)
373-
end
374-
375-
if Logger.Backends.JsonRpc not in :logger.get_handler_ids() do
376-
:ok =
377-
:logger.add_handler(
378-
Logger.Backends.JsonRpc,
379-
Logger.Backends.JsonRpc,
380-
Logger.Backends.JsonRpc.handler_config()
381-
)
382-
end
383-
end
384-
385-
# make sure ANSI is disabled
386-
Application.put_env(:elixir, :ansi_enabled, false)
387-
end
388-
end)
389-
390-
config_path = SourceFile.Path.absname(Mix.Project.config()[:config_path], root_path)
391-
392-
config_diagnostics =
393-
config_raw_diagnostics
394-
|> Enum.map(&Diagnostics.from_code_diagnostic(&1, config_path, root_path))
395-
396-
case config_result do
397-
{:error, kind, err, stacktrace} ->
398-
{:error,
399-
mixfile_diagnostics ++
400-
config_diagnostics ++
401-
[Diagnostics.from_error(kind, err, stacktrace, config_path, root_path)]}
402-
403-
:ok ->
404-
{:ok, mixfile_diagnostics ++ config_diagnostics}
405-
end
354+
{config_result, mixfile_diagnostics ++ config_diagnostics}
406355
else
407356
{mixfile_status, mixfile_diagnostics}
408357
end
@@ -417,6 +366,61 @@ defmodule ElixirLS.LanguageServer.Build do
417366
end
418367
end
419368

369+
# Runs the "loadconfig" task and resets logger/environment settings, collecting diagnostics.
370+
defp load_mix_config_with_diagnostics(root_path) do
371+
# The project may override our logger config, so we reset it after loading their config
372+
# store log config
373+
logger_config = Application.get_all_env(:logger)
374+
375+
{result, raw_diagnostics} =
376+
with_diagnostics([log: true], fn ->
377+
try do
378+
Mix.Task.run("loadconfig")
379+
:ok
380+
catch
381+
kind, err ->
382+
{payload, stacktrace} = Exception.blame(kind, err, __STACKTRACE__)
383+
{:error, kind, payload, stacktrace}
384+
after
385+
# reset log config
386+
Application.put_all_env(logger: logger_config)
387+
388+
if Version.match?(System.version(), ">= 1.15.0-dev") do
389+
# remove all log handlers and restore our
390+
for handler_id <- :logger.get_handler_ids(),
391+
handler_id != Logger.Backends.JsonRpc do
392+
:logger.remove_handler(handler_id)
393+
end
394+
395+
if Logger.Backends.JsonRpc not in :logger.get_handler_ids() do
396+
:logger.add_handler(
397+
Logger.Backends.JsonRpc,
398+
Logger.Backends.JsonRpc,
399+
Logger.Backends.JsonRpc.handler_config()
400+
)
401+
end
402+
end
403+
404+
# make sure ANSI is disabled
405+
Application.put_env(:elixir, :ansi_enabled, false)
406+
end
407+
end)
408+
409+
config_path = SourceFile.Path.absname(Mix.Project.config()[:config_path], root_path)
410+
411+
diagnostics =
412+
Enum.map(raw_diagnostics, &Diagnostics.from_code_diagnostic(&1, config_path, root_path))
413+
414+
case result do
415+
:ok ->
416+
{:ok, diagnostics}
417+
418+
{:error, kind, err, stacktrace} ->
419+
{:error,
420+
diagnostics ++ [Diagnostics.from_error(kind, err, stacktrace, config_path, root_path)]}
421+
end
422+
end
423+
420424
defp run_mix_compile(force?) do
421425
opts = [
422426
"--return-errors",

0 commit comments

Comments
 (0)