-
Notifications
You must be signed in to change notification settings - Fork 210
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
Use Cargo's JSON diagnostics for suggestions #767
Changes from all commits
793cc00
6644f82
84c33ad
b25073a
60a42dc
04427ae
0aa59c3
8d3a0ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -78,6 +78,8 @@ pub enum Error { | |||||||||||||||
UnableToReadOutput { source: io::Error }, | ||||||||||||||||
#[snafu(display("Unable to read crate information: {}", source))] | ||||||||||||||||
UnableToParseCrateInformation { source: ::serde_json::Error }, | ||||||||||||||||
#[snafu(display("Unable to parse cargo output: {}", source))] | ||||||||||||||||
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.
Suggested change
|
||||||||||||||||
UnableToParseCargoOutput { source: io::Error }, | ||||||||||||||||
#[snafu(display("Output was not valid UTF-8: {}", source))] | ||||||||||||||||
OutputNotUtf8 { source: string::FromUtf8Error }, | ||||||||||||||||
#[snafu(display("Output was missing"))] | ||||||||||||||||
|
@@ -146,8 +148,9 @@ impl Sandbox { | |||||||||||||||
.map(|entry| entry.path()) | ||||||||||||||||
.find(|path| path.extension() == Some(req.target.extension())); | ||||||||||||||||
|
||||||||||||||||
let stdout = vec_to_str(output.stdout)?; | ||||||||||||||||
let (stdout, stderr_tail) = parse_json_output(output.stdout)?; | ||||||||||||||||
let mut stderr = vec_to_str(output.stderr)?; | ||||||||||||||||
stderr.push_str(&stderr_tail); | ||||||||||||||||
|
||||||||||||||||
let mut code = match file { | ||||||||||||||||
Some(file) => read(&file)?.unwrap_or_else(String::new), | ||||||||||||||||
|
@@ -193,10 +196,14 @@ impl Sandbox { | |||||||||||||||
|
||||||||||||||||
let output = run_command_with_timeout(command)?; | ||||||||||||||||
|
||||||||||||||||
let (stdout, stderr_tail) = parse_json_output(output.stdout)?; | ||||||||||||||||
let mut stderr = vec_to_str(output.stderr)?; | ||||||||||||||||
stderr.push_str(&stderr_tail); | ||||||||||||||||
|
||||||||||||||||
Ok(ExecuteResponse { | ||||||||||||||||
success: output.status.success(), | ||||||||||||||||
stdout: vec_to_str(output.stdout)?, | ||||||||||||||||
stderr: vec_to_str(output.stderr)?, | ||||||||||||||||
stdout, | ||||||||||||||||
stderr, | ||||||||||||||||
}) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
|
@@ -526,8 +533,14 @@ fn build_execution_command( | |||||||||||||||
(Some(Wasm), _, _) => cmd.push("wasm"), | ||||||||||||||||
(Some(_), _, _) => cmd.push("rustc"), | ||||||||||||||||
(_, _, true) => cmd.push("test"), | ||||||||||||||||
(_, Library(_), _) => cmd.push("build"), | ||||||||||||||||
(_, _, _) => cmd.push("run"), | ||||||||||||||||
(_, Library(_), _) => { | ||||||||||||||||
cmd.push("build"); | ||||||||||||||||
cmd.push("--message-format=json"); | ||||||||||||||||
} | ||||||||||||||||
(_, _, _) => { | ||||||||||||||||
cmd.push("run"); | ||||||||||||||||
cmd.push("--message-format=json") | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if mode == Release { | ||||||||||||||||
|
@@ -571,6 +584,72 @@ fn build_execution_command( | |||||||||||||||
cmd | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
fn parse_json_output(output: Vec<u8>) -> Result<(String, String)> { | ||||||||||||||||
let mut composed_stderr_string = String::new(); | ||||||||||||||||
let mut composed_stdout_string = String::new(); | ||||||||||||||||
|
||||||||||||||||
let metadata_stream = cargo_metadata::Message::parse_stream(&output[..]); | ||||||||||||||||
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. I'd have expected you could elide the
Suggested change
|
||||||||||||||||
|
||||||||||||||||
for msg in metadata_stream { | ||||||||||||||||
let message = msg.context(UnableToParseCargoOutputSnafu)?; | ||||||||||||||||
|
||||||||||||||||
match message { | ||||||||||||||||
cargo_metadata::Message::TextLine(line) => { | ||||||||||||||||
composed_stdout_string.push_str(&(line + "\n")) | ||||||||||||||||
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. The same idea for using |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
cargo_metadata::Message::CompilerMessage(cargo_metadata::CompilerMessage { | ||||||||||||||||
message, | ||||||||||||||||
.. | ||||||||||||||||
}) => { | ||||||||||||||||
composed_stderr_string.push_str(&parse_diagnostic(message, true)); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
_ => {} | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
Ok((composed_stdout_string, composed_stderr_string)) | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
fn parse_diagnostic( | ||||||||||||||||
diagnostic: cargo_metadata::diagnostic::Diagnostic, | ||||||||||||||||
should_output_message: bool, | ||||||||||||||||
) -> String { | ||||||||||||||||
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. Instead of building a brand new string here, it'd be a bit more efficient to accept a |
||||||||||||||||
let mut diagnostic_string = String::new(); | ||||||||||||||||
|
||||||||||||||||
if should_output_message { | ||||||||||||||||
if let Some(rendered_msg) = diagnostic.rendered { | ||||||||||||||||
diagnostic_string.push_str(&rendered_msg); | ||||||||||||||||
} else { | ||||||||||||||||
diagnostic_string.push_str(&diagnostic.message); | ||||||||||||||||
} | ||||||||||||||||
Comment on lines
+622
to
+626
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. Not a required changed, but this could be something along the lines of this (maybe with some
Suggested change
|
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
for span in diagnostic.spans { | ||||||||||||||||
if span.file_name != "src/lib.rs" && span.file_name != "src/main.rs" { | ||||||||||||||||
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. My brain is slow at parsing this. Maybe we could use
Suggested change
|
||||||||||||||||
continue; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
let label = if let Some(label) = span.suggested_replacement { | ||||||||||||||||
label | ||||||||||||||||
} else { | ||||||||||||||||
continue; | ||||||||||||||||
}; | ||||||||||||||||
|
||||||||||||||||
diagnostic_string.push_str(&format!( | ||||||||||||||||
"\n[[Line {} Col {} - Line {} Col {}: {}]]", | ||||||||||||||||
span.line_start, span.column_start, span.line_end, span.column_end, label | ||||||||||||||||
)); | ||||||||||||||||
Comment on lines
+640
to
+643
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. Instead of creating the intermediate |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
for children in diagnostic.children { | ||||||||||||||||
diagnostic_string.push_str(&parse_diagnostic(children, false)); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
diagnostic_string | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
fn set_execution_environment( | ||||||||||||||||
cmd: &mut Command, | ||||||||||||||||
target: Option<CompileTarget>, | ||||||||||||||||
|
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.
This logic is tricky enough that I can't just glance at it and feel very confident about what it's doing. It'd be helpful to extract out the core logic here and write some unit tests to exercise the various
if
s. Something like this sketch: