rksm / emacs-rust-config

emacs.d files for making Emacs a Rust development environment
MIT License
92 stars 28 forks source link

LSP cargo check on save not respecting CARGO_HOME env var. #13

Closed apoorv569 closed 1 year ago

apoorv569 commented 1 year ago

I have configured my Emacs for using rustic-mode by reading your blog post - https://robert.kra.hn/posts/rust-emacs-setup/

Though I have a problem with the cargo check command being run whenever you save your modified buffer.

I have changed my CARGO_HOME and RUSTUP_HOME to ~/.local/share/cargo and ~/.local/share/rustup respectively but the cargo check command that runs when you save buffer still uses ~/.cargo which leads me to compile my project twice before I can run it as I use external terminal to run the project.

I tried changing it to clippy as mentioned in your post but its still same it runs under ~/.cargo.

Is there a way to fix this? I have tried modifying various variables related to cargo bin path and all nothing seems to help.

I also have exec-path-from-shell plugin installed but it doesn't help either.

  (use-package exec-path-from-shell
    :straight t)

  (when (memq window-system '(mac ns x))
    (exec-path-from-shell-initialize))
rksm commented 1 year ago

What is the value of (getenv "CARGO_HOME") and (getenv "RUSTUP_HOME") (you can evaluate an elisp expression e.g. with M-:)? Does it get picked up up by exec-path-from-shell-initialize?

If not, you can set thoes manually with (setenv "CARGO_HOME" "...") and (setenv "RUSTUP_HOME" "...").

Other than that it seems that lsp-mode defines the following variables:

(defcustom lsp-rust-library-directories '("~/.cargo/registry/src" "~/.rustup/toolchains") ...)
(defcustom lsp-rust-analyzer-library-directories '("~/.cargo/registry/src" "~/.rustup/toolchains") ...)

You can customize them, e.g. by doing

(use-package lsp-rust
  :custom
   (lsp-rust-library-directories '("~/.local/share/cargo/registry/src" "~/.local/share/rustup/toolchains"))
   (lsp-rust-analyzer-library-directories '("~/.local/share/cargo/registry/src" "~/.local/share/rustup/toolchains")))

My guess would be that this is enough, if not let me know.

apoorv569 commented 1 year ago

What is the value of (getenv "CARGO_HOME") and (getenv "RUSTUP_HOME") (you can evaluate an elisp expression e.g. with M-:)? Does it get picked up up by exec-path-from-shell-initialize?

If not, you can set thoes manually with (setenv "CARGO_HOME" "...") and (setenv "RUSTUP_HOME" "...").

Other than that it seems that lsp-mode defines the following variables:

(defcustom lsp-rust-library-directories '("~/.cargo/registry/src" "~/.rustup/toolchains") ...)
(defcustom lsp-rust-analyzer-library-directories '("~/.cargo/registry/src" "~/.rustup/toolchains") ...)

You can customize them, e.g. by doing

(use-package lsp-rust
  :custom
   (lsp-rust-library-directories '("~/.local/share/cargo/registry/src" "~/.local/share/rustup/toolchains"))
   (lsp-rust-analyzer-library-directories '("~/.local/share/cargo/registry/src" "~/.local/share/rustup/toolchains")))

My guess would be that this is enough, if not let me know.

(getenv "CARGO_HOME") and (getenv "RUSTUP_HOME") returned nil which made me wonder if the exec-path-from-shell does anything and it looks like exec-path-from-shell only adds $PATH and $MANPATH from the shell by default.

Digging around I found this variable exec-path-from-shell-variables in which you can specify which env variable it should copy. I added all my manually exported env variables to this list. It is now working as expected.

I also changed lsp-rust-library-directories and lsp-rust-analyzer-library-directories but I assume they should be set automatically now as well.