Skip to content

Commit e03c895

Browse files
mkpankovswsnr
authored andcommitted
Fix binary crate detection
flycheck-rust-binary-crate-p should be passed project root, not the crate root. Otherwise, detected crate root "src/main.rs" that gets passed to the function makes root-dir "src/" and expand-file-name produces "src/src/main.rs". The correct way to detect this is to navigate from project root - the directory where Cargo.toml is located. Also, set flycheck-rust-crate-root in both cases: when one of predicates match, the crate root is the rel-name of our current source. Otherwise, we locate closest possible crate root and set that. Fixes GH-20 and closes GH-22
1 parent e802d78 commit e03c895

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

flycheck-rust.el

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ relative to the current file."
8585
(-when-let (exe-crate-dir (locate-dominating-file (buffer-file-name) "main.rs"))
8686
(expand-file-name "main.rs" exe-crate-dir))))
8787

88-
(defun flycheck-rust-binary-crate-p (crate-root)
89-
"Determine whether CRATE-ROOT is a binary crate.
88+
(defun flycheck-rust-binary-crate-p (project-root)
89+
"Determine whether PROJECT-ROOT is a binary crate.
9090
91-
CRATE-ROOT is the path to the root module of a crate.
91+
PROJECT-ROOT is the path to the root directory of the project.
9292
93-
Return non-nil if CRATE-ROOT is a binary crate, nil otherwise."
94-
(let ((root-dir (file-name-directory crate-root)))
93+
Return non-nil if PROJECT-ROOT is a binary crate, nil otherwise."
94+
(let ((root-dir (file-name-directory project-root)))
9595
(file-exists-p (expand-file-name "src/main.rs" root-dir))))
9696

9797
;;;###autoload
@@ -105,21 +105,21 @@ Flycheck according to the Cargo project layout."
105105
(-when-let (root (flycheck-rust-project-root))
106106
(let ((rel-name (file-relative-name (buffer-file-name) root)))
107107
;; These are valid crate roots as by Cargo's layout
108-
(unless (or (flycheck-rust-executable-p rel-name)
109-
(flycheck-rust-test-p rel-name)
110-
(flycheck-rust-bench-p rel-name)
111-
(flycheck-rust-example-p rel-name)
112-
(string= "src/lib.rs" rel-name))
108+
(if (or (flycheck-rust-executable-p rel-name)
109+
(flycheck-rust-test-p rel-name)
110+
(flycheck-rust-bench-p rel-name)
111+
(flycheck-rust-example-p rel-name)
112+
(string= "src/lib.rs" rel-name))
113+
(setq-local flycheck-rust-crate-root rel-name)
113114
;; For other files, the library is either the default library or the
114115
;; executable
115116
(setq-local flycheck-rust-crate-root (flycheck-rust-find-crate-root)))
116117
;; Check tests in libraries and integration tests
117118
(setq-local flycheck-rust-check-tests
118119
(not (flycheck-rust-executable-p rel-name)))
119-
;; Set the crate type
120120
(setq-local flycheck-rust-crate-type
121-
(if (flycheck-rust-binary-crate-p flycheck-rust-crate-root)
122-
"bin" "lib"))
121+
(if (flycheck-rust-binary-crate-p root)
122+
"bin" "lib")) ;; Set the crate type
123123
;; Find build libraries
124124
(setq-local flycheck-rust-library-path
125125
(list (expand-file-name "target/debug" root)

0 commit comments

Comments
 (0)