rust-lang / rust-mode

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

Better support for `envrc` #446

Closed glasserc closed 2 years ago

glasserc commented 2 years ago

I have been using rust-mode on a project whose Rust version I manage using direnv and the associated emacs package envrc. When I try to use rust-test, it can't find cargo.

How envrc works is by making process-environment a file-local variable and updating it analogously to running direnv reload. However, because process-environment has not historically been file-local, some uses of it don't work the way you would expect. In particular, invoking with-temp-buffer does not re-use the correct environment. It's idiomatic in elisp to call call-process inside with-temp-buffer to capture its output, and because with-temp-buffer uses another environment, call-process cannot find executables that "should" be available in that directory.

I filed https://github.com/purcell/envrc/issues/12 to try to identify the correct solution to this problem. It feels like a footgun in emacs/elisp itself, but in the short term, Steve Purcell has been kind enough to write a utility library called inheritenv which allows these temporary buffers to inherit the environment from the buffer where they are being created. Alternately, it's possible for affected packages (like rust-mode) to define a utility that is like with-temp-buffer but propagates the environment, as in https://github.com/magit/magit/pull/4169/. I think one of these approaches would be a good change for rust-mode.

brotzeit commented 2 years ago

Pull requests are very welcome :+1: