zbelial / lspce

LSP Client for Emacs implemented as a module using rust.
GNU General Public License v3.0
154 stars 11 forks source link

Add 'exec-path' to environment variable when spawning the LSP server. #29

Closed peterzky closed 4 months ago

peterzky commented 4 months ago

I have been using direnv to manage my development environment and I'd like to switch to lspce from eglot. However, when spawning the LSP server, it ignores the PATH that direnv provides, preventing me from accessing the proper development environment, like python virtualenv.

I made a few changes to lspce.el to make it pass the exec-path when spawning the LSP server. It's now working, but I'm not sure if this is the correct approach. Do you have any suggestions?

zbelial commented 4 months ago

Hi @peterzky, thanks for your PR. I have never used direnv before, so I'll learn what it is and how to use it first.

peterzky commented 4 months ago

Direnv sets up environment variables when you cd into directories.

zbelial commented 4 months ago

Thanks. So how do you use direnv with Emacs? I mean do you cd some dir, then start Emacs there, and then do you development?

You said you use eglot, so eglot works well with direnv, right? Do you have any special config when using eglot + direnv? (such as .dir-locals.el or something else). I ask because I didn't find anything related to exec-path in eglot's code.

peterzky commented 4 months ago

The emacs-direnv package sets up the environment variables for an open file or project if direnv is already set up in the project/directory. When using eglot, it inherits the exec-path in Emacs. However, it seems that lspce does not function in the same way.

peterzky commented 4 months ago

For additional information, the following shows how eglot starts the process:

(make-process
    :name readable-name
    :command (setq server-info (eglot--cmd contact))
    :connection-type 'pipe
    :coding 'utf-8-emacs-unix
    :noquery t
    :stderr (get-buffer-create (format "*%s stderr*" readable-name))
    :file-handler t)

The make-process function will inherit the 'exec-path' from Emacs.

zbelial commented 4 months ago

Sorry for late reply, I need more time to get familiar with it, maybe test it too. Once I'm done, I'll merge it or talk about it with you. Thanks. Also thanks for expaining what direnv is and how it works.

zbelial commented 4 months ago

Hi @peterzky , I finally merged your PR. Thanks again.

And I also made addtional changes:

  1. I added a new customization variable lspce-inherit-exec-path (means passing exec-path or not) and its default value is nil, so people who don't need this feature won't be affected.
  2. In lib.rs, only when emacs_exec_path is not empty, it will be used as PATH.

So it you use the new code, you should set lspce-inherit-exec-path to t in your config.

peterzky commented 4 months ago

Thanks, great job!