reddit / emacs
Restart Eglot when Conda environment changes
SaiDuc · 2022-03-14 13:58
For certain reasons I am switching from LSP-mode to Eglot. I am a Python developer who works a lot with conda environments. Sometimes I will open a python file before realising I am in the wrong conda environment. This means the LSP server (pyright) started with LSP-mode uses the wrong Python interpreter, not giving completions for libraries which are envionment-specific.
For this reason, in LSP-mode, I had:
`(add-hook 'conda-postactivate-hook (lambda () (lsp-restart-workspace)))`
I cannot find the equivalent for Eglot. If I do:
`(add-hook 'conda-postactivate-hook (lambda () (eglot-reconnect)))`
Note I am using the conda.el package for conda environment switching.
This does not work, instead I get the error "Error running timer: Process EGLOT (test/python-mode) not running"
Is there a way for me to cleanly restart Eglot in the buffer as though I had changed environments before visiting the file? Note, if I change environments first and then open the file, Eglot is correctly started in the proper environment.
Any help appreciated!
astoff1 · 2022-03-14 16:54
What you did looks correct. What happens if you don't modify that hook, but instead call `eglot-reconnect` manually after switching environments?
If you find it annoying to manually switch environments (which you should), then I suggest checking out the packages `buffer-env` and `envrc`.
SaiDuc · 2022-03-14 16:57
Just tried that now and I am getting the same error and same behaviour
wargxs · 2022-03-14 19:22
Do you want to shutdown and the restart the server? Does eglot-reconnect do that?
SaiDuc · 2022-03-14 19:35
I ideally do want to shutdown and restart the server. I do not think eglot-reconnect achieves that but I can't find any other easy function provided by eglot.
paretoOptimalDev · 2022-03-14 20:33
I don't think eglot has an equivalent "restart workspace" like lsp-mode does.
Honestly I stopped using eglot because when the lsp-server crashed it seemed like only restarting emacs made it functional again.
Plus even though eglot is supposed to stick more to standard emacs, things like Corfu and orderless completion style require just as much configuration as lsp-mode.
Then for some reason go to definition randomly stopped working.
Ah well...
SaiDuc · 2022-03-14 20:39
Honestly, I am seeing a lot of these problems as I am trying to get this working. There are a lot of things missing in Eglot that I consider to be basic. Like why can't Eglot handle killing and restarting the server, it started the server in the first place!
And the main reason I considered switching (performance in TRAMP on a very slow server) is also not really present. LSP-mode seems to perform just about the same on the server. Might give up on this endeavour. Thanks for your input!
astoff1 · 2022-03-15 08:17
`eglot-reconnect` does precisely that. I use it all the time when debugging a server.
SaiDuc · 2022-03-15 08:48
If that's the case then the server for pyright is not restarting correctly. I have no additional configuration for Eglot, so this should work as is.
My workflow is:
Open a python file, Eglot is started automatically. Completions are provided for the base environment.
I change environments using conda.el and run eglot-reconnect. The server should be restarted here but it is not, and I get no completions and the error message in the post is displayed.
astoff1 · 2022-03-15 18:27
It turns out `eglot-reconnect` calls the same executable, with same arguments, as the previously connected server. So you should do `eglot-shutdown` followed by `eglot`.
(I like to not have the server installed in my global environment, so when something is not right, it fails completely.)
SaiDuc · 2022-03-15 19:08
Still no luck with this method, I'm afraid. It is the same error. And in fact if `eglot-reconnect` was just calling the same executable with the same arguments, it would just run the server with the original interpreter, giving completions albeit incorrect ones. In my case the server fails to start entirely.
astoff1 · 2022-03-15 22:24
What if you actiavte your conda env in a terminal, launch Emacs from there, and then start Eglot? If that works, the problem must be with conda.el.