Skip to content

Commit

Permalink
Avoid directly calling version to avoid Sorbet error (#2663)
Browse files Browse the repository at this point in the history
This will avoid breaking add-ons that don't define a `version` method and
doesn't require `ruby-lsp` as a dependency when displaying the add-ons in the UI.
  • Loading branch information
st0012 authored Oct 3, 2024
1 parent 9154bfc commit 791cd00
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
11 changes: 10 additions & 1 deletion lib/ruby_lsp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,16 @@ def process_message(message)
id: message[:id],
response:
Addon.addons.map do |addon|
{ name: addon.name, version: addon.version, errored: addon.error? }
version_method = addon.method(:version)

# If the add-on doesn't define a `version` method, we'd be calling the abstract method defined by
# Sorbet, which would raise an error.
# Therefore, we only call the method if it's defined by the add-on itself
if version_method.owner != Addon
version = addon.version
end

{ name: addon.name, version: version, errored: addon.error? }
end,
),
)
Expand Down
7 changes: 2 additions & 5 deletions test/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@ def test_workspace_addons
refute(addons_info[0][:errored])

assert_equal("Bar", addons_info[1][:name])
assert_equal("0.2.0", addons_info[1][:version])
# It doesn't define a `version` method
assert_nil(addons_info[1][:version])
assert(addons_info[1][:errored])
ensure
RubyLsp::Addon.addons.clear
Expand Down Expand Up @@ -722,10 +723,6 @@ def name
end

def deactivate; end

def version
"0.2.0"
end
end
end

Expand Down

0 comments on commit 791cd00

Please sign in to comment.