-
-
Notifications
You must be signed in to change notification settings - Fork 19
Fix error related to multiple binary targets #25
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,7 @@ | |
|
||
(require 'dash) | ||
(require 'flycheck) | ||
(require 'json) | ||
|
||
(defun flycheck-rust-executable-p (rel-name) | ||
"Whether REL-NAME denotes an executable. | ||
|
@@ -94,6 +95,32 @@ Return non-nil if PROJECT-ROOT is a binary crate, nil otherwise." | |
(let ((root-dir (file-name-directory project-root))) | ||
(file-exists-p (expand-file-name "src/main.rs" root-dir)))) | ||
|
||
(defun flycheck-rust-find-target (file-name) | ||
"Find and return the cargo target associated with the given file. | ||
|
||
FILE-NAME is the name of the file that is matched against the | ||
'src_path' value in the list 'targets' returned by 'cargo | ||
read-manifest'. If there is no match, the first target is | ||
returned by default. | ||
|
||
Return a cons cell (TYPE . NAME), where TYPE is the target | ||
type (lib or bin), and NAME the target name (usually, the crate | ||
name)." | ||
(let* ((manifest (let ((json-array-type 'list)) | ||
(json-read-from-string | ||
(shell-command-to-string "cargo read-manifest")))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't use Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, skipping the shell is indeed best. Out of curiosity, would |
||
(default-target (cadr (assq 'targets manifest))) | ||
;; Assuming there is always at least one target | ||
(target-type (cadr (assq 'kind default-target))) | ||
(target-name (cdr (assq 'name default-target)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please take a look at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! I was wondering if there wasn't something more direct to deal with that. |
||
;; If there is a target that matches the file-name exactly, pick that one | ||
;; instead | ||
(dolist (target (cdr (assq 'targets manifest))) | ||
(when (string= file-name (cdr (assq 'src_path target))) | ||
(setq target-type (cadr (assq 'kind target))) | ||
(setq target-name (cdr (assq 'name target))))) | ||
(cons target-type target-name))) | ||
|
||
;;;###autoload | ||
(defun flycheck-rust-setup () | ||
"Setup Rust in Flycheck. | ||
|
@@ -103,7 +130,8 @@ Flycheck according to the Cargo project layout." | |
(interactive) | ||
(when (buffer-file-name) | ||
(-when-let (root (flycheck-rust-project-root)) | ||
(let ((rel-name (file-relative-name (buffer-file-name) root))) | ||
(let ((rel-name (file-relative-name (buffer-file-name) root)) | ||
(target (flycheck-rust-find-target (buffer-file-name)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we don't need Makes no difference semantically, but it's much easier to read :) |
||
;; These are valid crate roots as by Cargo's layout | ||
(if (or (flycheck-rust-executable-p rel-name) | ||
(flycheck-rust-test-p rel-name) | ||
|
@@ -117,9 +145,16 @@ Flycheck according to the Cargo project layout." | |
;; Check tests in libraries and integration tests | ||
(setq-local flycheck-rust-check-tests | ||
(not (flycheck-rust-executable-p rel-name))) | ||
;; Set the crate type | ||
(setq-local flycheck-rust-crate-type | ||
(if (flycheck-rust-binary-crate-p root) | ||
"bin" "lib")) ;; Set the crate type | ||
(if (string= (car target) "bin") | ||
(progn | ||
;; If it's binary target, we need to pass the binary | ||
;; name | ||
(setq-local flycheck-rust-binary-name | ||
(cdr target)) | ||
"bin") | ||
"lib")) | ||
;; Find build libraries | ||
(setq-local flycheck-rust-library-path | ||
(list (expand-file-name "target/debug" root) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Emacs docstrings use a different quoting style, namely ``src_path'`. Please update all quotes accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure what to use because those weren't elisp value I was quoting. Good to know.