elixir-lsp / elixir-ls

A frontend-independent IDE "smartness" server for Elixir. Implements the "Language Server Protocol" standard and provides debugger support via the "Debug Adapter Protocol"
https://elixir-lsp.github.io/elixir-ls/
Apache License 2.0
1.5k stars 196 forks source link

Crashing String.slice after editing file/module endings #1002

Closed florianb closed 1 year ago

florianb commented 1 year ago

Environment

Current behavior

The LS seems to crash when adding/removing the last end in this module:

Change

defmodule T do
  defdelegate, to:
  def t(), do: :smth e
  defp u do

  end
end

to

defmodule T do
  defdelegate, to:
  def t(), do: :smth e
  defp u do

  end

It might be, that the appearance/disappearance of the last line/last "newline" plays a role here. I noticed my Editor currently seems to strip the last line and this happens a lot when quickly edditing in the last section of the file.

Log

[Error - 7:42:30 AM] GenServer ElixirLS.LanguageServer.Server terminating
** (FunctionClauseError) no function clause matching in String.slice/2
    (elixir 1.15.4) lib/string.ex:2241: String.slice(nil, 0..0)
    (language_server 0.17.0) lib/language_server/source_file.ex:316: ElixirLS.LanguageServer.SourceFile.elixir_character_to_lsp/2
    (language_server 0.17.0) lib/language_server/diagnostics.ex:324: ElixirLS.LanguageServer.Diagnostics.range/2
    (language_server 0.17.0) lib/language_server/diagnostics.ex:210: anonymous fn/2 in ElixirLS.LanguageServer.Diagnostics.publish_file_diagnostics/3
    (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (elixir 1.15.4) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (language_server 0.17.0) lib/language_server/diagnostics.ex:192: ElixirLS.LanguageServer.Diagnostics.publish_file_diagnostics/3
    (language_server 0.17.0) lib/language_server/server.ex:1187: anonymous fn/4 in ElixirLS.LanguageServer.Server.publish_diagnostics/3
lukaszsamson commented 1 year ago

That's most likely race conditions between publishing diagnostics and source file changes. This line assumes the source file has the line with error https://github.com/elixir-lsp/elixir-ls/blob/42f16b83708bd2068f8c54b3631b53edc99d7525/apps/language_server/lib/language_server/diagnostics.ex#L322

florianb commented 1 year ago

Shouldn't the file already being read at this point, how can this differ?

lukaszsamson commented 1 year ago

Async parsing and async text document synchronisation. When the diagnostic is being published the line is not there anymore and we need the line to convert UTF8 code points to UTF16. I know what needs fixing here :)

florianb commented 1 year ago

Thank you very much for your work! 🙏