-
Notifications
You must be signed in to change notification settings - Fork 10
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
Allow passthrough when --message-format
is specified
#15
Allow passthrough when --message-format
is specified
#15
Conversation
It would be interesting to check how this is done in similar cargo extensions. |
I tried to look around a bit for other cases of this, but didn't seem to find too many examples (most cargo extensions seem to operate fairly independently of the usual If it were provided like that, |
An alternative approach I just thought of would be to run the first command with the user's preference (defaulting to an unspecified message-format parameter), then immediately run it a second time with the JSON format we need. But this seems pretty hacky. |
I also took a look at |
For one other example in the wild, I looked at It looks like they use a custom iteratator over the flags to extract the ones they want to read or modify. There is a slight difference I think, since miri is also invoked as We might be able to borrow that flag-reading code, but otherwise it's not a great example since they don't actually care about the emitted binary paths. I'll investigate using a tee-like solution to see if we can avoid having to run cargo more than necessary. |
We can still parse the JSON output, even if Also: - remove some unused deps. - make sure we use eprintln so it doesn't interfere with any JSON output the user might want
Phew, this has been on backburner for a while but I finally got around to trying a different approach with I tested with
Best I can tell, this is the easiest solution that will work for both use cases, for now. Also,
I wonder if we will want to do that at some point, to support something like e.g. |
for (i, arg) in args.iter().enumerate() { | ||
if arg.starts_with("--message-format") { | ||
return { | ||
let arg = args.remove(i); |
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.
Wow, rustc understands that the immutable borrow from iter
is no longer needed at this point due to the return and not referencing arg
, and lets you call remove
(mutable borrow). Sweet!
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.
Yeah, I wasn't really expecting this to compile when I first wrote it, but was impressed that that it actually did!
Some(format) => { | ||
if !format.starts_with("json") { | ||
eprintln!("error: non-JSON `message-format` is not supported"); | ||
std::process::exit(1); |
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.
nit: std::
isn't needed since process
is already imported. (showed up as a lint in my editor)
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 neat, is that lint builtin to your editor or does it come from clippy or something? I've wanted something like this but haven't seen such a lint in my editor
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 use CLion with the IntelliJ-Rust plugin. The lint was recently added:
intellij-rust/intellij-rust#7410
https://intellij-rust.github.io/2022/04/25/changelog-169.html
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.
Cool, I wonder if there would be an appetite for something like that in rust-analyzer... they have some similar stuff with e.g. merging use
🤔
|
||
let messages = Message::parse_stream(stdout_reader) | ||
.collect::<io::Result<Vec<Message>>>() | ||
let buf_reader: &mut dyn BufRead = if self.message_format == Self::DEFAULT_MESSAGE_FORMAT { |
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.
Cool pattern!
I use rust-analyzer with VSCode and it requires
--message-format=json
when using a nonstandardcargo check
command.This change allows me to configure something like
to get proper diagnostics from
cargo 3ds check
in the editor.I think this used to work normally, before I changed to using
Message::parse_stream
and--message-format=json-render-diagnostics
.Tested both to verify JSON output is printed:
cargo 3ds check --message-format=json
cargo 3ds check --message-format json
cc @AzureMarker @Meziu