Skip to content

Commit

Permalink
Diagnostics refactor (#1040)
Browse files Browse the repository at this point in the history
* handle uppercase hint

* extract line in one more case

* wip

* wip

* build release assets with recent version

* fix tests on < 1.16

* format

* improve rendering of TokenMissingError for heredocs

* fix warning

* format

* deduplicate rendered messages

* try to get line from stacktrace if position unknown

* add tests
  • Loading branch information
lukaszsamson authored Dec 21, 2023
1 parent b30939c commit f51019e
Show file tree
Hide file tree
Showing 10 changed files with 1,133 additions and 402 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release-asset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ jobs:
- name: Set up BEAM
uses: erlef/setup-beam@v1
with:
elixir-version: 1.14.x
otp-version: 25.x
elixir-version: 1.15.x
otp-version: 26.x

- name: Install dependencies
run: mix deps.get
Expand Down
25 changes: 15 additions & 10 deletions apps/debug_adapter/lib/debug_adapter/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1101,15 +1101,16 @@ defmodule ElixirLS.DebugAdapter.Server do
"expensive" => false
}

args_scope = if frame.args != :undefined do
%{
"name" => "arguments",
"variablesReference" => args_id,
"namedVariables" => 0,
"indexedVariables" => Enum.count(frame.args),
"expensive" => false
}
end
args_scope =
if frame.args != :undefined do
%{
"name" => "arguments",
"variablesReference" => args_id,
"namedVariables" => 0,
"indexedVariables" => Enum.count(frame.args),
"expensive" => false
}
end

messages_scope = %{
"name" => "messages",
Expand All @@ -1129,7 +1130,11 @@ defmodule ElixirLS.DebugAdapter.Server do

scopes =
[vars_scope, versioned_vars_scope, process_info_scope]
|> Kernel.++(if frame.args != :undefined and Enum.count(frame.args) > 0, do: [args_scope], else: [])
|> Kernel.++(
if frame.args != :undefined and Enum.count(frame.args) > 0,
do: [args_scope],
else: []
)
|> Kernel.++(if Enum.count(frame.messages) > 0, do: [messages_scope], else: [])

{state, scopes}
Expand Down
6 changes: 2 additions & 4 deletions apps/debug_adapter/test/debugger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2322,8 +2322,7 @@ defmodule ElixirLS.DebugAdapter.ServerTest do
assert Proto in :int.interpreted()

assert [
{{Proto, 2},
[:active, :enable, :null, {BreakpointCondition, :check_0}]}
{{Proto, 2}, [:active, :enable, :null, {BreakpointCondition, :check_0}]}
] = :int.all_breaks(Proto)

assert %{{Proto, :go, 1} => [2]} = :sys.get_state(server).function_breakpoints
Expand Down Expand Up @@ -2434,8 +2433,7 @@ defmodule ElixirLS.DebugAdapter.ServerTest do
assert Proto.List in :int.interpreted()

assert [
{{Proto.List, 7},
[:active, :enable, :null, {BreakpointCondition, :check_0}]}
{{Proto.List, 7}, [:active, :enable, :null, {BreakpointCondition, :check_0}]}
] = :int.all_breaks(Proto.List)

assert %{{Proto.List, :go, 1} => [7]} = :sys.get_state(server).function_breakpoints
Expand Down
75 changes: 58 additions & 17 deletions apps/language_server/lib/language_server/build.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ defmodule ElixirLS.LanguageServer.Build do

deps_diagnostics =
deps_raw_diagnostics
|> Enum.map(&Diagnostics.code_diagnostic/1)
|> Enum.map(&Diagnostics.from_code_diagnostic(&1, mixfile, root_path))

case deps_result do
:ok ->
Expand All @@ -71,7 +71,10 @@ defmodule ElixirLS.LanguageServer.Build do
run_mix_compile(Keyword.get(opts, :force?, false))

compile_diagnostics =
Diagnostics.normalize(compile_diagnostics, root_path, mixfile)
compile_diagnostics
|> Enum.map(
&Diagnostics.from_mix_task_compiler_diagnostic(&1, mixfile, root_path)
)

Server.build_finished(
parent,
Expand All @@ -95,7 +98,7 @@ defmodule ElixirLS.LanguageServer.Build do
mixfile_diagnostics ++
deps_diagnostics ++
[
Diagnostics.error_to_diagnostic(
Diagnostics.from_error(
kind,
err,
stacktrace,
Expand Down Expand Up @@ -270,16 +273,54 @@ defmodule ElixirLS.LanguageServer.Build do

# We can get diagnostics if Mixfile fails to load
{mixfile_status, mixfile_diagnostics} =
case Kernel.ParallelCompiler.compile([mixfile]) do
{:ok, _, warnings} ->
{:ok, Enum.map(warnings, &Diagnostics.mixfile_diagnostic(&1, :warning))}

{:error, errors, warnings} ->
{
:error,
Enum.map(warnings, &Diagnostics.mixfile_diagnostic(&1, :warning)) ++
Enum.map(errors, &Diagnostics.mixfile_diagnostic(&1, :error))
}
if Version.match?(System.version(), ">= 1.15.3") do
{result, raw_diagnostics} =
with_diagnostics([log: true], fn ->
try do
Code.compile_file(mixfile)
:ok
catch
kind, err ->
{payload, stacktrace} = Exception.blame(kind, err, __STACKTRACE__)
{:error, kind, payload, stacktrace}
end
end)

diagnostics =
raw_diagnostics
|> Enum.map(&Diagnostics.from_code_diagnostic(&1, mixfile, root_path))

case result do
:ok ->
{:ok, diagnostics}

{:error, kind, err, stacktrace} ->
{:error,
diagnostics ++
[Diagnostics.from_error(kind, err, stacktrace, mixfile, root_path)]}
end
else
case Kernel.ParallelCompiler.compile([mixfile]) do
{:ok, _, warnings} ->
{:ok,
Enum.map(
warnings,
&Diagnostics.from_kernel_parallel_compiler_tuple(&1, :warning, mixfile)
)}

{:error, errors, warnings} ->
{
:error,
Enum.map(
warnings,
&Diagnostics.from_kernel_parallel_compiler_tuple(&1, :warning, mixfile)
) ++
Enum.map(
errors,
&Diagnostics.from_kernel_parallel_compiler_tuple(&1, :error, mixfile)
)
}
end
end

# restore warnings
Expand Down Expand Up @@ -325,18 +366,18 @@ defmodule ElixirLS.LanguageServer.Build do
end
end)

config_path = SourceFile.Path.absname(Mix.Project.config()[:config_path], root_path)

config_diagnostics =
config_raw_diagnostics
|> Enum.map(&Diagnostics.code_diagnostic/1)
|> Enum.map(&Diagnostics.from_code_diagnostic(&1, config_path, root_path))

case config_result do
{:error, kind, err, stacktrace} ->
config_path = Mix.Project.config()[:config_path]

{:error,
mixfile_diagnostics ++
config_diagnostics ++
[Diagnostics.error_to_diagnostic(kind, err, stacktrace, config_path, root_path)]}
[Diagnostics.from_error(kind, err, stacktrace, config_path, root_path)]}

:ok ->
{:ok, mixfile_diagnostics ++ config_diagnostics}
Expand Down
Loading

0 comments on commit f51019e

Please sign in to comment.