Skip to content

Commit 7caeea4

Browse files
committed
Do not raise if response is empty
1 parent 77650f0 commit 7caeea4

File tree

4 files changed

+62
-32
lines changed

4 files changed

+62
-32
lines changed

lib/ruby_lsp/ruby_lsp_rails/runner_client.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,27 @@ def initialize(outgoing_queue)
7070
# https://github.com/Shopify/ruby-lsp-rails/issues/348
7171
end
7272

73+
log_message("Ruby LSP Rails booting server")
74+
7375
stdin, stdout, stderr, wait_thread = Bundler.with_original_env do
7476
Open3.popen3("bundle", "exec", "rails", "runner", "#{__dir__}/server.rb", "start")
7577
end
7678

7779
@stdin = T.let(stdin, IO)
7880
@stdout = T.let(stdout, IO)
7981
@stderr = T.let(stderr, IO)
82+
@stdin.sync = true
83+
@stdout.sync = true
84+
@stderr.sync = true
8085
@wait_thread = T.let(wait_thread, Process::Waiter)
8186

8287
# We set binmode for Windows compatibility
8388
@stdin.binmode
8489
@stdout.binmode
8590
@stderr.binmode
8691

87-
log_message("Ruby LSP Rails booting server")
88-
count = 0
89-
90-
begin
91-
count += 1
92-
initialize_response = T.must(read_response)
93-
@rails_root = T.let(initialize_response[:root], String)
94-
rescue EmptyMessageError
95-
log_message("Ruby LSP Rails is retrying initialize (#{count})")
96-
retry if count < MAX_RETRIES
97-
end
98-
92+
initialize_response = T.must(read_response)
93+
@rails_root = T.let(initialize_response[:root], String)
9994
log_message("Finished booting Ruby LSP Rails server")
10095

10196
unless ENV["RAILS_ENV"] == "test"
@@ -274,11 +269,9 @@ def send_message(request, **params)
274269
sig { overridable.returns(T.nilable(T::Hash[Symbol, T.untyped])) }
275270
def read_response
276271
raw_response = @mutex.synchronize do
277-
headers = @stdout.gets("\r\n\r\n")
278-
raise IncompleteMessageError unless headers
279-
280-
content_length = headers[/Content-Length: (\d+)/i, 1].to_i
281-
raise EmptyMessageError if content_length.zero?
272+
content_length = read_content_length
273+
content_length = read_content_length unless content_length
274+
raise EmptyMessageError unless content_length
282275

283276
@stdout.read(content_length)
284277
end
@@ -311,6 +304,17 @@ def log_message(message, type: RubyLsp::Constant::MessageType::LOG)
311304

312305
@outgoing_queue << RubyLsp::Notification.window_log_message(message, type: type)
313306
end
307+
308+
sig { returns(T.nilable(Integer)) }
309+
def read_content_length
310+
headers = @stdout.gets("\r\n\r\n")
311+
return unless headers
312+
313+
length = headers[/Content-Length: (\d+)/i, 1]
314+
return unless length
315+
316+
length.to_i
317+
end
314318
end
315319

316320
class NullClient < RunnerClient

test/ruby_lsp_rails/launch_test.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
require "test_helper"
5+
6+
module RubyLsp
7+
module Rails
8+
class LaunchTest < ActiveSupport::TestCase
9+
test "launching the client succeeds" do
10+
outgoing_queue = Thread::Queue.new
11+
12+
client = RunnerClient.create_client(outgoing_queue)
13+
refute_instance_of(NullClient, client)
14+
15+
first = pop_log_notification(outgoing_queue, Constant::MessageType::LOG)
16+
assert_equal("Ruby LSP Rails booting server", first.params.message)
17+
18+
second = pop_log_notification(outgoing_queue, Constant::MessageType::LOG)
19+
assert_match("Finished booting Ruby LSP Rails server", second.params.message)
20+
21+
client.shutdown
22+
assert_predicate(client, :stopped?)
23+
outgoing_queue.close
24+
end
25+
end
26+
end
27+
end

test/ruby_lsp_rails/runner_client_test.rb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,16 @@ class RunnerClientTest < ActiveSupport::TestCase
8787
junk = %{\nputs "1\r\n\r\nhello"}
8888
File.write("test/dummy/config/application.rb", content + junk)
8989

90-
capture_subprocess_io do
91-
outgoing_queue = Thread::Queue.new
92-
client = RunnerClient.create_client(outgoing_queue)
90+
outgoing_queue = Thread::Queue.new
91+
client = RunnerClient.create_client(outgoing_queue)
92+
response = client.model("User")
9393

94-
response = T.must(client.model("User"))
95-
assert(response.key?(:columns))
94+
begin
95+
assert(T.must(response).key?(:columns))
9696
ensure
9797
T.must(outgoing_queue).close
98+
FileUtils.mv("test/dummy/config/application.rb.bak", "test/dummy/config/application.rb")
9899
end
99-
ensure
100-
FileUtils.mv("test/dummy/config/application.rb.bak", "test/dummy/config/application.rb")
101100
end
102101

103102
test "delegate notification" do
@@ -155,14 +154,6 @@ def execute(request, params)
155154
ensure
156155
FileUtils.rm("server_addon.rb")
157156
end
158-
159-
private
160-
161-
def pop_log_notification(message_queue, type)
162-
log = message_queue.pop
163-
log = message_queue.pop until log.params.type == type
164-
log
165-
end
166157
end
167158

168159
class NullClientTest < ActiveSupport::TestCase

test/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,13 @@ def pop_result(server)
4848
)
4949
T.cast(result, RubyLsp::Result)
5050
end
51+
52+
def pop_log_notification(message_queue, type)
53+
log = message_queue.pop
54+
return log if log.params.type == type
55+
56+
log = message_queue.pop until log.params.type == type
57+
log
58+
end
5159
end
5260
end

0 commit comments

Comments
 (0)