@@ -560,6 +560,19 @@ defmodule ElixirLS.LanguageServer.Server do
560
560
561
561
state
562
562
else
563
+ unless String . valid? ( text ) do
564
+ JsonRpc . telemetry (
565
+ "lsp_server_error" ,
566
+ % {
567
+ "elixir_ls.lsp_process" => inspect ( __MODULE__ ) ,
568
+ "elixir_ls.lsp_server_error" => "File not valid on open:\n #{ inspect ( text ) } "
569
+ } ,
570
+ % { }
571
+ )
572
+
573
+ Logger . error ( "File not valid on open:\n #{ inspect ( text ) } " )
574
+ end
575
+
563
576
source_file = % SourceFile { text: text , version: version }
564
577
565
578
state = put_in ( state . source_files [ uri ] , source_file )
@@ -603,8 +616,27 @@ defmodule ElixirLS.LanguageServer.Server do
603
616
else
604
617
state =
605
618
update_in ( state . source_files [ uri ] , fn source_file ->
606
- % SourceFile { source_file | version: version , dirty?: true }
607
- |> SourceFile . apply_content_changes ( content_changes )
619
+ file =
620
+ % SourceFile { source_file | version: version , dirty?: true }
621
+ |> SourceFile . apply_content_changes ( content_changes )
622
+
623
+ unless String . valid? ( file . text ) do
624
+ JsonRpc . telemetry (
625
+ "lsp_server_error" ,
626
+ % {
627
+ "elixir_ls.lsp_process" => inspect ( __MODULE__ ) ,
628
+ "elixir_ls.lsp_server_error" =>
629
+ "File not valid after: #{ inspect ( content_changes ) } text was:\n #{ inspect ( source_file . text ) } "
630
+ } ,
631
+ % { }
632
+ )
633
+
634
+ Logger . error (
635
+ "File not valid after: #{ inspect ( content_changes ) } text was:\n #{ inspect ( source_file . text ) } "
636
+ )
637
+ end
638
+
639
+ file
608
640
end )
609
641
610
642
# trigger parse with debounce
@@ -1446,7 +1478,12 @@ defmodule ElixirLS.LanguageServer.Server do
1446
1478
defp safely_read_file ( file ) do
1447
1479
case File . read ( file ) do
1448
1480
{ :ok , text } ->
1449
- text
1481
+ if String . valid? ( text ) do
1482
+ text
1483
+ else
1484
+ Logger . warning ( "Invalid encoding in #{ file } " )
1485
+ nil
1486
+ end
1450
1487
1451
1488
{ :error , reason } ->
1452
1489
if reason != :enoent do
0 commit comments