manateelazycat / lsp-bridge

A blazingly fast LSP client for Emacs
GNU General Public License v3.0
1.47k stars 213 forks source link

[BUG] lsp-bridge-find-def error with jedi server for python: [AttributeError: 'str' object has no attribute 'as_uri'] #386

Closed K-gihu closed 2 years ago

K-gihu commented 2 years ago

To reproduce, open emacs with:

emacs -q -l ~/meow-emacs/test.el

test.el:

(dolist (folder (directory-files "~/meow-emacs/straight/build" t))
  (add-to-list 'load-path folder))

(require 'posframe)
(require 'markdown-mode)
(require 'lsp-bridge)
(setq lsp-bridge-python-lsp-server "jedi")
(keymap-global-set "C-6" 'lsp-bridge-lookup-documentation)
(keymap-global-set "C-7" 'lsp-bridge-find-def)
(keymap-global-set "C-8" 'lsp-bridge-return-from-def)
(keymap-global-set "C-9" 'lsp-bridge-find-references)
(keymap-global-set "C-0" 'lsp-bridge-rename)
(global-lsp-bridge-mode)

Then open a test python file ~/test.py, in which:

a = 3

Place the cursor on a,and press C-7, error occurs in *lsp-bridge* buffer:

--- Send initialize for /home/klu/test.py (jedi)

--- Send (63459): initialize

--- Recv response jedi (63459)

--- Send (notification): initialized

--- Send (notification): workspace/didChangeConfiguration

--- Send (notification): textDocument/didOpen

--- Send (30661): textDocument/signatureHelp

--- Recv notification: textDocument/publishDiagnostics

--- Recv response jedi (30661): textDocument/signatureHelp

--- Send (42924): textDocument/signatureHelp

--- Recv response jedi (42924): textDocument/signatureHelp

--- Send (4818): textDocument/definition
ERROR:pygls.protocol:Failed to handle request 4818 textDocument/definition work_done_token=None text_document=TextDocumentIdentifier(uri='file:///home/klu/test.py') position=0:0
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/pygls/protocol.py", line 331, in _handle_request
    self._execute_request(msg_id, handler, params)
  File "/usr/lib/python3.10/site-packages/pygls/protocol.py", line 260, in _execute_request
    method_name, method_type, msg_id, handler(params))
  File "/usr/lib/python3.10/site-packages/jedi_language_server/server.py", line 304, in definition
    definitions = [
  File "/usr/lib/python3.10/site-packages/jedi_language_server/server.py", line 304, in <listcomp>
    definitions = [
  File "/usr/lib/python3.10/site-packages/jedi_language_server/server.py", line 306, in <genexpr>
    for definition in (jedi_utils.lsp_location(name) for name in names)
  File "/usr/lib/python3.10/site-packages/jedi_language_server/jedi_utils.py", line 104, in lsp_location
    return Location(uri=module_path.as_uri(), range=lsp)
AttributeError: 'str' object has no attribute 'as_uri'

--- Recv message (error):
{
   "jsonrpc": "2.0",
   "id": 4818,
   "error": {
      "code": -32602,
      "message": "AttributeError: 'str' object has no attribute 'as_uri'",
      "data": "{'traceback': ['  File \"/usr/lib/python3.10/site-packages/pygls/protocol.py\", line 331, in _handle_request\\n    self._execute_request(msg_id, handler, params)\\n', '  File \"/usr/lib/python3.10/site-packages/pygls/protocol.py\", line 260, in _execute_request\\n    method_name, method_type, msg_id, handler(params))\\n', '  File \"/usr/lib/python3.10/site-packages/jedi_language_server/server.py\", line 304, in definition\\n    definitions = [\\n', '  File \"/usr/lib/python3.10/site-packages/jedi_language_server/server.py\", line 304, in <listcomp>\\n    definitions = [\\n', '  File \"/usr/lib/python3.10/site-packages/jedi_language_server/server.py\", line 306, in <genexpr>\\n    for definition in (jedi_utils.lsp_location(name) for name in names)\\n', '  File \"/usr/lib/python3.10/site-packages/jedi_language_server/jedi_utils.py\", line 104, in lsp_location\\n    return Location(uri=module_path.as_uri(), range=lsp)\\n']}"
   }
}

--- Send (24011): textDocument/signatureHelp

--- Recv response jedi (24011): textDocument/signatureHelp

--- Send (5210): textDocument/signatureHelp

--- Recv response jedi (5210): textDocument/signatureHelp

--- Send (notification): textDocument/didClose
Exit server: /home/klu/test.py#jedi

--- Send (60895): shutdown

--- Send (notification): exit

--- Lsp server exited, exit code: -9
b''
manateelazycat commented 2 years ago

I can't reproduce your problem, my jedi-language-server --version is 0.36.1

K-gihu commented 2 years ago

打开 lsp-bridge-enable-log 之后,进行与上面相同的操作,*lsp-bridge* 显示:

Eval in Emacs: (lsp-bridge--first-start '58243)
Eval in Emacs: (message '"[LSP-Bridge] Start LSP server (jedi) for /home/klu/test.py...")
Handlers: [<class 'core.handler.completion.Completion'>,
 <class 'core.handler.completion_item.CompletionItem'>,
 <class 'core.handler.find_define.FindDefine'>,
 <class 'core.handler.find_implementation.FindImplementation'>,
 <class 'core.handler.find_references.FindReferences'>,
 <class 'core.handler.hover.Hover'>,
 <class 'core.handler.signature_help.SignatureHelp'>,
 <class 'core.handler.prepare_rename.PrepareRename'>,
 <class 'core.handler.rename.Rename'>,
 <class 'core.handler.jdt_uri_resolver.JDTUriResolver'>,
 <class 'core.handler.deno_uri_resolver.DenoUriResolver'>,
 <class 'core.handler.code_action.CodeAction'>,
 <class 'core.handler.formatting.Formatting'>,
 <class 'core.handler.execute_command.ExecuteCommand'>,
 <class 'core.handler.workspace_symbol.WorkspaceSymbol'>]

--- Send initialize for /home/klu/test.py (jedi)

--- Send (25015): initialize
{
   "id": 25015,
   "method": "initialize",
   "params": {
      "processId": 191312,
      "rootPath": "/home/klu/test.py",
      "clientInfo": {
         "name": "emacs",
         "version": "GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.34, cairo version 1.17.6)\n of 2022-10-25"
      },
      "rootUri": "file:///home/klu/test.py",
      "capabilities": {
         "workspace": {
            "configuration": true,
            "symbol": {
               "resolveSupport": {
                  "properties": []
               }
            }
         },
         "textDocument": {
            "completion": {
               "completionItem": {
                  "snippetSupport": false,
                  "deprecatedSupport": true,
                  "tagSupport": {
                     "valueSet": [
                        1
                     ]
                  }
               }
            },
            "codeAction": {
               "dynamicRegistration": false,
               "codeActionLiteralSupport": {
                  "codeActionKind": {
                     "valueSet": [
                        "quickfix",
                        "refactor",
                        "refactor.extract",
                        "refactor.inline",
                        "refactor.rewrite",
                        "source",
                        "source.organizeImports"
                     ]
                  }
               },
               "isPreferredSupport": true
            }
         }
      },
      "initializationOptions": {}
   },
   "jsonrpc": "2.0"
}

--- Recv response jedi (25015)
{
   "jsonrpc": "2.0",
   "id": 25015,
   "result": {
      "capabilities": {
         "textDocumentSync": {
            "openClose": true,
            "change": 2,
            "willSave": null,
            "willSaveWaitUntil": null,
            "save": true
         },
         "completionProvider": {
            "triggerCharacters": [
               ".",
               "'",
               "\""
            ],
            "resolveProvider": true
         },
         "hoverProvider": true,
         "signatureHelpProvider": {
            "triggerCharacters": [
               "(",
               ","
            ]
         },
         "definitionProvider": true,
         "typeDefinitionProvider": {},
         "referencesProvider": true,
         "documentHighlightProvider": true,
         "documentSymbolProvider": true,
         "codeActionProvider": {
            "codeActionKinds": [
               "refactor.inline",
               "refactor.extract"
            ]
         },
         "renameProvider": true,
         "executeCommandProvider": {
            "commands": []
         },
         "workspaceSymbolProvider": true,
         "workspace": {
            "workspaceFolders": {
               "supported": true,
               "changeNotifications": true
            },
            "fileOperations": {}
         }
      }
   }
}
Eval in Emacs: (message '"[LSP-Bridge] Start LSP server (jedi) for /home/klu/test.py with 'single-file' mode, enjoy hacking!")

--- Send (notification): initialized
{
   "method": "initialized",
   "params": {},
   "jsonrpc": "2.0"
}

--- Send (notification): workspace/didChangeConfiguration
{
   "method": "workspace/didChangeConfiguration",
   "params": {
      "settings": {}
   },
   "jsonrpc": "2.0"
}

--- Send (notification): textDocument/didOpen
{
   "method": "textDocument/didOpen",
   "params": {
      "textDocument": {
         "uri": "file:///home/klu/test.py",
         "languageId": "python",
         "version": 0,
         "text": "a = 3\n"
      }
   },
   "jsonrpc": "2.0"
}

--- Send (22040): textDocument/definition
{
   "id": 22040,
   "method": "textDocument/definition",
   "params": {
      "position": {
         "line": 0,
         "character": 0
      },
      "textDocument": {
         "uri": "file:///home/klu/test.py"
      }
   },
   "jsonrpc": "2.0"
}

--- Recv notification: textDocument/publishDiagnostics
{
   "jsonrpc": "2.0",
   "method": "textDocument/publishDiagnostics",
   "params": {
      "uri": "file:///home/klu/test.py",
      "diagnostics": []
   }
}
ERROR:pygls.protocol:Failed to handle request 22040 textDocument/definition work_done_token=None text_document=TextDocumentIdentifier(uri='file:///home/klu/test.py') position=0:0
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/pygls/protocol.py", line 331, in _handle_request
    self._execute_request(msg_id, handler, params)
  File "/usr/lib/python3.10/site-packages/pygls/protocol.py", line 260, in _execute_request
    method_name, method_type, msg_id, handler(params))
  File "/usr/lib/python3.10/site-packages/jedi_language_server/server.py", line 306, in definition
    definitions = [
  File "/usr/lib/python3.10/site-packages/jedi_language_server/server.py", line 306, in <listcomp>
    definitions = [
  File "/usr/lib/python3.10/site-packages/jedi_language_server/server.py", line 308, in <genexpr>
    for definition in (jedi_utils.lsp_location(name) for name in names)
  File "/usr/lib/python3.10/site-packages/jedi_language_server/jedi_utils.py", line 107, in lsp_location
    return Location(uri=module_path.as_uri(), range=lsp)
AttributeError: 'str' object has no attribute 'as_uri'

--- Recv message (error):
{
   "jsonrpc": "2.0",
   "id": 22040,
   "error": {
      "code": -32602,
      "message": "AttributeError: 'str' object has no attribute 'as_uri'",
      "data": "{'traceback': ['  File \"/usr/lib/python3.10/site-packages/pygls/protocol.py\", line 331, in _handle_request\\n    self._execute_request(msg_id, handler, params)\\n', '  File \"/usr/lib/python3.10/site-packages/pygls/protocol.py\", line 260, in _execute_request\\n    method_name, method_type, msg_id, handler(params))\\n', '  File \"/usr/lib/python3.10/site-packages/jedi_language_server/server.py\", line 306, in definition\\n    definitions = [\\n', '  File \"/usr/lib/python3.10/site-packages/jedi_language_server/server.py\", line 306, in <listcomp>\\n    definitions = [\\n', '  File \"/usr/lib/python3.10/site-packages/jedi_language_server/server.py\", line 308, in <genexpr>\\n    for definition in (jedi_utils.lsp_location(name) for name in names)\\n', '  File \"/usr/lib/python3.10/site-packages/jedi_language_server/jedi_utils.py\", line 107, in lsp_location\\n    return Location(uri=module_path.as_uri(), range=lsp)\\n']}"
   }
}
Eval in Emacs: (message '"[LSP-Bridge] AttributeError: 'str' object has no attribute 'as_uri'")
Eval in Emacs: (lsp-bridge-diagnostic--render '"/home/klu/test.py" '())

--- Send (33658): textDocument/signatureHelp
{
   "id": 33658,
   "method": "textDocument/signatureHelp",
   "params": {
      "position": {
         "line": 0,
         "character": 0
      },
      "textDocument": {
         "uri": "file:///home/klu/test.py"
      }
   },
   "jsonrpc": "2.0"
}

--- Recv response jedi (33658): textDocument/signatureHelp
{
   "jsonrpc": "2.0",
   "id": 33658,
   "result": null
}

--- Send (6143): textDocument/signatureHelp
{
   "id": 6143,
   "method": "textDocument/signatureHelp",
   "params": {
      "position": {
         "line": 0,
         "character": 0
      },
      "textDocument": {
         "uri": "file:///home/klu/test.py"
      }
   },
   "jsonrpc": "2.0"
}

--- Recv response jedi (6143): textDocument/signatureHelp
{
   "jsonrpc": "2.0",
   "id": 6143,
   "result": null
}

--- Send (15734): textDocument/signatureHelp
{
   "id": 15734,
   "method": "textDocument/signatureHelp",
   "params": {
      "position": {
         "line": 0,
         "character": 0
      },
      "textDocument": {
         "uri": "file:///home/klu/test.py"
      }
   },
   "jsonrpc": "2.0"
}

--- Recv response jedi (15734): textDocument/signatureHelp
{
   "jsonrpc": "2.0",
   "id": 15734,
   "result": null
}

--- Send (32487): textDocument/signatureHelp
{
   "id": 32487,
   "method": "textDocument/signatureHelp",
   "params": {
      "position": {
         "line": 0,
         "character": 0
      },
      "textDocument": {
         "uri": "file:///home/klu/test.py"
      }
   },
   "jsonrpc": "2.0"
}

--- Recv response jedi (32487): textDocument/signatureHelp
{
   "jsonrpc": "2.0",
   "id": 32487,
   "result": null
}

--- Send (notification): textDocument/didClose
{
   "method": "textDocument/didClose",
   "params": {
      "textDocument": {
         "uri": "file:///home/klu/test.py"
      }
   },
   "jsonrpc": "2.0"
}

--- Send (14501): shutdown
{
   "id": 14501,
   "method": "shutdown",
   "params": {},
   "jsonrpc": "2.0"
}

--- Send (notification): exit
{
   "method": "exit",
   "params": {},
   "jsonrpc": "2.0"
}
Exit server: /home/klu/test.py#jedi

--- Lsp server exited, exit code: -9
b''

使用 arch linux 系统,几个包的版本是:

% pacman -Q python-jedi jedi-language-server emacs-git
python-jedi 0.18.1-1
jedi-language-server 0.37.0-2
emacs-git 29.0.50.161406-1

lsp-bridge 用最新的。

manateelazycat commented 2 years ago

把你的 test.py 附件发上来看看吧

K-gihu commented 2 years ago

test.py 实际上只有一行,就是这个

a = 3

然后把光标放在 a 上执行 lsp-bridge-find-def 之后就会出现上面的错误

K-gihu commented 2 years ago

我怀疑这可能是 jedi 服务器配置的问题?我还是第一次用 jedi,请问您用了什么样的配置文件呢?

manateelazycat commented 2 years ago

我怀疑这可能是 jedi 服务器配置的问题?我还是第一次用 jedi,请问您用了什么样的配置文件呢?

我用pyright

manateelazycat commented 2 years ago

我没有配置 jedi , 请排查一下自己的配置吧。