rust-lang / rust-mode

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

rust-mode doesn't find rustfmt by default #450

Open GaryBlackbourne opened 2 years ago

GaryBlackbourne commented 2 years ago

I have installed rust following the guide on rusts website, and installed rust-mode through melpa. When I put the autoformat on save hook into my .emacs file, it failed to find rustfmt in my ~/.cargo/bin directory even if this is part of my PATH variable.

I fixed this with creating a simlink in my /usr/local/bin directory, and now, it seems to work. I think it might be good to add this to README or otherwise make it work with default install.

yugaego commented 2 years ago

I too remember getting this error message: Could not locate executable "rustfmt".

I think your approach to fix the issue is fine. More approaches follow.

  1. Note there's also a customizable variable rust-rustfmt-bin, which is set to rustfmt by default. This variable can be changed to the full path to the rustfmt executable. Now I wonder, might be ~/.cargo/bin/rustfmt would be a better default (with similar change for rust-cargo-bin)?

  2. Or add rustfmt directory to Emacs' exec-path variable (PATH and Emacs is a separate topic you can quite easily find information about). For instance,

(add-to-list 'exec-path "~/.cargo/bin")

As for the README or code changes, I suspect this repo maintainers would be glad to consider/accept PRs with the improvements. Or consider adding a Wiki note on this. Though might be Discussions, if enabled, could be a better place for the topic we're in now.

brotzeit commented 2 years ago

Now I wonder, might be ~/.cargo/bin/rustfmt would be a better default (with similar change for rust-cargo-bin)?

We've already had this discussion several times and I've also changed it several times over the last years =)

As you already mentioned, this is an emacs issue not rust-mode.

Have you guys tried https://github.com/purcell/exec-path-from-shell ?

yugaego commented 2 years ago

We've already had this discussion several times and I've also changed it several times over the last years

Not surprised.

Have you guys tried https://github.com/purcell/exec-path-from-shell ?

I tried it, but now happily live without it. BTW, my previous message contains a link to the SO thread that suggests this package among the other things.

brotzeit commented 2 years ago

I wonder why this still wasn't fixed in emacs. doom-emacs seem to have solved it.

yugaego commented 2 years ago

doom-emacs seem to have solved it.

Looks like doom-emacs generates a file with correct environment variables. There's an interesting comparison to exec-path-from-shell below in that file too.

why this still wasn't fixed in emacs

I see this topic was mentioned several times on the dev/bug mailing lists and an overall impression I got: