diff --git a/lib/ruby_lsp/server.rb b/lib/ruby_lsp/server.rb index 52cd25d32..04eb5f342 100644 --- a/lib/ruby_lsp/server.rb +++ b/lib/ruby_lsp/server.rb @@ -1036,6 +1036,10 @@ def handle_ruby_file_change(index, file_path, change_type) when Constant::FileChangeType::DELETED index.delete(uri) end + rescue Errno::ENOENT + # If a file is created and then delete immediately afterwards, we will process the created notification before we + # receive the deleted one, but the file no longer exists. This may happen when running a test suite that creates + # and deletes files automatically. end sig { params(uri: URI::Generic).void } diff --git a/test/server_test.rb b/test/server_test.rb index d4781e595..531990d3d 100644 --- a/test/server_test.rb +++ b/test/server_test.rb @@ -486,6 +486,27 @@ def test_changed_file_only_indexes_ruby FileUtils.rm(T.must(path)) end + def test_did_change_watched_files_does_not_fail_for_non_existing_files + @server.process_message({ + method: "workspace/didChangeWatchedFiles", + params: { + changes: [ + { + uri: URI::Generic.from_path(path: File.join(Dir.pwd, "lib", "non_existing.rb")), + type: RubyLsp::Constant::FileChangeType::CREATED, + }, + ], + }, + }) + + assert_raises(Timeout::Error) do + Timeout.timeout(0.5) do + notification = find_message(RubyLsp::Notification, "window/logMessage") + flunk(notification.params.message) + end + end + end + def test_workspace_addons create_test_addons @server.load_addons