Skip to content

Commit

Permalink
Changed flycheck-rust-manifest-directory to use 'cargo metadata'
Browse files Browse the repository at this point in the history
This commit fixes flycheck#1397.
By the recent change of cargo(rust-lang/cargo#4788),
flycheck can't detect the error file.
So I changed to use 'workspace_root' in 'cargo metadata's outputs,
as working directory.
If the user's cargo is a bit old and there's no 'workspace_root',
flycheck-rust-manifest-directory search a directory with 'Cargo.toml',
as ever.
  • Loading branch information
kngwyu committed Mar 7, 2018
1 parent b0edfef commit 2c82fbe
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions flycheck.el
Original file line number Diff line number Diff line change
Expand Up @@ -9471,14 +9471,38 @@ Relative paths are relative to the file being checked."
errors))

(defun flycheck-rust-manifest-directory ()
"Return the nearest directory holding the Cargo manifest.
"Return the working directory of Rust project.

Return the nearest directory containing the `Cargo.toml' manifest
file, starting from the current buffer and using
If we can get workspace root by 'cargo metadata' command, return it.
If not, return the nearest directory containing the `Cargo.toml'
manifest file, starting from the current buffer and using
`locate-dominating-file'. Return nil if there is no such file,
or if the current buffer has no file name."
(and buffer-file-name
(locate-dominating-file buffer-file-name "Cargo.toml")))
(let ((workspace-root (flycheck-rust-workspace-root)))
(or workspace-root
(and buffer-file-name
(locate-dominating-file buffer-file-name "Cargo.toml")))))

(defun flycheck-rust-get-metadata ()
"Exec 'cargo metadata' and return result as json.

Return nil if command doesn't exist or a parse error occurs."
(let ((metadata-list
(ignore-errors
(process-lines "cargo" "metadata" "--no-deps" "--format-version" "1"))))
(and metadata-list
(json-read-from-string (mapconcat 'identity metadata-list "")))))

(defun flycheck-rust-workspace-root ()
"Return the workspace root of Rust project.

This function depends on the 'workspace_root' field of 'cargo metadata' command.
This field is add on Dec 13, 2017 by https://github.com/rust-lang/cargo/pull/4788.
Return nil if 'workspace_root' doesn't exist."
(let ((metadata
(flycheck-rust-get-metadata)))
(and metadata
(alist-get 'workspace_root metadata))))

(defun flycheck-rust-cargo-has-command-p (command)
"Whether Cargo has COMMAND in its list of commands.
Expand Down

0 comments on commit 2c82fbe

Please sign in to comment.