1 //! Flycheck provides the functionality needed to run `cargo check` or
2 //! another compatible command (f.x. clippy) in a background thread and provide
3 //! LSP diagnostics based on the output of the command.
5 #![warn(rust_2018_idioms, unused_lifetimes, semicolon_in_expressions_from_macros)]
9 process::{ChildStderr, ChildStdout, Command, Stdio},
13 use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
14 use paths::AbsPathBuf;
15 use rustc_hash::FxHashMap;
16 use serde::Deserialize;
17 use stdx::{process::streaming_output, JodChild};
19 pub use cargo_metadata::diagnostic::{
20 Applicability, Diagnostic, DiagnosticCode, DiagnosticLevel, DiagnosticSpan,
21 DiagnosticSpanMacroExpansion,
24 #[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
25 pub enum InvocationStrategy {
31 #[derive(Clone, Debug, Default, PartialEq, Eq)]
32 pub enum InvocationLocation {
38 #[derive(Clone, Debug, PartialEq, Eq)]
39 pub enum FlycheckConfig {
42 target_triple: Option<String>,
44 no_default_features: bool,
46 features: Vec<String>,
47 extra_args: Vec<String>,
48 extra_env: FxHashMap<String, String>,
53 extra_env: FxHashMap<String, String>,
54 invocation_strategy: InvocationStrategy,
55 invocation_location: InvocationLocation,
59 impl fmt::Display for FlycheckConfig {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62 FlycheckConfig::CargoCommand { command, .. } => write!(f, "cargo {}", command),
63 FlycheckConfig::CustomCommand { command, args, .. } => {
64 write!(f, "{} {}", command, args.join(" "))
70 /// Flycheck wraps the shared state and communication machinery used for
71 /// running `cargo check` (or other compatible command) and providing
72 /// diagnostics based on the output.
73 /// The spawned thread is shut down when this struct is dropped.
75 pub struct FlycheckHandle {
76 // XXX: drop order is significant
77 sender: Sender<Restart>,
78 _thread: jod_thread::JoinHandle,
85 sender: Box<dyn Fn(Message) + Send>,
86 config: FlycheckConfig,
87 workspace_root: AbsPathBuf,
89 let actor = FlycheckActor::new(id, sender, config, workspace_root);
90 let (sender, receiver) = unbounded::<Restart>();
91 let thread = jod_thread::Builder::new()
92 .name("Flycheck".to_owned())
93 .spawn(move || actor.run(receiver))
94 .expect("failed to spawn thread");
95 FlycheckHandle { id, sender, _thread: thread }
98 /// Schedule a re-start of the cargo check worker.
99 pub fn restart(&self) {
100 self.sender.send(Restart::Yes).unwrap();
103 /// Stop this cargo check worker.
104 pub fn cancel(&self) {
105 self.sender.send(Restart::No).unwrap();
108 pub fn id(&self) -> usize {
114 /// Request adding a diagnostic with fixes included to a file
115 AddDiagnostic { id: usize, workspace_root: AbsPathBuf, diagnostic: Diagnostic },
117 /// Request check progress notification to client
119 /// Flycheck instance ID
125 impl fmt::Debug for Message {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128 Message::AddDiagnostic { id, workspace_root, diagnostic } => f
129 .debug_struct("AddDiagnostic")
131 .field("workspace_root", workspace_root)
132 .field("diagnostic_code", &diagnostic.code.as_ref().map(|it| &it.code))
134 Message::Progress { id, progress } => {
135 f.debug_struct("Progress").field("id", id).field("progress", progress).finish()
144 DidCheckCrate(String),
145 DidFinish(io::Result<()>),
147 DidFailToRestart(String),
155 /// A [`FlycheckActor`] is a single check instance of a workspace.
156 struct FlycheckActor {
157 /// The workspace id of this flycheck instance.
159 sender: Box<dyn Fn(Message) + Send>,
160 config: FlycheckConfig,
161 /// Either the workspace root of the workspace we are flychecking,
162 /// or the project root of the project.
164 /// CargoHandle exists to wrap around the communication needed to be able to
165 /// run `cargo check` without blocking. Currently the Rust standard library
166 /// doesn't provide a way to read sub-process output without blocking, so we
167 /// have to wrap sub-processes output handling in a thread and pass messages
168 /// back over a channel.
169 cargo_handle: Option<CargoHandle>,
174 CheckEvent(Option<CargoMessage>),
180 sender: Box<dyn Fn(Message) + Send>,
181 config: FlycheckConfig,
182 workspace_root: AbsPathBuf,
184 tracing::info!(%id, ?workspace_root, "Spawning flycheck");
185 FlycheckActor { id, sender, config, root: workspace_root, cargo_handle: None }
188 fn report_progress(&self, progress: Progress) {
189 self.send(Message::Progress { id: self.id, progress });
192 fn next_event(&self, inbox: &Receiver<Restart>) -> Option<Event> {
193 let check_chan = self.cargo_handle.as_ref().map(|cargo| &cargo.receiver);
194 if let Ok(msg) = inbox.try_recv() {
195 // give restarts a preference so check outputs don't block a restart or stop
196 return Some(Event::Restart(msg));
199 recv(inbox) -> msg => msg.ok().map(Event::Restart),
200 recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())),
204 fn run(mut self, inbox: Receiver<Restart>) {
205 'event: while let Some(event) = self.next_event(&inbox) {
207 Event::Restart(Restart::No) => {
208 self.cancel_check_process();
210 Event::Restart(Restart::Yes) => {
211 // Cancel the previously spawned process
212 self.cancel_check_process();
213 while let Ok(restart) = inbox.recv_timeout(Duration::from_millis(50)) {
214 // restart chained with a stop, so just cancel
215 if let Restart::No = restart {
220 let command = self.check_command();
221 tracing::debug!(?command, "will restart flycheck");
222 match CargoHandle::spawn(command) {
223 Ok(cargo_handle) => {
225 command = ?self.check_command(),
226 "did restart flycheck"
228 self.cargo_handle = Some(cargo_handle);
229 self.report_progress(Progress::DidStart);
232 self.report_progress(Progress::DidFailToRestart(format!(
233 "Failed to run the following command: {:?} error={}",
234 self.check_command(),
240 Event::CheckEvent(None) => {
241 tracing::debug!(flycheck_id = self.id, "flycheck finished");
244 let cargo_handle = self.cargo_handle.take().unwrap();
245 let res = cargo_handle.join();
248 "Flycheck failed to run the following command: {:?}",
252 self.report_progress(Progress::DidFinish(res));
254 Event::CheckEvent(Some(message)) => match message {
255 CargoMessage::CompilerArtifact(msg) => {
256 self.report_progress(Progress::DidCheckCrate(msg.target.name));
259 CargoMessage::Diagnostic(msg) => {
260 self.send(Message::AddDiagnostic {
262 workspace_root: self.root.clone(),
269 // If we rerun the thread, we need to discard the previous check results first
270 self.cancel_check_process();
273 fn cancel_check_process(&mut self) {
274 if let Some(cargo_handle) = self.cargo_handle.take() {
276 command = ?self.check_command(),
277 "did cancel flycheck"
279 cargo_handle.cancel();
280 self.report_progress(Progress::DidCancel);
284 fn check_command(&self) -> Command {
285 let (mut cmd, args) = match &self.config {
286 FlycheckConfig::CargoCommand {
296 let mut cmd = Command::new(toolchain::cargo());
298 cmd.args(&["--workspace", "--message-format=json"]);
300 if let Some(target) = target_triple {
301 cmd.args(&["--target", target.as_str()]);
304 cmd.arg("--all-targets");
307 cmd.arg("--all-features");
309 if *no_default_features {
310 cmd.arg("--no-default-features");
312 if !features.is_empty() {
313 cmd.arg("--features");
314 cmd.arg(features.join(" "));
320 FlycheckConfig::CustomCommand {
327 let mut cmd = Command::new(command);
330 match invocation_location {
331 InvocationLocation::Workspace => {
332 match invocation_strategy {
333 InvocationStrategy::Once => {
334 cmd.current_dir(&self.root);
336 InvocationStrategy::PerWorkspace => {
337 // FIXME: cmd.current_dir(&affected_workspace);
338 cmd.current_dir(&self.root);
342 InvocationLocation::Root(root) => {
343 cmd.current_dir(root);
355 fn send(&self, check_task: Message) {
356 (self.sender)(check_task);
360 /// A handle to a cargo process used for fly-checking.
362 /// The handle to the actual cargo process. As we cannot cancel directly from with
363 /// a read syscall dropping and therefor terminating the process is our best option.
365 thread: jod_thread::JoinHandle<io::Result<(bool, String)>>,
366 receiver: Receiver<CargoMessage>,
370 fn spawn(mut command: Command) -> std::io::Result<CargoHandle> {
371 command.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());
372 let mut child = JodChild::spawn(command)?;
374 let stdout = child.stdout.take().unwrap();
375 let stderr = child.stderr.take().unwrap();
377 let (sender, receiver) = unbounded();
378 let actor = CargoActor::new(sender, stdout, stderr);
379 let thread = jod_thread::Builder::new()
380 .name("CargoHandle".to_owned())
381 .spawn(move || actor.run())
382 .expect("failed to spawn thread");
383 Ok(CargoHandle { child, thread, receiver })
386 fn cancel(mut self) {
387 let _ = self.child.kill();
388 let _ = self.child.wait();
391 fn join(mut self) -> io::Result<()> {
392 let _ = self.child.kill();
393 let exit_status = self.child.wait()?;
394 let (read_at_least_one_message, error) = self.thread.join()?;
395 if read_at_least_one_message || exit_status.success() {
398 Err(io::Error::new(io::ErrorKind::Other, format!(
399 "Cargo watcher failed, the command produced no valid metadata (exit code: {:?}):\n{}",
407 sender: Sender<CargoMessage>,
413 fn new(sender: Sender<CargoMessage>, stdout: ChildStdout, stderr: ChildStderr) -> CargoActor {
414 CargoActor { sender, stdout, stderr }
417 fn run(self) -> io::Result<(bool, String)> {
418 // We manually read a line at a time, instead of using serde's
419 // stream deserializers, because the deserializer cannot recover
420 // from an error, resulting in it getting stuck, because we try to
421 // be resilient against failures.
423 // Because cargo only outputs one JSON object per line, we can
424 // simply skip a line if it doesn't parse, which just ignores any
427 let mut error = String::new();
428 let mut read_at_least_one_message = false;
429 let output = streaming_output(
433 read_at_least_one_message = true;
435 // Try to deserialize a message from Cargo or Rustc.
436 let mut deserializer = serde_json::Deserializer::from_str(line);
437 deserializer.disable_recursion_limit();
438 if let Ok(message) = JsonMessage::deserialize(&mut deserializer) {
440 // Skip certain kinds of messages to only spend time on what's useful
441 JsonMessage::Cargo(message) => match message {
442 cargo_metadata::Message::CompilerArtifact(artifact)
443 if !artifact.fresh =>
445 self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap();
447 cargo_metadata::Message::CompilerMessage(msg) => {
448 self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap();
452 JsonMessage::Rustc(message) => {
453 self.sender.send(CargoMessage::Diagnostic(message)).unwrap();
459 error.push_str(line);
464 Ok(_) => Ok((read_at_least_one_message, error)),
465 Err(e) => Err(io::Error::new(e.kind(), format!("{:?}: {}", e, error))),
471 CompilerArtifact(cargo_metadata::Artifact),
472 Diagnostic(Diagnostic),
475 #[derive(Deserialize)]
478 Cargo(cargo_metadata::Message),