mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #5114
5114: Cleanup cargo process handling in flycheck r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
11f31ae4c3
3 changed files with 137 additions and 103 deletions
|
@ -5,8 +5,9 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt,
|
||||||
io::{self, BufReader},
|
io::{self, BufReader},
|
||||||
|
ops,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::{Command, Stdio},
|
process::{self, Command, Stdio},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -49,8 +50,8 @@ impl fmt::Display for FlycheckConfig {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FlycheckHandle {
|
pub struct FlycheckHandle {
|
||||||
// XXX: drop order is significant
|
// XXX: drop order is significant
|
||||||
cmd_send: Sender<Restart>,
|
sender: Sender<Restart>,
|
||||||
handle: jod_thread::JoinHandle,
|
thread: jod_thread::JoinHandle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FlycheckHandle {
|
impl FlycheckHandle {
|
||||||
|
@ -59,16 +60,15 @@ impl FlycheckHandle {
|
||||||
config: FlycheckConfig,
|
config: FlycheckConfig,
|
||||||
workspace_root: PathBuf,
|
workspace_root: PathBuf,
|
||||||
) -> FlycheckHandle {
|
) -> FlycheckHandle {
|
||||||
let (cmd_send, cmd_recv) = unbounded::<Restart>();
|
let actor = FlycheckActor::new(sender, config, workspace_root);
|
||||||
let handle = jod_thread::spawn(move || {
|
let (sender, receiver) = unbounded::<Restart>();
|
||||||
FlycheckActor::new(sender, config, workspace_root).run(cmd_recv);
|
let thread = jod_thread::spawn(move || actor.run(receiver));
|
||||||
});
|
FlycheckHandle { sender, thread }
|
||||||
FlycheckHandle { cmd_send, handle }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Schedule a re-start of the cargo check worker.
|
/// Schedule a re-start of the cargo check worker.
|
||||||
pub fn update(&self) {
|
pub fn update(&self) {
|
||||||
self.cmd_send.send(Restart).unwrap();
|
self.sender.send(Restart).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ pub enum Message {
|
||||||
pub enum Progress {
|
pub enum Progress {
|
||||||
DidStart,
|
DidStart,
|
||||||
DidCheckCrate(String),
|
DidCheckCrate(String),
|
||||||
DidFinish,
|
DidFinish(io::Result<()>),
|
||||||
DidCancel,
|
DidCancel,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,8 +100,7 @@ struct FlycheckActor {
|
||||||
/// doesn't provide a way to read sub-process output without blocking, so we
|
/// doesn't provide a way to read sub-process output without blocking, so we
|
||||||
/// have to wrap sub-processes output handling in a thread and pass messages
|
/// have to wrap sub-processes output handling in a thread and pass messages
|
||||||
/// back over a channel.
|
/// back over a channel.
|
||||||
// XXX: drop order is significant
|
cargo_handle: Option<CargoHandle>,
|
||||||
check_process: Option<(Receiver<cargo_metadata::Message>, jod_thread::JoinHandle)>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Event {
|
enum Event {
|
||||||
|
@ -115,29 +114,36 @@ impl FlycheckActor {
|
||||||
config: FlycheckConfig,
|
config: FlycheckConfig,
|
||||||
workspace_root: PathBuf,
|
workspace_root: PathBuf,
|
||||||
) -> FlycheckActor {
|
) -> FlycheckActor {
|
||||||
FlycheckActor { sender, config, workspace_root, check_process: None }
|
FlycheckActor { sender, config, workspace_root, cargo_handle: None }
|
||||||
}
|
}
|
||||||
fn next_event(&self, inbox: &Receiver<Restart>) -> Option<Event> {
|
fn next_event(&self, inbox: &Receiver<Restart>) -> Option<Event> {
|
||||||
let check_chan = self.check_process.as_ref().map(|(chan, _thread)| chan);
|
let check_chan = self.cargo_handle.as_ref().map(|cargo| &cargo.receiver);
|
||||||
select! {
|
select! {
|
||||||
recv(inbox) -> msg => msg.ok().map(Event::Restart),
|
recv(inbox) -> msg => msg.ok().map(Event::Restart),
|
||||||
recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())),
|
recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn run(&mut self, inbox: Receiver<Restart>) {
|
fn run(mut self, inbox: Receiver<Restart>) {
|
||||||
while let Some(event) = self.next_event(&inbox) {
|
while let Some(event) = self.next_event(&inbox) {
|
||||||
match event {
|
match event {
|
||||||
Event::Restart(Restart) => {
|
Event::Restart(Restart) => {
|
||||||
while let Ok(Restart) = inbox.recv_timeout(Duration::from_millis(50)) {}
|
while let Ok(Restart) = inbox.recv_timeout(Duration::from_millis(50)) {}
|
||||||
|
|
||||||
self.cancel_check_process();
|
self.cancel_check_process();
|
||||||
self.check_process = Some(self.start_check_process());
|
|
||||||
self.send(Message::Progress(Progress::DidStart));
|
let mut command = self.check_command();
|
||||||
|
command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null());
|
||||||
|
if let Ok(child) = command.spawn().map(JodChild) {
|
||||||
|
self.cargo_handle = Some(CargoHandle::spawn(child));
|
||||||
|
self.send(Message::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
|
||||||
// avoid busy-waiting.
|
// avoid busy-waiting.
|
||||||
assert!(self.check_process.take().is_some());
|
let cargo_handle = self.cargo_handle.take().unwrap();
|
||||||
self.send(Message::Progress(Progress::DidFinish));
|
let res = cargo_handle.join();
|
||||||
|
self.send(Message::Progress(Progress::DidFinish(res)));
|
||||||
}
|
}
|
||||||
Event::CheckEvent(Some(message)) => match message {
|
Event::CheckEvent(Some(message)) => match message {
|
||||||
cargo_metadata::Message::CompilerArtifact(msg) => {
|
cargo_metadata::Message::CompilerArtifact(msg) => {
|
||||||
|
@ -162,11 +168,11 @@ impl FlycheckActor {
|
||||||
self.cancel_check_process();
|
self.cancel_check_process();
|
||||||
}
|
}
|
||||||
fn cancel_check_process(&mut self) {
|
fn cancel_check_process(&mut self) {
|
||||||
if self.check_process.take().is_some() {
|
if self.cargo_handle.take().is_some() {
|
||||||
self.send(Message::Progress(Progress::DidCancel));
|
self.send(Message::Progress(Progress::DidCancel));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn start_check_process(&self) -> (Receiver<cargo_metadata::Message>, jod_thread::JoinHandle) {
|
fn check_command(&self) -> Command {
|
||||||
let mut cmd = match &self.config {
|
let mut cmd = match &self.config {
|
||||||
FlycheckConfig::CargoCommand {
|
FlycheckConfig::CargoCommand {
|
||||||
command,
|
command,
|
||||||
|
@ -198,33 +204,7 @@ impl FlycheckActor {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
cmd.current_dir(&self.workspace_root);
|
cmd.current_dir(&self.workspace_root);
|
||||||
|
cmd
|
||||||
let (message_send, message_recv) = unbounded();
|
|
||||||
let thread = jod_thread::spawn(move || {
|
|
||||||
// If we trigger an error here, we will do so in the loop instead,
|
|
||||||
// which will break out of the loop, and continue the shutdown
|
|
||||||
let res = run_cargo(cmd, &mut |message| {
|
|
||||||
// Skip certain kinds of messages to only spend time on what's useful
|
|
||||||
match &message {
|
|
||||||
cargo_metadata::Message::CompilerArtifact(artifact) if artifact.fresh => {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
cargo_metadata::Message::BuildScriptExecuted(_)
|
|
||||||
| cargo_metadata::Message::Unknown => return true,
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if the send channel was closed, we want to shutdown
|
|
||||||
message_send.send(message).is_ok()
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Err(err) = res {
|
|
||||||
// FIXME: make the `message_send` to be `Sender<Result<CheckEvent, CargoError>>`
|
|
||||||
// to display user-caused misconfiguration errors instead of just logging them here
|
|
||||||
log::error!("Cargo watcher failed {:?}", err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
(message_recv, thread)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send(&self, check_task: Message) {
|
fn send(&self, check_task: Message) {
|
||||||
|
@ -232,54 +212,104 @@ impl FlycheckActor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_cargo(
|
struct CargoHandle {
|
||||||
mut command: Command,
|
child: JodChild,
|
||||||
on_message: &mut dyn FnMut(cargo_metadata::Message) -> bool,
|
#[allow(unused)]
|
||||||
) -> io::Result<()> {
|
thread: jod_thread::JoinHandle<io::Result<bool>>,
|
||||||
let mut child =
|
receiver: Receiver<cargo_metadata::Message>,
|
||||||
command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null()).spawn()?;
|
}
|
||||||
|
|
||||||
// We manually read a line at a time, instead of using serde's
|
impl CargoHandle {
|
||||||
// stream deserializers, because the deserializer cannot recover
|
fn spawn(mut child: JodChild) -> CargoHandle {
|
||||||
// from an error, resulting in it getting stuck, because we try to
|
let child_stdout = child.stdout.take().unwrap();
|
||||||
// be resillient against failures.
|
let (sender, receiver) = unbounded();
|
||||||
//
|
let actor = CargoActor::new(child_stdout, sender);
|
||||||
// Because cargo only outputs one JSON object per line, we can
|
let thread = jod_thread::spawn(move || actor.run());
|
||||||
// simply skip a line if it doesn't parse, which just ignores any
|
CargoHandle { child, thread, receiver }
|
||||||
// erroneus output.
|
}
|
||||||
let stdout = BufReader::new(child.stdout.take().unwrap());
|
fn join(mut self) -> io::Result<()> {
|
||||||
let mut read_at_least_one_message = false;
|
// It is okay to ignore the result, as it only errors if the process is already dead
|
||||||
for message in cargo_metadata::Message::parse_stream(stdout) {
|
let _ = self.child.kill();
|
||||||
let message = match message {
|
let exit_status = self.child.wait()?;
|
||||||
Ok(message) => message,
|
let read_at_least_one_message = self.thread.join()?;
|
||||||
Err(err) => {
|
if !exit_status.success() && !read_at_least_one_message {
|
||||||
log::error!("Invalid json from cargo check, ignoring ({})", err);
|
// FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
|
||||||
continue;
|
// https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
|
||||||
}
|
return Err(io::Error::new(
|
||||||
};
|
io::ErrorKind::Other,
|
||||||
|
format!(
|
||||||
read_at_least_one_message = true;
|
"Cargo watcher failed,the command produced no valid metadata (exit code: {:?})",
|
||||||
|
exit_status
|
||||||
if !on_message(message) {
|
),
|
||||||
break;
|
));
|
||||||
}
|
}
|
||||||
}
|
Ok(())
|
||||||
|
}
|
||||||
// It is okay to ignore the result, as it only errors if the process is already dead
|
}
|
||||||
let _ = child.kill();
|
|
||||||
|
struct CargoActor {
|
||||||
let exit_status = child.wait()?;
|
child_stdout: process::ChildStdout,
|
||||||
if !exit_status.success() && !read_at_least_one_message {
|
sender: Sender<cargo_metadata::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(
|
impl CargoActor {
|
||||||
io::ErrorKind::Other,
|
fn new(
|
||||||
format!(
|
child_stdout: process::ChildStdout,
|
||||||
"the command produced no valid metadata (exit code: {:?}): {:?}",
|
sender: Sender<cargo_metadata::Message>,
|
||||||
exit_status, command
|
) -> CargoActor {
|
||||||
),
|
CargoActor { child_stdout, sender }
|
||||||
));
|
}
|
||||||
}
|
fn run(self) -> io::Result<bool> {
|
||||||
|
// We manually read a line at a time, instead of using serde's
|
||||||
Ok(())
|
// stream deserializers, because the deserializer cannot recover
|
||||||
|
// from an error, resulting in it getting stuck, because we try to
|
||||||
|
// be resilient against failures.
|
||||||
|
//
|
||||||
|
// Because cargo only outputs one JSON object per line, we can
|
||||||
|
// simply skip a line if it doesn't parse, which just ignores any
|
||||||
|
// erroneus output.
|
||||||
|
let stdout = BufReader::new(self.child_stdout);
|
||||||
|
let mut read_at_least_one_message = false;
|
||||||
|
for message in cargo_metadata::Message::parse_stream(stdout) {
|
||||||
|
let message = match message {
|
||||||
|
Ok(message) => message,
|
||||||
|
Err(err) => {
|
||||||
|
log::error!("Invalid json from cargo check, ignoring ({})", err);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
read_at_least_one_message = true;
|
||||||
|
|
||||||
|
// Skip certain kinds of messages to only spend time on what's useful
|
||||||
|
match &message {
|
||||||
|
cargo_metadata::Message::CompilerArtifact(artifact) if artifact.fresh => (),
|
||||||
|
cargo_metadata::Message::BuildScriptExecuted(_)
|
||||||
|
| cargo_metadata::Message::Unknown => (),
|
||||||
|
_ => self.sender.send(message).unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(read_at_least_one_message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct JodChild(process::Child);
|
||||||
|
|
||||||
|
impl ops::Deref for JodChild {
|
||||||
|
type Target = process::Child;
|
||||||
|
fn deref(&self) -> &process::Child {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ops::DerefMut for JodChild {
|
||||||
|
fn deref_mut(&mut self) -> &mut process::Child {
|
||||||
|
&mut self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for JodChild {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let _ = self.0.kill();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -216,7 +216,11 @@ impl GlobalState {
|
||||||
flycheck::Progress::DidCheckCrate(target) => {
|
flycheck::Progress::DidCheckCrate(target) => {
|
||||||
(Progress::Report, Some(target))
|
(Progress::Report, Some(target))
|
||||||
}
|
}
|
||||||
flycheck::Progress::DidFinish | flycheck::Progress::DidCancel => {
|
flycheck::Progress::DidCancel => (Progress::End, None),
|
||||||
|
flycheck::Progress::DidFinish(result) => {
|
||||||
|
if let Err(err) = result {
|
||||||
|
log::error!("cargo check failed: {}", err)
|
||||||
|
}
|
||||||
(Progress::End, None)
|
(Progress::End, None)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -10,7 +10,7 @@ mod include;
|
||||||
|
|
||||||
use std::convert::{TryFrom, TryInto};
|
use std::convert::{TryFrom, TryInto};
|
||||||
|
|
||||||
use crossbeam_channel::{select, unbounded, Receiver};
|
use crossbeam_channel::{select, unbounded, Receiver, Sender};
|
||||||
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
|
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
|
||||||
use paths::{AbsPath, AbsPathBuf};
|
use paths::{AbsPath, AbsPathBuf};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
@ -22,8 +22,8 @@ use crate::include::Include;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct NotifyHandle {
|
pub struct NotifyHandle {
|
||||||
// Relative order of fields below is significant.
|
// Relative order of fields below is significant.
|
||||||
sender: crossbeam_channel::Sender<Message>,
|
sender: Sender<Message>,
|
||||||
_thread: jod_thread::JoinHandle,
|
thread: jod_thread::JoinHandle,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -37,7 +37,7 @@ impl loader::Handle for NotifyHandle {
|
||||||
let actor = NotifyActor::new(sender);
|
let actor = NotifyActor::new(sender);
|
||||||
let (sender, receiver) = unbounded::<Message>();
|
let (sender, receiver) = unbounded::<Message>();
|
||||||
let thread = jod_thread::spawn(move || actor.run(receiver));
|
let thread = jod_thread::spawn(move || actor.run(receiver));
|
||||||
NotifyHandle { sender, _thread: thread }
|
NotifyHandle { sender, thread }
|
||||||
}
|
}
|
||||||
fn set_config(&mut self, config: loader::Config) {
|
fn set_config(&mut self, config: loader::Config) {
|
||||||
self.sender.send(Message::Config(config)).unwrap()
|
self.sender.send(Message::Config(config)).unwrap()
|
||||||
|
|
Loading…
Reference in a new issue