Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
apoorv569 opened this issue Oct 13, 2022 · 2 comments
Closed

Comments

@apoorv569
Copy link

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
Copy link

azzamsa commented Jun 26, 2023

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)

@apoorv569
Copy link
Author

apoorv569 commented Jun 26, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants