The command xref-find-definitions
is no longer working for me in Python buffers (with elpy-mode
enabled. Its companions, such as xref-find-references
, are also not working in Python buffers. Specifically, when I press M-.
I get No definition found
in the minibuffer. xref
was previously working perfectly in Python buffers. Since it worked, I have updated my installed emacs packages, and also set up ggtags
& GNU Global to work in cc-mode
. Note that xref
still works like a charm in elisp files (such as my .emacs
file), so I believe the problem to be elpy
-specific.
Due to the interaction of several packages, I am not sure how to approach debugging and resolving this issue. How do I get M-.
working in Python buffers once more?
Steps to reproduce
In my_file.py
write:
def func():
print("hi")
func()
Move point to the second occurrence of func
and hit M-.
to receive No definition found
message.
My configuration
I am running MacOS Catalina (10.15.7)
In python buffers, I have:
xref-backend-functions
set to (buffer local) (elpy--xref-backend t)
. When I try to run, in turn, elpy--xref-backend
, I learn that this function takes no arguments (though it was given one). When I run it with no arguments, I get (elpy--xref-backend)
returning elpy
.
Result of (elpy-config)
Elpy Configuration
Emacs.............: 26.3
Elpy..............: 1.35.0
Virtualenv........: python38 (/usr/local/Caskroom/miniconda/base/envs/python38)
Interactive Python: python 3.8.5 (/usr/local/Caskroom/miniconda/base/envs/python38/bin/python)
RPC virtualenv....: python38 (/usr/local/Caskroom/miniconda/base/envs/python38)
Python...........: python 3.8.5 (/usr/local/Caskroom/miniconda/base/envs/python38/bin/python)
Jedi.............: 0.18.0
Rope.............: Not found (0.18.0 available)
Autopep8.........: 1.5.4
Yapf.............: 0.30.0
Black............: 20.8b1
Syntax checker....: flake8 (/usr/local/Caskroom/miniconda/base/envs/python38/bin/flake8)
Elpy configuration in my .emacs
(use-package elpy
;;:diminish elpy-mode
:ensure t
:defer t
:init
(pyvenv-activate "/usr/local/Caskroom/miniconda/base/envs/python38")
(advice-add 'python-mode :before 'elpy-enable)
:config
(remove-hook 'elpy-modules 'elpy-module-flymake)
(define-key elpy-mode-map (kbd "C-c C-n") 'flycheck-next-error)
(define-key elpy-mode-map (kbd "C-c C-p") 'flycheck-previous-error)
(define-key elpy-mode-map (kbd "s-<down>") 'elpy-nav-forward-block)
(define-key elpy-mode-map (kbd "s-<up>") 'elpy-nav-backward-block)
(define-key elpy-mode-map (kbd "s-<right>") 'elpy-nav-forward-indent)
(define-key elpy-mode-map (kbd "s-<left>") 'elpy-nav-backward-indent)
)
And in the custom-set-variables call I have
'(elpy-rpc-timeout 2)
'(elpy-rpc-virtualenv-path "/usr/local/Caskroom/miniconda/base/envs/python38")
Backtrace
I used debug-on-entry
on xref-find-definitions
and I'm getting the following clip that will hopefully be enlightening to someone more savvy with the guts of elpy
Debugger entered--returning value: nil
elpy-rpc--default-error-callback(((message . "Object of type PosixPath is not JSON serializable") (code . 500) (data (traceback . "Traceback (most recent call last):\n File \"/Users/uname/.emacs.d/elpa/elpy-20201115.1811/elpy/rpc.py\", line 104, in handle_request\n self.write_json(result=result,\n File \"/Users/uname/.emacs.d/elpa/elpy-20201115.1811/elpy/rpc.py\", line 77, in write_json\n self.stdout.write(json.dumps(kwargs) + \"\\n\")\n File \"/usr/local/Caskroom/miniconda/base/envs/python38/lib/python3.8/json/__init__.py\", line 231, in dumps\n return _default_encoder.encode(obj)\n File \"/usr/local/Caskroom/miniconda/base/envs/python38/lib/python3.8/json/encoder.py\", line 199, in encode\n chunks = self.iterencode(o, _one_shot=True)\n File \"/usr/local/Caskroom/miniconda/base/envs/python38/lib/python3.8/json/encoder.py\", line 257, in iterencode\n return _iterencode(o, 0)\n File \"/usr/local/Caskroom/miniconda/base/envs/python38/lib/python3.8/json/encoder.py\", line 179, in default\n raise TypeError(f'Object of type {o.__class__.__name__} '\nTypeError: Object of type PosixPath is not JSON serializable\n"))))
* elpy-rpc--call-blocking("get_definition" ("/Users/uname/tmp/tmp.py" #("def func(x):\n return \"hi\"\n\n\ndef hello():\n func(1)\n return\n" 0 3 (fontified t face font-lock-keyword-face) 3 4 (fontified t) 4 8 (fontified t face font-lock-function-name-face) 8 11 (fontified t) 11 12 (fontified t composition (0 1 [32 65 57708]) prettify-symbols-start 12 prettify-symbols-end 13) 12 17 (fontified t) 17 23 (fontified t face font-lock-keyword-face) 23 24 (fontified t) 24 25 (syntax-table (15) fontified t face font-lock-string-face) 25 27 (fontified t face font-lock-string-face) 27 28 (syntax-table (15) fontified t face font-lock-string-face) 28 31 (fontified t) 31 34 (fontified t face font-lock-keyword-face) 34 35 (fontified t) 35 40 (fontified t face font-lock-function-name-face) 40 42 (fontified t) 42 43 (fontified t composition (0 1 [32 65 57708]) prettify-symbols-start 43 prettify-symbols-end 44) 43 44 (fontified t) 44 56 (fontified t) 56 60 (fontified t) 60 66 (fontified t face font-lock-keyword-face) 66 67 (fontified t)) 48))
* elpy-rpc("get_definition" ("/Users/uname/tmp/tmp.py" #("def func(x):\n return \"hi\"\n\n\ndef hello():\n func(1)\n return\n" 0 3 (fontified t face font-lock-keyword-face) 3 4 (fontified t) 4 8 (fontified t face font-lock-function-name-face) 8 11 (fontified t) 11 12 (fontified t composition (0 1 [32 65 57708]) prettify-symbols-start 12 prettify-symbols-end 13) 12 17 (fontified t) 17 23 (fontified t face font-lock-keyword-face) 23 24 (fontified t) 24 25 (syntax-table (15) fontified t face font-lock-string-face) 25 27 (fontified t face font-lock-string-face) 27 28 (syntax-table (15) fontified t face font-lock-string-face) 28 31 (fontified t) 31 34 (fontified t face font-lock-keyword-face) 34 35 (fontified t) 35 40 (fontified t face font-lock-function-name-face) 40 42 (fontified t) 42 43 (fontified t composition (0 1 [32 65 57708]) prettify-symbols-start 43 prettify-symbols-end 44) 43 44 (fontified t) 44 56 (fontified t) 56 60 (fontified t) 60 66 (fontified t face font-lock-keyword-face) 66 67 (fontified t)) 48) nil nil)
* elpy-rpc-get-definition()
* elpy-xref--definitions("6: func")
...