mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Show cargo check failures to the user
This commit is contained in:
parent
079e9fe496
commit
d14c9be321
2 changed files with 61 additions and 70 deletions
|
@ -2,17 +2,12 @@
|
||||||
//! another compatible command (f.x. clippy) in a background thread and provide
|
//! another compatible command (f.x. clippy) in a background thread and provide
|
||||||
//! LSP diagnostics based on the output of the command.
|
//! LSP diagnostics based on the output of the command.
|
||||||
|
|
||||||
use std::{
|
use std::{fmt, io, process::Command, time::Duration};
|
||||||
fmt,
|
|
||||||
io::{self, BufRead, BufReader},
|
|
||||||
process::{self, Command, Stdio},
|
|
||||||
time::Duration,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
|
use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
|
||||||
use paths::AbsPathBuf;
|
use paths::AbsPathBuf;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use stdx::JodChild;
|
use stdx::process::streaming_output;
|
||||||
|
|
||||||
pub use cargo_metadata::diagnostic::{
|
pub use cargo_metadata::diagnostic::{
|
||||||
Applicability, Diagnostic, DiagnosticCode, DiagnosticLevel, DiagnosticSpan,
|
Applicability, Diagnostic, DiagnosticCode, DiagnosticLevel, DiagnosticSpan,
|
||||||
|
@ -162,13 +157,10 @@ impl FlycheckActor {
|
||||||
|
|
||||||
self.cancel_check_process();
|
self.cancel_check_process();
|
||||||
|
|
||||||
let mut command = self.check_command();
|
let command = self.check_command();
|
||||||
tracing::info!("restart flycheck {:?}", command);
|
tracing::info!("restart flycheck {:?}", command);
|
||||||
command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null());
|
self.cargo_handle = Some(CargoHandle::spawn(command));
|
||||||
if let Ok(child) = command.spawn().map(JodChild) {
|
self.progress(Progress::DidStart);
|
||||||
self.cargo_handle = Some(CargoHandle::spawn(child));
|
|
||||||
self.progress(Progress::DidStart);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Event::CheckEvent(None) => {
|
Event::CheckEvent(None) => {
|
||||||
// Watcher finished, replace it with a never channel to
|
// Watcher finished, replace it with a never channel to
|
||||||
|
@ -258,53 +250,37 @@ impl FlycheckActor {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CargoHandle {
|
struct CargoHandle {
|
||||||
child: JodChild,
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
thread: jod_thread::JoinHandle<bool>,
|
thread: jod_thread::JoinHandle<io::Result<()>>,
|
||||||
receiver: Receiver<CargoMessage>,
|
receiver: Receiver<CargoMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CargoHandle {
|
impl CargoHandle {
|
||||||
fn spawn(mut child: JodChild) -> CargoHandle {
|
fn spawn(command: Command) -> CargoHandle {
|
||||||
let child_stdout = child.stdout.take().unwrap();
|
|
||||||
let (sender, receiver) = unbounded();
|
let (sender, receiver) = unbounded();
|
||||||
let actor = CargoActor::new(child_stdout, sender);
|
let actor = CargoActor::new(sender);
|
||||||
let thread = jod_thread::Builder::new()
|
let thread = jod_thread::Builder::new()
|
||||||
.name("CargoHandle".to_owned())
|
.name("CargoHandle".to_owned())
|
||||||
.spawn(move || actor.run())
|
.spawn(move || actor.run(command))
|
||||||
.expect("failed to spawn thread");
|
.expect("failed to spawn thread");
|
||||||
CargoHandle { child, thread, receiver }
|
CargoHandle { thread, receiver }
|
||||||
}
|
}
|
||||||
fn join(mut self) -> io::Result<()> {
|
|
||||||
// It is okay to ignore the result, as it only errors if the process is already dead
|
fn join(self) -> io::Result<()> {
|
||||||
let _ = self.child.kill();
|
self.thread.join()
|
||||||
let exit_status = self.child.wait()?;
|
|
||||||
let read_at_least_one_message = self.thread.join();
|
|
||||||
if !exit_status.success() && !read_at_least_one_message {
|
|
||||||
// FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
|
|
||||||
// https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
|
|
||||||
return Err(io::Error::new(
|
|
||||||
io::ErrorKind::Other,
|
|
||||||
format!(
|
|
||||||
"Cargo watcher failed, the command produced no valid metadata (exit code: {:?})",
|
|
||||||
exit_status
|
|
||||||
),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct CargoActor {
|
struct CargoActor {
|
||||||
child_stdout: process::ChildStdout,
|
|
||||||
sender: Sender<CargoMessage>,
|
sender: Sender<CargoMessage>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CargoActor {
|
impl CargoActor {
|
||||||
fn new(child_stdout: process::ChildStdout, sender: Sender<CargoMessage>) -> CargoActor {
|
fn new(sender: Sender<CargoMessage>) -> CargoActor {
|
||||||
CargoActor { child_stdout, sender }
|
CargoActor { sender }
|
||||||
}
|
}
|
||||||
fn run(self) -> bool {
|
|
||||||
|
fn run(self, command: Command) -> io::Result<()> {
|
||||||
// We manually read a line at a time, instead of using serde's
|
// We manually read a line at a time, instead of using serde's
|
||||||
// stream deserializers, because the deserializer cannot recover
|
// stream deserializers, because the deserializer cannot recover
|
||||||
// from an error, resulting in it getting stuck, because we try to
|
// from an error, resulting in it getting stuck, because we try to
|
||||||
|
@ -313,41 +289,53 @@ impl CargoActor {
|
||||||
// Because cargo only outputs one JSON object per line, we can
|
// Because cargo only outputs one JSON object per line, we can
|
||||||
// simply skip a line if it doesn't parse, which just ignores any
|
// simply skip a line if it doesn't parse, which just ignores any
|
||||||
// erroneus output.
|
// erroneus output.
|
||||||
let stdout = BufReader::new(self.child_stdout);
|
|
||||||
|
let mut error = String::new();
|
||||||
let mut read_at_least_one_message = false;
|
let mut read_at_least_one_message = false;
|
||||||
for message in stdout.lines() {
|
let output = streaming_output(
|
||||||
let message = match message {
|
command,
|
||||||
Ok(message) => message,
|
&mut |line| {
|
||||||
Err(err) => {
|
read_at_least_one_message = true;
|
||||||
tracing::error!("Invalid json from cargo check, ignoring ({})", err);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
read_at_least_one_message = true;
|
// Try to deserialize a message from Cargo or Rustc.
|
||||||
|
let mut deserializer = serde_json::Deserializer::from_str(&line);
|
||||||
// Try to deserialize a message from Cargo or Rustc.
|
deserializer.disable_recursion_limit();
|
||||||
let mut deserializer = serde_json::Deserializer::from_str(&message);
|
if let Ok(message) = JsonMessage::deserialize(&mut deserializer) {
|
||||||
deserializer.disable_recursion_limit();
|
match message {
|
||||||
if let Ok(message) = JsonMessage::deserialize(&mut deserializer) {
|
// Skip certain kinds of messages to only spend time on what's useful
|
||||||
match message {
|
JsonMessage::Cargo(message) => match message {
|
||||||
// Skip certain kinds of messages to only spend time on what's useful
|
cargo_metadata::Message::CompilerArtifact(artifact)
|
||||||
JsonMessage::Cargo(message) => match message {
|
if !artifact.fresh =>
|
||||||
cargo_metadata::Message::CompilerArtifact(artifact) if !artifact.fresh => {
|
{
|
||||||
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
|
self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
|
||||||
|
}
|
||||||
|
cargo_metadata::Message::CompilerMessage(msg) => {
|
||||||
|
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
},
|
||||||
|
JsonMessage::Rustc(message) => {
|
||||||
|
self.sender.send(CargoMessage::Diagnostic(message)).unwrap();
|
||||||
}
|
}
|
||||||
cargo_metadata::Message::CompilerMessage(msg) => {
|
|
||||||
self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
JsonMessage::Rustc(message) => {
|
|
||||||
self.sender.send(CargoMessage::Diagnostic(message)).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
&mut |line| {
|
||||||
|
error.push_str(line);
|
||||||
|
error.push('\n');
|
||||||
|
},
|
||||||
|
);
|
||||||
|
match output {
|
||||||
|
Ok(_) if read_at_least_one_message => Ok(()),
|
||||||
|
Ok(output) if output.status.success() => {
|
||||||
|
Err(io::Error::new(io::ErrorKind::Other, format!(
|
||||||
|
"Cargo watcher failed, the command produced no valid metadata (exit code: {:?})",
|
||||||
|
output.status
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
|
Ok(_) => Err(io::Error::new(io::ErrorKind::Other, error)),
|
||||||
|
Err(e) => Err(e),
|
||||||
}
|
}
|
||||||
read_at_least_one_message
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -394,7 +394,10 @@ impl GlobalState {
|
||||||
flycheck::Progress::DidCancel => (Progress::End, None),
|
flycheck::Progress::DidCancel => (Progress::End, None),
|
||||||
flycheck::Progress::DidFinish(result) => {
|
flycheck::Progress::DidFinish(result) => {
|
||||||
if let Err(err) = result {
|
if let Err(err) = result {
|
||||||
tracing::error!("cargo check failed: {}", err)
|
self.show_message(
|
||||||
|
lsp_types::MessageType::Error,
|
||||||
|
format!("cargo check failed: {}", err),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
(Progress::End, None)
|
(Progress::End, None)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue