@@ -1585,6 +1585,7 @@ defmodule ElixirLS.LanguageServer.Server do
1585
1585
defp publish_diagnostics (
1586
1586
state = % __MODULE__ {
1587
1587
project_dir: project_dir ,
1588
+ mix_project?: mix_project? ,
1588
1589
source_files: source_files ,
1589
1590
last_published_diagnostics_uris: last_published_diagnostics_uris
1590
1591
}
@@ -1596,43 +1597,27 @@ defmodule ElixirLS.LanguageServer.Server do
1596
1597
1597
1598
uris_from_parser_diagnostics = Map . keys ( state . parser_diagnostics )
1598
1599
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
-
1607
1600
valid_build_and_dialyzer_diagnostics_by_uri =
1608
1601
( 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 )
1610
1614
|> Enum . group_by ( fn
1611
1615
% Diagnostics { file: file } -> SourceFile.Path . to_uri ( file , project_dir )
1612
1616
end )
1613
1617
1614
1618
uris_from_build_and_dialyzer_diagnostics =
1615
1619
Map . keys ( valid_build_and_dialyzer_diagnostics_by_uri )
1616
1620
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
-
1636
1621
uris_from_open_files = Map . keys ( source_files )
1637
1622
1638
1623
uris_to_publish_diagnostics =
0 commit comments