Skip to content

WorkspaceSymbol request are returning invalid response on method alias, unresolved method alias and constants and class variables #3781

@pkondzior

Description

@pkondzior

Description

Ruby LSP Information

Ruby LSP WorkspaceSymbol Responses are not generated according to spec if index contains unresolved alias methods, unresolved constant aliases or class variables. Additionally resolvable methods and constants are not being processed either in resolved method or alias form.

The spec for SymbolInformation stands that kind should be represented by one of SybolKind enum variants while RubyLSP. If we are going to look into how these are encoded by Ruby-LSP we can notice that neither RubyIndexer::Entry::ConstantAlias, RubyIndexer::Entry::MethodAlias nor RubyIndexer::Entry::ClassVariable have their when branch on the case. This leads to nil results. Additionally due to lazy nature of unresolved entries fuzzy_search is not able to work on RubyIndexer::Entry::ConstantAlias nor RubyIndexer::Entry::MethodAlias from index.

Reproduction steps

  1. Start the Ruby LSP using a Zed Eeditor
  2. Open a Ruby file with following content
OK = 'OK'
class Foo
  BOK = OK
  BAD = AD
  def test
  end
  alias whatever test
  alias_method :bar, :to_a
  alias_method "baz", "to_a"
  @@test = '123'
end
  1. Go to New Agent Thread and try to open type @symbol
  2. Observer nothing shows up and You have data did not match any variant of untagged enum WorkspaceSymbolResponse exception in the logs

Alternatively you can run this following code in IRB

Code snippet or error message

require 'ruby-lsp'
require "ruby_lsp/internal"
@global_state = RubyLsp::GlobalState.new
# @global_state.stubs(:has_type_checker).returns(false)
@index = @global_state.index
@index.index_single(URI::Generic.from_path(path: "/fake.rb"), <<~RUBY)
  OK = 'OK'
  class Foo
    BOK = OK
    BAD = AD
    def test
    end
    alias whatever test
    alias_method :bar, :to_a
    alias_method "baz", "to_a"
    @@test = '123'
  end
RUBY

RubyLsp::Requests::WorkspaceSymbol.new(@global_state, nil).perform

Which should result in following response:

[...,
#<LanguageServer::Protocol::Interface::WorkspaceSymbol:0x000000010646ee50
  @attributes=
   {:name=>"@@test",
    :kind=>nil,
    :containerName=>"",
    :location=>
     #<LanguageServer::Protocol::Interface::Location:0x000000010646ef40
      @attributes=
       {:uri=>"file:///fake.rb",
        :range=>
         #<LanguageServer::Protocol::Interface::Range:0x000000010646f058
          @attributes=
           {:start=>#<LanguageServer::Protocol::Interface::Position:0x000000010646f530 @attributes={:line=>9, :character=>2}>,
            :end=>#<LanguageServer::Protocol::Interface::Position:0x000000010646f0f8 @attributes={:line=>9, :character=>8}>}>}>}>]

You can notice that kind for class variable, constant alias and resolvable method alias is nil, additionally method aliases which are not resolvable are also included in the result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions