Skip to content

Commit

Permalink
Use stacktrace in mix compiled diagnostics on elixir 1.15
Browse files Browse the repository at this point in the history
remove special handling for token missing error
the hint line is extracted elsewhere and populates related info
  • Loading branch information
lukaszsamson committed Dec 22, 2023
1 parent ad3473c commit 98e4228
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 50 deletions.
38 changes: 11 additions & 27 deletions apps/language_server/lib/language_server/diagnostics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,20 @@ defmodule ElixirLS.LanguageServer.Diagnostics do

%__MODULE__{normalized | message: message, file: file, position: position}
else
{type, file, position, stacktrace} =
{_type, file, position, stacktrace} =
extract_message_info(diagnostic.message, root_path)

{file, position} =
get_file_and_position_with_stacktrace_fallback(
{file || diagnostic.file, position || diagnostic.position},
Map.get(diagnostic, :stacktrace, []),
root_path,
mixfile
)

normalized
|> maybe_update_file(file, mixfile)
|> maybe_update_position(type, position, stacktrace)
|> maybe_update_position(position, stacktrace)
end
end

Expand Down Expand Up @@ -85,21 +93,7 @@ defmodule ElixirLS.LanguageServer.Diagnostics do
end
end

defp maybe_update_position(diagnostic, "TokenMissingError", position, stacktrace) do
case extract_line_from_missing_hint(diagnostic.message) do
line when is_integer(line) and line > 0 ->
%{diagnostic | position: line}

_ ->
do_maybe_update_position(diagnostic, position, stacktrace)
end
end

defp maybe_update_position(diagnostic, _type, position, stacktrace) do
do_maybe_update_position(diagnostic, position, stacktrace)
end

defp do_maybe_update_position(diagnostic, position, stacktrace) do
defp maybe_update_position(diagnostic, position, stacktrace) do
cond do
position != nil ->
%{diagnostic | position: position}
Expand Down Expand Up @@ -191,16 +185,6 @@ defmodule ElixirLS.LanguageServer.Diagnostics do
false
end

defp extract_line_from_missing_hint(message) do
case Regex.run(
~r/starting at line (\d+)\)/u,
message
) do
[_, line] -> String.to_integer(line)
_ -> nil
end
end

defp extract_line_from_stacktrace(file, stacktrace) do
Enum.find_value(stacktrace, fn stack_item ->
with [_, _, file_relative, line] <-
Expand Down
23 changes: 0 additions & 23 deletions apps/language_server/test/diagnostics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,6 @@ defmodule ElixirLS.LanguageServer.DiagnosticsTest do
assert diagnostic.position == 13
end

test "if position is nil and error is TokenMissingError, try to retrieve from the hint" do
root_path = Path.join(__DIR__, "fixtures/token_missing_error")
file = Path.join(root_path, "lib/has_error.ex")
position = nil

message = """
** (TokenMissingError) lib/has_error.ex:16:1: missing terminator: end (for "do" starting at line 1)
HINT: it looks like the "do" on line 6 does not have a matching "end"
(elixir 1.12.1) lib/kernel/parallel_compiler.ex:319: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
"""

diagnostic =
build_diagnostic(message, file, position)
|> Diagnostics.from_mix_task_compiler_diagnostic(
Path.join(root_path, "mix.exs"),
root_path
)

assert diagnostic.position == 1
end

defp build_diagnostic(message, file, position) do
%Mix.Task.Compiler.Diagnostic{
compiler_name: "Elixir",
Expand Down

0 comments on commit 98e4228

Please sign in to comment.