diff --git a/apps/language_server/lib/language_server/build.ex b/apps/language_server/lib/language_server/build.ex index 72473ef2c..348ffc25e 100644 --- a/apps/language_server/lib/language_server/build.ex +++ b/apps/language_server/lib/language_server/build.ex @@ -16,41 +16,43 @@ defmodule ElixirLS.LanguageServer.Build do case reload_project(mixfile) do {:ok, mixfile_diagnostics} -> - {deps_result, deps_raw_diagnostics} = with_diagnostics([log: true], fn -> - try do - # this call can raise - current_deps = Mix.Dep.load_on_environment([]) - - purge_changed_deps(current_deps, cached_deps) - - if Keyword.get(opts, :fetch_deps?) and current_deps != cached_deps do - fetch_deps(current_deps) + {deps_result, deps_raw_diagnostics} = + with_diagnostics([log: true], fn -> + try do + # this call can raise + current_deps = Mix.Dep.load_on_environment([]) + + purge_changed_deps(current_deps, cached_deps) + + if Keyword.get(opts, :fetch_deps?) and current_deps != cached_deps do + fetch_deps(current_deps) + end + + state = %{ + get: Mix.Project.get(), + # project_file: Mix.Project.project_file(), + config: Mix.Project.config(), + # config_files: Mix.Project.config_files(), + config_mtime: Mix.Project.config_mtime(), + umbrella?: Mix.Project.umbrella?(), + apps_paths: Mix.Project.apps_paths(), + # deps_path: Mix.Project.deps_path(), + # deps_apps: Mix.Project.deps_apps(), + # deps_scms: Mix.Project.deps_scms(), + deps_paths: Mix.Project.deps_paths(), + # build_path: Mix.Project.build_path(), + manifest_path: Mix.Project.manifest_path() + } + + ElixirLS.LanguageServer.MixProject.store(state) + + :ok + catch + kind, err -> + {payload, stacktrace} = Exception.blame(kind, err, __STACKTRACE__) + {:error, kind, payload, stacktrace} end - - state = %{ - get: Mix.Project.get(), - # project_file: Mix.Project.project_file(), - config: Mix.Project.config(), - # config_files: Mix.Project.config_files(), - config_mtime: Mix.Project.config_mtime(), - umbrella?: Mix.Project.umbrella?(), - apps_paths: Mix.Project.apps_paths(), - # deps_path: Mix.Project.deps_path(), - # deps_apps: Mix.Project.deps_apps(), - # deps_scms: Mix.Project.deps_scms(), - deps_paths: Mix.Project.deps_paths(), - # build_path: Mix.Project.build_path(), - manifest_path: Mix.Project.manifest_path() - } - ElixirLS.LanguageServer.MixProject.store(state) - - :ok - catch - kind, err -> - {payload, stacktrace} = Exception.blame(kind, err, __STACKTRACE__) - {:error, kind, payload, stacktrace} - end - end) + end) deps_diagnostics = deps_raw_diagnostics @@ -62,15 +64,32 @@ defmodule ElixirLS.LanguageServer.Build do {status, compile_diagnostics} = run_mix_compile() compile_diagnostics = Diagnostics.normalize(compile_diagnostics, root_path) - Server.build_finished(parent, {status, mixfile_diagnostics ++ deps_diagnostics ++ compile_diagnostics}) + + Server.build_finished( + parent, + {status, mixfile_diagnostics ++ deps_diagnostics ++ compile_diagnostics} + ) + :"mix_compile_#{status}" else - Server.build_finished(parent, {:ok, mixfile_diagnostics ++ deps_diagnostics}) + Server.build_finished( + parent, + {:ok, mixfile_diagnostics ++ deps_diagnostics} + ) + :mix_compile_disabled end + {:error, kind, err, stacktrace} -> # TODO get path from exception message - Server.build_finished(parent, {:error, mixfile_diagnostics ++ deps_diagnostics ++ [Diagnostics.error_to_diagnostic(kind, err, stacktrace, mixfile)]}) + Server.build_finished( + parent, + {:error, + mixfile_diagnostics ++ + deps_diagnostics ++ + [Diagnostics.error_to_diagnostic(kind, err, stacktrace, mixfile)]} + ) + :deps_error end diff --git a/apps/language_server/lib/language_server/dialyzer.ex b/apps/language_server/lib/language_server/dialyzer.ex index 7d9772dde..2646e311a 100644 --- a/apps/language_server/lib/language_server/dialyzer.ex +++ b/apps/language_server/lib/language_server/dialyzer.ex @@ -319,16 +319,24 @@ defmodule ElixirLS.LanguageServer.Dialyzer do {:ok, _} -> Logger.info("Beam file #{inspect(beam_path)} removed") :ok + rm_error -> - Logger.warning("Unable to remove beam file #{inspect(beam_path)}: #{inspect(rm_error)}") + Logger.warning( + "Unable to remove beam file #{inspect(beam_path)}: #{inspect(rm_error)}" + ) + JsonRpc.show_message( :error, - "ElixirLS Dialyzer is unable to process #{inspect(beam_path)}. Please remove it manually") + "ElixirLS Dialyzer is unable to process #{inspect(beam_path)}. Please remove it manually" + ) end + _ -> JsonRpc.show_message( - :error, - "ElixirLS Dialyzer is unable to process one of the beam files. Please remove .elixir_ls/dialyzer* directory manually") + :error, + "ElixirLS Dialyzer is unable to process one of the beam files. Please remove .elixir_ls/dialyzer* directory manually" + ) + :ok end diff --git a/apps/language_server/lib/language_server/server.ex b/apps/language_server/lib/language_server/server.ex index 65161fa7b..bd72e343a 100644 --- a/apps/language_server/lib/language_server/server.ex +++ b/apps/language_server/lib/language_server/server.ex @@ -1863,6 +1863,7 @@ defmodule ElixirLS.LanguageServer.Server do {:ok, cwd} -> if Path.absname(cwd) == Path.absname(project_dir) do mixfile = Path.absname(MixfileHelpers.mix_exs()) + case Build.reload_project(mixfile) do {:ok, _} -> Build.clean(true) @@ -1872,9 +1873,12 @@ defmodule ElixirLS.LanguageServer.Server do :ok end else - message = "Unable to reload project: cwd #{inspect(cwd)} is not project dir #{project_dir}" + message = + "Unable to reload project: cwd #{inspect(cwd)} is not project dir #{project_dir}" + Logger.error(message) - JsonRpc.telemetry( + + JsonRpc.telemetry( "lsp_server_error", %{ "elixir_ls.lsp_process" => inspect(__MODULE__), @@ -1883,17 +1887,19 @@ defmodule ElixirLS.LanguageServer.Server do %{} ) end + {:error, reason} -> message = "Unable to reload project: #{inspect(reason)}" Logger.error(message) + JsonRpc.telemetry( - "lsp_server_error", - %{ - "elixir_ls.lsp_process" => inspect(__MODULE__), - "elixir_ls.lsp_server_error" => message - }, - %{} - ) + "lsp_server_error", + %{ + "elixir_ls.lsp_process" => inspect(__MODULE__), + "elixir_ls.lsp_server_error" => message + }, + %{} + ) end end end diff --git a/apps/language_server/test/providers/formatting_test.exs b/apps/language_server/test/providers/formatting_test.exs index cf45a1da4..98fe913e2 100644 --- a/apps/language_server/test/providers/formatting_test.exs +++ b/apps/language_server/test/providers/formatting_test.exs @@ -561,6 +561,7 @@ defmodule ElixirLS.LanguageServer.Providers.FormattingTest do # build_path: Mix.Project.build_path(), manifest_path: Mix.Project.manifest_path() } + MixProject.store(state) end end