Skip to content

Commit f8ae845

Browse files
committed
Demote errors in flycheck-rust-setup
As discussed in [1], raising errors in `flycheck-rust-setup` in combination with `global-flycheck-mode` will leave Emacs in an unusable state. The previous commit added a guard for catching errors when the cargo executable wasn't found, but other errors could still happen (wrong argument types, or through `json-read`). To make sure no errors are raised, this commit wraps the body of `flycheck-rust-setup` in `with-demoted-errors`. [1]: #40 (comment)
1 parent d019e7a commit f8ae845

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

flycheck-rust.el

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,16 @@ returned by default.
107107
Return a cons cell (TYPE . NAME), where TYPE is the target
108108
type (lib or bin), and NAME the target name (usually, the crate
109109
name)."
110-
(let ((json-array-type 'list))
110+
(let ((json-array-type 'list)
111+
(cargo (funcall flycheck-executable-find "cargo")))
112+
(unless cargo
113+
(user-error "flycheck-rust cannot find `cargo'. Please \
114+
make sure that cargo is installed and on your PATH. See \
115+
http://www.flycheck.org/en/latest/user/troubleshooting.html for \
116+
more information on setting your PATH with Emacs."))
111117
(-let [(&alist 'targets targets)
112118
(with-temp-buffer
113-
(call-process (funcall flycheck-executable-find "cargo") nil t nil "read-manifest")
119+
(call-process cargo nil t nil "read-manifest")
114120
(goto-char (point-min))
115121
(json-read))]
116122
;; If there is a target that matches the file-name exactly, pick that
@@ -129,17 +135,12 @@ name)."
129135
If the current file is part of a Cargo project, configure
130136
Flycheck according to the Cargo project layout."
131137
(interactive)
132-
(when (buffer-file-name)
133-
(-when-let (root (flycheck-rust-project-root))
134-
;; We should avoid raising any error in this function, as in combination
135-
;; with `global-flycheck-mode' it will render Emacs unusable (see
136-
;; https://github.com/flycheck/flycheck-rust/issues/40#issuecomment-253760883).
137-
(if (not (funcall flycheck-executable-find "cargo"))
138-
;; We can still inform the user though
139-
(message "flycheck-rust cannot find `cargo'. Please \
140-
make sure that cargo is installed and on your PATH. See \
141-
http://www.flycheck.org/en/latest/user/troubleshooting.html for \
142-
more information on setting your PATH with Emacs.")
138+
;; We should avoid raising any error in this function, as in combination
139+
;; with `global-flycheck-mode' it will render Emacs unusable (see
140+
;; https://github.com/flycheck/flycheck-rust/issues/40#issuecomment-253760883).
141+
(with-demoted-errors "Error in flycheck-rust-setup: %S"
142+
(when (buffer-file-name)
143+
(-when-let (root (flycheck-rust-project-root))
143144
(pcase-let ((rel-name (file-relative-name (buffer-file-name) root))
144145
(`(,target-type . ,target-name) (flycheck-rust-find-target
145146
(buffer-file-name))))

0 commit comments

Comments
 (0)