Skip to content

Commit 955dc27

Browse files
authored
Merge pull request #542 from missive/bugfix/content-length-multibyte
Support migration output messages containing multibyte characters
2 parents e28e268 + 94f0024 commit 955dc27

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

lib/ruby_lsp/ruby_lsp_rails/runner_client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def send_message(request, **params)
281281
json = message.to_json
282282

283283
@mutex.synchronize do
284-
@stdin.write("Content-Length: #{json.length}\r\n\r\n", json)
284+
@stdin.write("Content-Length: #{json.bytesize}\r\n\r\n", json)
285285
end
286286
rescue Errno::EPIPE
287287
# The server connection died

lib/ruby_lsp/ruby_lsp_rails/server.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module Common
1313
# Write a message to the client. Can be used for sending notifications to the editor
1414
def send_message(message)
1515
json_message = message.to_json
16-
@stdout.write("Content-Length: #{json_message.length}\r\n\r\n#{json_message}")
16+
@stdout.write("Content-Length: #{json_message.bytesize}\r\n\r\n#{json_message}")
1717
end
1818

1919
# Log a message to the editor's output panel

test/ruby_lsp_rails/server_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ def resolve_route_info(requirements)
217217
)
218218
end
219219

220+
test "send_message uses bytesize for content length with ASCII characters" do
221+
@server.send(:send_message, { test: "hello" })
222+
assert_equal "Content-Length: 16\r\n\r\n{\"test\":\"hello\"}", @stdout.string
223+
end
224+
225+
test "send_message uses bytesize for content length with multibyte characters" do
226+
@server.send(:send_message, { test: "こんにちは" }) # Japanese "hello"
227+
expected = "Content-Length: 26\r\n\r\n"
228+
expected += { test: "こんにちは" }.to_json.force_encoding(Encoding::ASCII_8BIT)
229+
assert_equal expected, @stdout.string
230+
end
231+
220232
private
221233

222234
def response

0 commit comments

Comments
 (0)