Skip to content

Commit d14c9be

Browse files
committed
Show cargo check failures to the user
1 parent 079e9fe commit d14c9be

File tree

2 files changed

+61
-70
lines changed

2 files changed

+61
-70
lines changed

crates/flycheck/src/lib.rs

Lines changed: 57 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
//! another compatible command (f.x. clippy) in a background thread and provide
33
//! LSP diagnostics based on the output of the command.
44
5-
use std::{
6-
fmt,
7-
io::{self, BufRead, BufReader},
8-
process::{self, Command, Stdio},
9-
time::Duration,
10-
};
5+
use std::{fmt, io, process::Command, time::Duration};
116

127
use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
138
use paths::AbsPathBuf;
149
use serde::Deserialize;
15-
use stdx::JodChild;
10+
use stdx::process::streaming_output;
1611

1712
pub use cargo_metadata::diagnostic::{
1813
Applicability, Diagnostic, DiagnosticCode, DiagnosticLevel, DiagnosticSpan,
@@ -162,13 +157,10 @@ impl FlycheckActor {
162157

163158
self.cancel_check_process();
164159

165-
let mut command = self.check_command();
160+
let command = self.check_command();
166161
tracing::info!("restart flycheck {:?}", command);
167-
command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null());
168-
if let Ok(child) = command.spawn().map(JodChild) {
169-
self.cargo_handle = Some(CargoHandle::spawn(child));
170-
self.progress(Progress::DidStart);
171-
}
162+
self.cargo_handle = Some(CargoHandle::spawn(command));
163+
self.progress(Progress::DidStart);
172164
}
173165
Event::CheckEvent(None) => {
174166
// Watcher finished, replace it with a never channel to
@@ -258,53 +250,37 @@ impl FlycheckActor {
258250
}
259251

260252
struct CargoHandle {
261-
child: JodChild,
262253
#[allow(unused)]
263-
thread: jod_thread::JoinHandle<bool>,
254+
thread: jod_thread::JoinHandle<io::Result<()>>,
264255
receiver: Receiver<CargoMessage>,
265256
}
266257

267258
impl CargoHandle {
268-
fn spawn(mut child: JodChild) -> CargoHandle {
269-
let child_stdout = child.stdout.take().unwrap();
259+
fn spawn(command: Command) -> CargoHandle {
270260
let (sender, receiver) = unbounded();
271-
let actor = CargoActor::new(child_stdout, sender);
261+
let actor = CargoActor::new(sender);
272262
let thread = jod_thread::Builder::new()
273263
.name("CargoHandle".to_owned())
274-
.spawn(move || actor.run())
264+
.spawn(move || actor.run(command))
275265
.expect("failed to spawn thread");
276-
CargoHandle { child, thread, receiver }
266+
CargoHandle { thread, receiver }
277267
}
278-
fn join(mut self) -> io::Result<()> {
279-
// It is okay to ignore the result, as it only errors if the process is already dead
280-
let _ = self.child.kill();
281-
let exit_status = self.child.wait()?;
282-
let read_at_least_one_message = self.thread.join();
283-
if !exit_status.success() && !read_at_least_one_message {
284-
// FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
285-
// https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
286-
return Err(io::Error::new(
287-
io::ErrorKind::Other,
288-
format!(
289-
"Cargo watcher failed, the command produced no valid metadata (exit code: {:?})",
290-
exit_status
291-
),
292-
));
293-
}
294-
Ok(())
268+
269+
fn join(self) -> io::Result<()> {
270+
self.thread.join()
295271
}
296272
}
297273

298274
struct CargoActor {
299-
child_stdout: process::ChildStdout,
300275
sender: Sender<CargoMessage>,
301276
}
302277

303278
impl CargoActor {
304-
fn new(child_stdout: process::ChildStdout, sender: Sender<CargoMessage>) -> CargoActor {
305-
CargoActor { child_stdout, sender }
279+
fn new(sender: Sender<CargoMessage>) -> CargoActor {
280+
CargoActor { sender }
306281
}
307-
fn run(self) -> bool {
282+
283+
fn run(self, command: Command) -> io::Result<()> {
308284
// We manually read a line at a time, instead of using serde's
309285
// stream deserializers, because the deserializer cannot recover
310286
// from an error, resulting in it getting stuck, because we try to
@@ -313,41 +289,53 @@ impl CargoActor {
313289
// Because cargo only outputs one JSON object per line, we can
314290
// simply skip a line if it doesn't parse, which just ignores any
315291
// erroneus output.
316-
let stdout = BufReader::new(self.child_stdout);
317-
let mut read_at_least_one_message = false;
318-
for message in stdout.lines() {
319-
let message = match message {
320-
Ok(message) => message,
321-
Err(err) => {
322-
tracing::error!("Invalid json from cargo check, ignoring ({})", err);
323-
continue;
324-
}
325-
};
326292

327-
read_at_least_one_message = true;
293+
let mut error = String::new();
294+
let mut read_at_least_one_message = false;
295+
let output = streaming_output(
296+
command,
297+
&mut |line| {
298+
read_at_least_one_message = true;
328299

329-
// Try to deserialize a message from Cargo or Rustc.
330-
let mut deserializer = serde_json::Deserializer::from_str(&message);
331-
deserializer.disable_recursion_limit();
332-
if let Ok(message) = JsonMessage::deserialize(&mut deserializer) {
333-
match message {
334-
// Skip certain kinds of messages to only spend time on what's useful
335-
JsonMessage::Cargo(message) => match message {
336-
cargo_metadata::Message::CompilerArtifact(artifact) if !artifact.fresh => {
337-
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
300+
// Try to deserialize a message from Cargo or Rustc.
301+
let mut deserializer = serde_json::Deserializer::from_str(&line);
302+
deserializer.disable_recursion_limit();
303+
if let Ok(message) = JsonMessage::deserialize(&mut deserializer) {
304+
match message {
305+
// Skip certain kinds of messages to only spend time on what's useful
306+
JsonMessage::Cargo(message) => match message {
307+
cargo_metadata::Message::CompilerArtifact(artifact)
308+
if !artifact.fresh =>
309+
{
310+
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
311+
}
312+
cargo_metadata::Message::CompilerMessage(msg) => {
313+
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
314+
}
315+
_ => (),
316+
},
317+
JsonMessage::Rustc(message) => {
318+
self.sender.send(CargoMessage::Diagnostic(message)).unwrap();
338319
}
339-
cargo_metadata::Message::CompilerMessage(msg) => {
340-
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
341-
}
342-
_ => (),
343-
},
344-
JsonMessage::Rustc(message) => {
345-
self.sender.send(CargoMessage::Diagnostic(message)).unwrap();
346320
}
347321
}
322+
},
323+
&mut |line| {
324+
error.push_str(line);
325+
error.push('\n');
326+
},
327+
);
328+
match output {
329+
Ok(_) if read_at_least_one_message => Ok(()),
330+
Ok(output) if output.status.success() => {
331+
Err(io::Error::new(io::ErrorKind::Other, format!(
332+
"Cargo watcher failed, the command produced no valid metadata (exit code: {:?})",
333+
output.status
334+
)))
348335
}
336+
Ok(_) => Err(io::Error::new(io::ErrorKind::Other, error)),
337+
Err(e) => Err(e),
349338
}
350-
read_at_least_one_message
351339
}
352340
}
353341

crates/rust-analyzer/src/main_loop.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,10 @@ impl GlobalState {
394394
flycheck::Progress::DidCancel => (Progress::End, None),
395395
flycheck::Progress::DidFinish(result) => {
396396
if let Err(err) = result {
397-
tracing::error!("cargo check failed: {}", err)
397+
self.show_message(
398+
lsp_types::MessageType::Error,
399+
format!("cargo check failed: {}", err),
400+
);
398401
}
399402
(Progress::End, None)
400403
}

0 commit comments

Comments
 (0)