-
-
Notifications
You must be signed in to change notification settings - Fork 456
Changed flycheck-rust-manifest-directory to use 'cargo metadata' #1422
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
Conversation
|
Thanks for reminding me about this. It looks like we don't have a choice, we have to call And thank you for writing the patch! I'll try to review it soon. |
|
I checked this change cause in the project like so it needs to be fixed! |
|
This error occurs in a Project which has both Is there any good idea to separate the directory where flycheck run command, from working directory? |
The working directory is the directory where flycheck runs the command. But for fixing #1397, the only thing that we need to change is to expand the names relatively to the workspace root, whereas previously they were relative to the working directory. We don't want to change the working directory. So the change should probably rather go to |
|
@fmdkdd |
|
I modified to use completely new way. This works well in my test directory, though I'm not sure there are no counter cases. |
|
@kngwyu Thanks! That's closer to what the fix should be. I agree that we should expand into an absolute path, only if the workspace root is present. But doing the expansion in I've pushed some changes in that direction on your branch. I've also fixed the tests, and added a new test for the workspace situation. It works with nightly and stable on my end. Can you check on your end if the patch still works? |
|
@fmdkdd Oh, I didn't consider |
|
@kngwyu No worries, that's what reviews are for. I've changed @cpitclaudel Can you take a look? |
cpitclaudel
left a comment
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.
Done! Thanks @kngwyu for this nice PR. I've left a few comments / questions
flycheck.el
Outdated
| (locate-dominating-file buffer-file-name "Cargo.toml"))) | ||
|
|
||
| (defun flycheck-rust-cargo-metadata () | ||
| "Exec 'cargo metadata' and return the result as parsed JSON object. |
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'd say Run rather than Exec, but that's nitpicking :)
flycheck.el
Outdated
| (defun flycheck-rust-cargo-metadata () | ||
| "Exec 'cargo metadata' and return the result as parsed JSON object. | ||
|
|
||
| Return nil if the command failed or if a parse error occured." |
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.
Do we really want to fail silently for failed calls to cargo metadata and for parse errors? (IIUC this is for backwards compatibility, but I'm afraid it'll create harder-to-debug bugs if things change unexpectedly)
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 think it's only because 'cargo' 's bug if parse error occurs, so we can allow error here, but I'm not sure.
flycheck.el
Outdated
| "Exec 'cargo metadata' and return the result as parsed JSON object. | ||
|
|
||
| Return nil if the command failed or if a parse error occured." | ||
| (let ((metadata-lines |
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.
-when-let here would remove the need for the and below
flycheck.el
Outdated
| (process-lines "cargo" "metadata" "--no-deps" "--format-version" "1")))) | ||
| (and metadata-lines | ||
| (ignore-errors | ||
| (json-read-from-string (string-join metadata-lines "")))))) |
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.
That call to string-join should use \n, not ``, I think (though it might not matter in this particular case?)
Besides, would it read nicer to use call-process and `json-read` instead of `process-lines` + `string-join` + `json-read-from-string`?
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.
In this case, we may just use flycheck-parse-json?
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.
@fmdkdd
flycheck-parse-json returns a list of parsed JSON lines, so isn't it too complex for this case, where what we want is only metadata ?
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.
It may be overkill, yes. Using call-process and json-read directly is fine :)
flycheck.el
Outdated
| Return nil if the workspace root does not exist (for Rust | ||
| versions inferior to 1.25)." | ||
| (let ((metadata (flycheck-rust-cargo-metadata))) | ||
| (let-alist metadata |
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.
No need to bind metadata here, I think ((let-alist (flycheck-rust-cargo-metadata) …))
|
Thanks for review. |
|
I simplified |
fmdkdd
left a comment
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.
Looking good! Just a small documentation change.
flycheck.el
Outdated
| (defun flycheck-rust-cargo-metadata () | ||
| "Run 'cargo metadata' and return the result as parsed JSON object. | ||
|
|
||
| Return nil if the command failed or if a parse error occured." |
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 guess this line can go now that we don't ignore errors anymore.
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.
Oh and it looks like the file is misformatted. Please run make format, which should take care of the issue.
|
Modified. |
flycheck.el
Outdated
| "Run 'cargo metadata' and return the result as parsed JSON object. | ||
|
|
||
| Return nil if the command failed or if a parse error occured." | ||
| We don't ignore any errors in this function." |
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 don't think we need to spell out that we don't do any error catching, as that is a detail of implementation. Anyone caring about implementation can see in the code that no error checking is done.
Please remove this line :)
|
Modified. |
Due to a recent change to cargo [1], in a workspace setting (multiple crates) filenames are now relative to the workspace root, whereas previously they were relative to the crate. This commit uses `cargo metadata` to figure out the workspace root, if it exists. If it doesn't, it fallbacks on the manifest directory. [1]: rust-lang/cargo#4788
|
Great! Thank you very much for this patch! 👏 |
This commit fixes #1397.
By the recent change of cargo(rust-lang/cargo#4788), flycheck can't detect error files in multi-crate project of Rust.
So I changed to use
workspace_rootincargo metadata's outputs, as working directory.If the user's cargo is a bit old and there's no 'workspace_root',
flycheck-rust-manifest-directorysearch a directory withCargo.toml, as ever.I've seen @fmdkdd's comment in rust-lang/cargo#4998, but I wrote this patch just because I need this, so please feel free to abort this PR if you don't like.