Skip to content

Support migration output messages containing multibyte characters #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ruby_lsp/ruby_lsp_rails/runner_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def send_message(request, **params)
json = message.to_json

@mutex.synchronize do
@stdin.write("Content-Length: #{json.length}\r\n\r\n", json)
@stdin.write("Content-Length: #{json.bytesize}\r\n\r\n", json)
end
rescue Errno::EPIPE
# The server connection died
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module Common
# Write a message to the client. Can be used for sending notifications to the editor
def send_message(message)
json_message = message.to_json
@stdout.write("Content-Length: #{json_message.length}\r\n\r\n#{json_message}")
@stdout.write("Content-Length: #{json_message.bytesize}\r\n\r\n#{json_message}")
end

# Log a message to the editor's output panel
Expand Down
12 changes: 12 additions & 0 deletions test/ruby_lsp_rails/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ def resolve_route_info(requirements)
)
end

test "send_message uses bytesize for content length with ASCII characters" do
@server.send(:send_message, { test: "hello" })
assert_equal "Content-Length: 16\r\n\r\n{\"test\":\"hello\"}", @stdout.string
end

test "send_message uses bytesize for content length with multibyte characters" do
@server.send(:send_message, { test: "こんにちは" }) # Japanese "hello"
expected = "Content-Length: 26\r\n\r\n"
expected += { test: "こんにちは" }.to_json.force_encoding(Encoding::ASCII_8BIT)
assert_equal expected, @stdout.string
end

private

def response
Expand Down
Loading