dcermak / rpm-spec-language-server

Language Server for RPM spec files
GNU General Public License v2.0
21 stars 4 forks source link

Modifying a spec file with a partial entry throws python exception and pops up an error box in Visual Studio Code #42

Open sreeves opened 10 months ago

sreeves commented 10 months ago

Fresh git clone and build/install today. Ran VSCode pointing at podman package checkout from SUSE:SLE-15-SP5:Update. Started to modify the spec file and only added a "{_datadir}" entry on a new line and hovering over that pops up a "Request textDocument/hover failed" message box and the command line shows the following python exception

ERROR:_handle_request:Failed to handle request 29 textDocument/hover HoverParams(text_document=TextDocumentIdentifier(uri='file:///home/vmuser1/obs_internal/podman/podman.spec'), position=63:6, work_done_token=None)
Traceback (most recent call last):
  File "/home/vmuser1/.cache/pypoetry/virtualenvs/rpm-spec-language-server-Ve3TtjRo-py3.11/lib/python3.11/site-packages/pygls/protocol/json_rpc.py", line 265, in _handle_request
    self._execute_request(msg_id, handler, params)
  File "/home/vmuser1/.cache/pypoetry/virtualenvs/rpm-spec-language-server-Ve3TtjRo-py3.11/lib/python3.11/site-packages/pygls/protocol/json_rpc.py", line 187, in _execute_request
    self._send_response(msg_id, handler(params))
                                ^^^^^^^^^^^^^^^
  File "/home/vmuser1/git_repo/rpm-spec-language-server/rpm_spec_language_server/server.py", line 400, in expand_macro
    macro = get_macro_under_cursor(
            ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vmuser1/git_repo/rpm-spec-language-server/rpm_spec_language_server/macros.py", line 103, in get_macro_under_cursor
    symbol = get_macro_string_at_position(lines[position.line], position.character)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/vmuser1/git_repo/rpm-spec-language-server/rpm_spec_language_server/macros.py", line 19, in get_macro_string_at_position
    line[i] == "%"
    ~~~~^^^
IndexError: string index out of range
dcermak commented 10 months ago

Ok, this is a nasty issue. What happens under the hood is the following: you added %_datadir below the preamble section. But that renders the spec file invalid, which results in the LSP server having a stale internal spec state (as rpm fails to parse the spec now). Thereby it keeps looking for the macro on the correct line, but in its internal state, that line is empty.