rust-lang / rust-mode

Emacs configuration for Rust
Apache License 2.0
1.09k stars 176 forks source link

cargo check and cargo metadata not respecting CARGO_HOME env variable. #464

Closed apoorv569 closed 1 year ago

apoorv569 commented 1 year ago

I have my CARGO_HOME and RUSTUP_HOME set to ~/.loca/share/cargo and ~/.local/share/rustup respectively, all commands do find the correct binary and run the correct command, but each time I make a change to my rust source file or Cargo.toml file cargo check or cargo metadata fires up automatically using the default cargo directory which is ~/.cargo and keeps re-compiling my program which takes some time and as it is needed to get linting and all I can't skip it.

I tried editing several variables to fix the issue but nothing seems to help,

  (use-package rust-mode
    :straight t
    :hook (rust-mode . lsp-deferred)
    :config
    (with-eval-after-load "lsp-mode"
      (add-to-list 'lsp-enabled-clients 'rust-analyzer))
    :custom
    (rust-format-on-save t)
    (rust-cargo-bin "~/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo")
    (rust-rustfmt-bin "~/.local/share/rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustfmt")
    (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")))

I added all those variables under the custom section, but nothing helps.

I had the problem with other commands not finding the binary path correctly at first, but I fixed that by installing,

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

  (when (memq window-system '(mac ns x))
    (exec-path-from-shell-initialize))

If I delete the ~/.cargo directory I get error,

LSP :: Error from the Language Server: waiting for cargo metadata or cargo check (Unknown error)

and after few seconds it starts again, creating the ~/.cargo directory and re-compiling my program.

azzamsa commented 1 year ago

Hi,

Same here, I set it to a custom location too. Do you find the solution?

Previously, I use Doom, and it works out of the box after running doom env https://github.com/rust-lang/rust-mode/issues/450#issuecomment-1172443787


UPDATE:

I need to set this https://github.com/joaotavora/eglot/discussions/1249#discussioncomment-6278374

apoorv569 commented 1 year ago

Hi,

Same here, I set it to a custom location too. Do you find the solution?

Previously, I use Doom, and it works out of the box after running doom env #450 (comment)

UPDATE:

I need to set this joaotavora/eglot#1249 (reply in thread)

Yes, it turns out exec-path-from-shell by default doesn't read all env variables. I added all my other env variables manually like this,

  (use-package exec-path-from-shell
    :straight t
    :custom
    ;; (exec-path-from-shell-shell-name "/bin/zsh")
    (exec-path-from-shell-variables '("PATH"
                                      "MANPATH"
                                      "XDG_DATA_HOME"
                                      "XDG_CACHE_HOME"
                                      "XDG_CONFIG_HOME"
                                      "XDG_STATE_HOME"
                                      "CARGO_HOME"
                                      "RUSTUP_HOME"
                                      "ANDROID_HOME"
                                      "DOOMDIR"
                                      "EMACSDIR"
                                      "XINITRC"
                                      "XSERVERRC"
                                      "XAUTHORITY"
                                      "USERXSESSION"
                                      "USERXSESSIONRC"
                                      "ALTUSERXSESSION"
                                      "INPUTRC"
                                      "ERRFILE"
                                      "GNUPGHOME"
                                      "_JAVA_OPTIONS"
                                      "GTK2_RC_FILES"
                                      "ZDOTDIR"
                                      "HISTFILE"
                                      "CARGO_HOME"
                                      "RUSTUP_HOME"
                                      "GOPATH"
                                      "RIPGREP_CONFIG_PATH"
                                      "XMONAD_CONFIG_HOME"
                                      "XMONAD_DATA_HOME"
                                      "XMONAD_CACHE_HOME"
                                      "WGETRC"
                                      "PASSWORD_STORE_DIR"
                                      "WINEPREFIX"
                                      "GEM_SPEC_CACHE"
                                      "VAGRANT_HOME"
                                      "XCURSOR_PATH"
                                      "STARSHIP_CONFIG"
                                      "STARSHIP_CACHE")))

  (when (memq window-system '(mac ns x))
    (exec-path-from-shell-initialize))

You can also just use getenv and setenv instead of this package if you want.