Skip to content

Commit

Permalink
Return nil on empty response
Browse files Browse the repository at this point in the history
  • Loading branch information
vinistock committed Oct 30, 2024
1 parent b5f4791 commit 5998250
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
31 changes: 27 additions & 4 deletions lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,35 @@ def send_message(request, **params)

sig { overridable.returns(T.nilable(T::Hash[Symbol, T.untyped])) }
def read_response
attempts = 0
content_length = T.let(nil, T.nilable(Integer))

raw_response = @mutex.synchronize do
headers = @stdout.gets("\r\n\r\n")
raise IncompleteMessageError unless headers
while !content_length && attempts < 3
headers = T.let(@stdout.gets("\r\n\r\n"), T.nilable(String))

unless headers
attempts += 1
next
end

content_length = headers[/Content-Length: (\d+)/i, 1].to_i
raise EmptyMessageError if content_length.zero?
content_length = headers[/Content-Length: (\d+)/i, 1].to_i
break if content_length.nonzero?

log_message(
"Ruby LSP Rails expected message headers but got: #{headers}. Trying again...",
type: Constant::MessageType::WARNING,
)
attempts += 1
end

unless content_length
log_message(
"Ruby LSP Rails expected message headers but got: #{headers}. Exceeded retries",
type: Constant::MessageType::WARNING,
)
return
end

@stdout.read(content_length)
end
Expand Down
19 changes: 12 additions & 7 deletions test/ruby_lsp_rails/runner_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,22 @@ class RunnerClientTest < ActiveSupport::TestCase
junk = %{\nputs "1\r\n\r\nhello"}
File.write("test/dummy/config/application.rb", content + junk)

capture_subprocess_io do
outgoing_queue = Thread::Queue.new
client = RunnerClient.create_client(outgoing_queue)
outgoing_queue = Thread::Queue.new

client = RunnerClient.create_client(outgoing_queue)
response = client.model("User")

response = T.must(client.model("User"))
assert(response.key?(:columns))
unless response
log = pop_log_notification(outgoing_queue, RubyLsp::Constant::MessageType::WARNING)
flunk("Model returned nil: #{log.params.message}")
end

begin
assert(T.must(response).key?(:columns))
ensure
T.must(outgoing_queue).close
FileUtils.mv("test/dummy/config/application.rb.bak", "test/dummy/config/application.rb")
end
ensure
FileUtils.mv("test/dummy/config/application.rb.bak", "test/dummy/config/application.rb")
end

test "delegate notification" do
Expand Down

0 comments on commit 5998250

Please sign in to comment.