Skip to content

Commit 075435b

Browse files
committed
fall back to mixfile if diagnostic without file emitted
1 parent 88b8208 commit 075435b

File tree

1 file changed

+13
-28
lines changed
  • apps/language_server/lib/language_server

1 file changed

+13
-28
lines changed

apps/language_server/lib/language_server/server.ex

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,6 +1585,7 @@ defmodule ElixirLS.LanguageServer.Server do
15851585
defp publish_diagnostics(
15861586
state = %__MODULE__{
15871587
project_dir: project_dir,
1588+
mix_project?: mix_project?,
15881589
source_files: source_files,
15891590
last_published_diagnostics_uris: last_published_diagnostics_uris
15901591
}
@@ -1596,43 +1597,27 @@ defmodule ElixirLS.LanguageServer.Server do
15961597

15971598
uris_from_parser_diagnostics = Map.keys(state.parser_diagnostics)
15981599

1599-
filter_diagnostics_with_known_location = fn
1600-
%Diagnostics{file: file} when is_binary(file) ->
1601-
file != "nofile"
1602-
1603-
_ ->
1604-
false
1605-
end
1606-
16071600
valid_build_and_dialyzer_diagnostics_by_uri =
16081601
(state.build_diagnostics ++ state.dialyzer_diagnostics)
1609-
|> Enum.filter(filter_diagnostics_with_known_location)
1602+
|> Enum.map(fn %Diagnostics{file: file} = diagnostic ->
1603+
if is_binary(file) or (is_list(file) and to_string(file) != "nofile") do
1604+
diagnostic
1605+
else
1606+
# diagnostics without file are meaningless in LSP, try to point to mixfile instead
1607+
if project_dir != nil and mix_project? do
1608+
file = Path.join(project_dir, MixfileHelpers.mix_exs())
1609+
%Diagnostics{diagnostic | file: file, source: file, position: 0}
1610+
end
1611+
end
1612+
end)
1613+
|> Enum.reject(&is_nil/1)
16101614
|> Enum.group_by(fn
16111615
%Diagnostics{file: file} -> SourceFile.Path.to_uri(file, project_dir)
16121616
end)
16131617

16141618
uris_from_build_and_dialyzer_diagnostics =
16151619
Map.keys(valid_build_and_dialyzer_diagnostics_by_uri)
16161620

1617-
invalid_diagnostics =
1618-
(state.build_diagnostics ++ state.dialyzer_diagnostics)
1619-
|> Enum.reject(filter_diagnostics_with_known_location)
1620-
1621-
# TODO remove when we are sure diagnostic code is correct
1622-
if invalid_diagnostics != [] do
1623-
Logger.error("Invalid diagnostic with nil file: #{inspect(hd(invalid_diagnostics))}")
1624-
1625-
JsonRpc.telemetry(
1626-
"lsp_server_error",
1627-
%{
1628-
"elixir_ls.lsp_process" => inspect(__MODULE__),
1629-
"elixir_ls.lsp_server_error" =>
1630-
"Invalid diagnostic: #{inspect(hd(invalid_diagnostics))}"
1631-
},
1632-
%{}
1633-
)
1634-
end
1635-
16361621
uris_from_open_files = Map.keys(source_files)
16371622

16381623
uris_to_publish_diagnostics =

0 commit comments

Comments
 (0)