From 7c03b8d109cbbe71f5d45499b6318933330f08aa Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Tue, 8 Aug 2023 08:11:30 +0200 Subject: [PATCH] make logger interception more robust Fixes https://github.com/elixir-lsp/elixir-ls/issues/947 --- apps/elixir_ls_debugger/lib/debugger/cli.ex | 2 +- .../elixir_ls_debugger/test/debugger_test.exs | 2 +- apps/elixir_ls_debugger/test/test_helper.exs | 2 +- apps/elixir_ls_utils/lib/launch.ex | 2 +- .../lib/language_server/build.ex | 54 ++++++++++++------- .../lib/language_server/cli.ex | 2 +- .../experimental/code_mod/format.ex | 2 +- .../lib/language_server/source_file.ex | 2 +- apps/language_server/test/server_test.exs | 2 +- .../test/support/server_test_helpers.ex | 2 +- 10 files changed, 43 insertions(+), 29 deletions(-) diff --git a/apps/elixir_ls_debugger/lib/debugger/cli.ex b/apps/elixir_ls_debugger/lib/debugger/cli.ex index 9829808df..de9ffb780 100644 --- a/apps/elixir_ls_debugger/lib/debugger/cli.ex +++ b/apps/elixir_ls_debugger/lib/debugger/cli.ex @@ -7,7 +7,7 @@ defmodule ElixirLS.Debugger.CLI do WireProtocol.intercept_output(&Output.debuggee_out/1, &Output.debuggee_err/1) Launch.start_mix() - if Version.match?(System.version(), ">= 1.15.0-dev") do + if Version.match?(System.version(), ">= 1.15.0") do # make sue that debugger modules are in code path # without starting the app Mix.ensure_application!(:debugger) diff --git a/apps/elixir_ls_debugger/test/debugger_test.exs b/apps/elixir_ls_debugger/test/debugger_test.exs index 6e5ed1908..008c3057c 100644 --- a/apps/elixir_ls_debugger/test/debugger_test.exs +++ b/apps/elixir_ls_debugger/test/debugger_test.exs @@ -2331,7 +2331,7 @@ defmodule ElixirLS.Debugger.ServerTest do Server.receive_packet(server, request(6, "threads", %{})) assert_receive(response(_, 6, "threads", %{"threads" => threads}), 1_000) - if Version.match?(System.version(), ">= 1.15.0-dev") do + if Version.match?(System.version(), ">= 1.15.0") do assert Enum.find(threads, &(&1["id"] == thread_id))["name"] == "Task.Supervised.noreply/4 #{:erlang.pid_to_list(pid)}" else diff --git a/apps/elixir_ls_debugger/test/test_helper.exs b/apps/elixir_ls_debugger/test/test_helper.exs index 44c7073d9..fd8139a48 100644 --- a/apps/elixir_ls_debugger/test/test_helper.exs +++ b/apps/elixir_ls_debugger/test/test_helper.exs @@ -1,7 +1,7 @@ Application.put_env(:elixir_ls_debugger, :test_mode, true) ExUnit.start(exclude: [pending: true]) -if Version.match?(System.version(), ">= 1.15.0-dev") do +if Version.match?(System.version(), ">= 1.15.0") do # make sue that debugger modules are in code path # without starting the app Mix.ensure_application!(:debugger) diff --git a/apps/elixir_ls_utils/lib/launch.ex b/apps/elixir_ls_utils/lib/launch.ex index e2ce87f8e..9dff8154c 100644 --- a/apps/elixir_ls_utils/lib/launch.ex +++ b/apps/elixir_ls_utils/lib/launch.ex @@ -80,7 +80,7 @@ defmodule ElixirLS.Utils.Launch do file = ElixirLS.Utils.MixfileHelpers.mix_exs() if File.regular?(file) do - if Version.match?(System.version(), ">= 1.15.0-dev") do + if Version.match?(System.version(), ">= 1.15.0") do Mix.ProjectStack.post_config(state_loader: {:cli, List.first(args)}) end diff --git a/apps/language_server/lib/language_server/build.ex b/apps/language_server/lib/language_server/build.ex index 3e29563b3..19412ce8f 100644 --- a/apps/language_server/lib/language_server/build.ex +++ b/apps/language_server/lib/language_server/build.ex @@ -113,9 +113,20 @@ defmodule ElixirLS.LanguageServer.Build do Mix.Task.clear() - if Version.match?(System.version(), ">= 1.15.0-dev") do + if Version.match?(System.version(), ">= 1.15.0") do if Logger.Backends.JsonRpc not in :logger.get_handler_ids() do - raise "build without intercepted logger #{inspect(:logger.get_handler_ids())}" + Logger.error("Build without intercepted logger #{inspect(:logger.get_handler_ids())}") + + for handler_id <- :logger.get_handler_ids() do + :ok = :logger.remove_handler(handler_id) + end + + :ok = + :logger.add_handler( + Logger.Backends.JsonRpc, + Logger.Backends.JsonRpc, + Logger.Backends.JsonRpc.handler_config() + ) end end @@ -168,28 +179,31 @@ defmodule ElixirLS.LanguageServer.Build do # store log config logger_config = Application.get_all_env(:logger) - logger_handler_configs = - if Version.match?(System.version(), ">= 1.15.0-dev") do - for handler_id <- :logger.get_handler_ids() do - {:ok, config} = :logger.get_handler_config(handler_id) + try do + Mix.Task.run("loadconfig") + after + # reset log config + Application.put_all_env(logger: logger_config) + + if Version.match?(System.version(), ">= 1.15.0") do + # remove all log handlers and restore our + for handler_id <- :logger.get_handler_ids(), handler_id != Logger.Backends.JsonRpc do :ok = :logger.remove_handler(handler_id) - config end - end - - Mix.Task.run("loadconfig") - # reset log config - Application.put_all_env(logger: logger_config) - - if Version.match?(System.version(), ">= 1.15.0-dev") do - for config <- logger_handler_configs do - :ok = :logger.add_handler(config.id, config.module, config) + if Logger.Backends.JsonRpc not in :logger.get_handler_ids() do + :ok = + :logger.add_handler( + Logger.Backends.JsonRpc, + Logger.Backends.JsonRpc, + Logger.Backends.JsonRpc.handler_config() + ) + end end - end - # make sure ANSI is disabled - Application.put_env(:elixir, :ansi_enabled, false) + # make sure ANSI is disabled + Application.put_env(:elixir, :ansi_enabled, false) + end end {status, diagnostics} @@ -212,7 +226,7 @@ defmodule ElixirLS.LanguageServer.Build do ] opts = - if Version.match?(System.version(), ">= 1.15.0-dev") do + if Version.match?(System.version(), ">= 1.15.0") do opts else opts ++ ["--all-warnings"] diff --git a/apps/language_server/lib/language_server/cli.ex b/apps/language_server/lib/language_server/cli.ex index d732d61dc..6df4bb7d0 100644 --- a/apps/language_server/lib/language_server/cli.ex +++ b/apps/language_server/lib/language_server/cli.ex @@ -10,7 +10,7 @@ defmodule ElixirLS.LanguageServer.CLI do # :logger application is already started # replace console logger with LSP - if Version.match?(System.version(), ">= 1.15.0-dev") do + if Version.match?(System.version(), ">= 1.15.0") do :ok = :logger.remove_handler(:default) :ok = diff --git a/apps/language_server/lib/language_server/experimental/code_mod/format.ex b/apps/language_server/lib/language_server/experimental/code_mod/format.ex index 7b5219862..115f8dba6 100644 --- a/apps/language_server/lib/language_server/experimental/code_mod/format.ex +++ b/apps/language_server/lib/language_server/experimental/code_mod/format.ex @@ -53,7 +53,7 @@ defmodule ElixirLS.LanguageServer.Experimental.CodeMod.Format do true = Code.ensure_loaded?(Mix.Tasks.Format) {formatter_function, options} = - if project_dir && Version.match?(System.version(), ">= 1.15.0-dev") do + if project_dir && Version.match?(System.version(), ">= 1.15.0") do Mix.Tasks.Format.formatter_for_file(path, root: project_dir) else Mix.Tasks.Format.formatter_for_file(path) diff --git a/apps/language_server/lib/language_server/source_file.ex b/apps/language_server/lib/language_server/source_file.ex index 9b7cac83a..7949ae50c 100644 --- a/apps/language_server/lib/language_server/source_file.ex +++ b/apps/language_server/lib/language_server/source_file.ex @@ -236,7 +236,7 @@ defmodule ElixirLS.LanguageServer.SourceFile do try do true = Code.ensure_loaded?(Mix.Tasks.Format) - if project_dir && Version.match?(System.version(), ">= 1.15.0-dev") do + if project_dir && Version.match?(System.version(), ">= 1.15.0") do {:ok, Mix.Tasks.Format.formatter_for_file(path, root: project_dir)} else {:ok, Mix.Tasks.Format.formatter_for_file(path)} diff --git a/apps/language_server/test/server_test.exs b/apps/language_server/test/server_test.exs index 44ac991d3..9f0b089b3 100644 --- a/apps/language_server/test/server_test.exs +++ b/apps/language_server/test/server_test.exs @@ -1038,7 +1038,7 @@ defmodule ElixirLS.LanguageServer.ServerTest do initialize(server) - if Version.match?(System.version(), ">= 1.15.0-dev") do + if Version.match?(System.version(), ">= 1.15.0") do assert_receive notification("textDocument/publishDiagnostics", %{ "uri" => ^error_file, "diagnostics" => [ diff --git a/apps/language_server/test/support/server_test_helpers.ex b/apps/language_server/test/support/server_test_helpers.ex index a03ae4fec..54a0a0c1e 100644 --- a/apps/language_server/test/support/server_test_helpers.ex +++ b/apps/language_server/test/support/server_test_helpers.ex @@ -26,7 +26,7 @@ defmodule ElixirLS.LanguageServer.Test.ServerTestHelpers do def replace_logger(packet_capture) do # :logger application is already started # replace console logger with LSP - if Version.match?(System.version(), ">= 1.15.0-dev") do + if Version.match?(System.version(), ">= 1.15.0") do configs = for handler_id <- :logger.get_handler_ids() do {:ok, config} = :logger.get_handler_config(handler_id)