Skip to content

Commit

Permalink
Avoid crash while reloading non-existing file (#1308)
Browse files Browse the repository at this point in the history
It can happen during a rebase operation that a file appears/disappears
multiple times in a very short timeframe. Specifically, since
`didChangeWatchedFiles` notifications are asynchronous, it can happen
that a file is deleted before a notification is processed by the
server. In such a case, the server should simply ignore the
notification, since a new one will arrive.

Also, there is no need to deeply index files during a rebase, so let's
convert to a shallow indexing.

We mark the source of the file as 'app', which has the only side
effect of indexing references.
  • Loading branch information
robertoaloi authored May 23, 2022
1 parent 97721de commit 2347a09
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions apps/els_lsp/src/els_indexing.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
maybe_start/0,
ensure_deeply_indexed/1,
shallow_index/2,
shallow_index/3,
deep_index/1,
remove/1
]).
Expand Down
23 changes: 20 additions & 3 deletions apps/els_lsp/src/els_text_synchronization.erl
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,26 @@ handle_file_change(Uri, Type) when Type =:= ?FILE_CHANGE_TYPE_DELETED ->

-spec reload_from_disk(uri()) -> ok.
reload_from_disk(Uri) ->
{ok, Text} = file:read_file(els_uri:path(Uri)),
{ok, Document} = els_utils:lookup_document(Uri),
els_indexing:deep_index(Document#{text => Text}),
Path = els_uri:path(Uri),
case file:read_file(Path) of
{ok, Text} ->
els_indexing:shallow_index(Uri, Text, app);
{error, Error} ->
%% File is not accessible. This can happen, for example,
%% during a rebase operation, when the file "appears and
%% disappears" multiple times in a very short
%% timeframe. Just log the fact as a warning, but keep
%% going. It should be possible to buffer
%% 'didChangeWatchedFiles' requests, but it's not a big
%% deal.
?LOG_WARNING(
"Error while reloading from disk. Ignoring. "
"[uri=~p] [error=~p]",
[
Uri, Error
]
)
end,
ok.

-spec background_index(els_dt_document:item()) -> {ok, pid()}.
Expand Down

0 comments on commit 2347a09

Please sign in to comment.