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.
7 io::{self, BufRead, BufReader},
9 process::{self, Command, Stdio},
13 use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
14 use serde::Deserialize;
17 pub use cargo_metadata::diagnostic::{
18 Applicability, Diagnostic, DiagnosticCode, DiagnosticLevel, DiagnosticSpan,
19 DiagnosticSpanMacroExpansion,
22 #[derive(Clone, Debug, PartialEq, Eq)]
23 pub enum FlycheckConfig {
26 target_triple: Option<String>,
28 no_default_features: bool,
30 features: Vec<String>,
31 extra_args: Vec<String>,
39 impl fmt::Display for FlycheckConfig {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 FlycheckConfig::CargoCommand { command, .. } => write!(f, "cargo {}", command),
43 FlycheckConfig::CustomCommand { command, args } => {
44 write!(f, "{} {}", command, args.join(" "))
50 /// Flycheck wraps the shared state and communication machinery used for
51 /// running `cargo check` (or other compatible command) and providing
52 /// diagnostics based on the output.
53 /// The spawned thread is shut down when this struct is dropped.
55 pub struct FlycheckHandle {
56 // XXX: drop order is significant
57 sender: Sender<Restart>,
58 thread: jod_thread::JoinHandle,
64 sender: Box<dyn Fn(Message) + Send>,
65 config: FlycheckConfig,
66 workspace_root: PathBuf,
68 let actor = FlycheckActor::new(id, sender, config, workspace_root);
69 let (sender, receiver) = unbounded::<Restart>();
70 let thread = jod_thread::spawn(move || actor.run(receiver));
71 FlycheckHandle { sender, thread }
74 /// Schedule a re-start of the cargo check worker.
75 pub fn update(&self) {
76 self.sender.send(Restart).unwrap();
81 /// Request adding a diagnostic with fixes included to a file
82 AddDiagnostic { workspace_root: PathBuf, diagnostic: Diagnostic },
84 /// Request check progress notification to client
86 /// Flycheck instance ID
92 impl fmt::Debug for Message {
93 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 Message::AddDiagnostic { workspace_root, diagnostic } => f
96 .debug_struct("AddDiagnostic")
97 .field("workspace_root", workspace_root)
98 .field("diagnostic_code", &diagnostic.code.as_ref().map(|it| &it.code))
100 Message::Progress { id, progress } => {
101 f.debug_struct("Progress").field("id", id).field("progress", progress).finish()
110 DidCheckCrate(String),
111 DidFinish(io::Result<()>),
117 struct FlycheckActor {
119 sender: Box<dyn Fn(Message) + Send>,
120 config: FlycheckConfig,
121 workspace_root: PathBuf,
122 /// WatchThread exists to wrap around the communication needed to be able to
123 /// run `cargo check` without blocking. Currently the Rust standard library
124 /// doesn't provide a way to read sub-process output without blocking, so we
125 /// have to wrap sub-processes output handling in a thread and pass messages
126 /// back over a channel.
127 cargo_handle: Option<CargoHandle>,
132 CheckEvent(Option<CargoMessage>),
138 sender: Box<dyn Fn(Message) + Send>,
139 config: FlycheckConfig,
140 workspace_root: PathBuf,
142 FlycheckActor { id, sender, config, workspace_root, cargo_handle: None }
144 fn progress(&self, progress: Progress) {
145 self.send(Message::Progress { id: self.id, progress });
147 fn next_event(&self, inbox: &Receiver<Restart>) -> Option<Event> {
148 let check_chan = self.cargo_handle.as_ref().map(|cargo| &cargo.receiver);
150 recv(inbox) -> msg => msg.ok().map(Event::Restart),
151 recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())),
154 fn run(mut self, inbox: Receiver<Restart>) {
155 while let Some(event) = self.next_event(&inbox) {
157 Event::Restart(Restart) => {
158 while let Ok(Restart) = inbox.recv_timeout(Duration::from_millis(50)) {}
160 self.cancel_check_process();
162 let mut command = self.check_command();
163 log::info!("restart flycheck {:?}", command);
164 command.stdout(Stdio::piped()).stderr(Stdio::null()).stdin(Stdio::null());
165 if let Ok(child) = command.spawn().map(JodChild) {
166 self.cargo_handle = Some(CargoHandle::spawn(child));
167 self.progress(Progress::DidStart);
170 Event::CheckEvent(None) => {
171 // Watcher finished, replace it with a never channel to
172 // avoid busy-waiting.
173 let cargo_handle = self.cargo_handle.take().unwrap();
174 let res = cargo_handle.join();
177 "Flycheck failed to run the following command: {:?}",
181 self.progress(Progress::DidFinish(res));
183 Event::CheckEvent(Some(message)) => match message {
184 CargoMessage::CompilerArtifact(msg) => {
185 self.progress(Progress::DidCheckCrate(msg.target.name));
188 CargoMessage::Diagnostic(msg) => {
189 self.send(Message::AddDiagnostic {
190 workspace_root: self.workspace_root.clone(),
197 // If we rerun the thread, we need to discard the previous check results first
198 self.cancel_check_process();
200 fn cancel_check_process(&mut self) {
201 if self.cargo_handle.take().is_some() {
202 self.progress(Progress::DidCancel);
205 fn check_command(&self) -> Command {
206 let mut cmd = match &self.config {
207 FlycheckConfig::CargoCommand {
216 let mut cmd = Command::new(toolchain::cargo());
218 cmd.args(&["--workspace", "--message-format=json", "--manifest-path"])
219 .arg(self.workspace_root.join("Cargo.toml"));
221 if let Some(target) = target_triple {
222 cmd.args(&["--target", target.as_str()]);
225 cmd.arg("--all-targets");
228 cmd.arg("--all-features");
230 if *no_default_features {
231 cmd.arg("--no-default-features");
233 if !features.is_empty() {
234 cmd.arg("--features");
235 cmd.arg(features.join(" "));
238 cmd.args(extra_args);
241 FlycheckConfig::CustomCommand { command, args } => {
242 let mut cmd = Command::new(command);
247 cmd.current_dir(&self.workspace_root);
251 fn send(&self, check_task: Message) {
252 (self.sender)(check_task)
259 thread: jod_thread::JoinHandle<io::Result<bool>>,
260 receiver: Receiver<CargoMessage>,
264 fn spawn(mut child: JodChild) -> CargoHandle {
265 let child_stdout = child.stdout.take().unwrap();
266 let (sender, receiver) = unbounded();
267 let actor = CargoActor::new(child_stdout, sender);
268 let thread = jod_thread::spawn(move || actor.run());
269 CargoHandle { child, thread, receiver }
271 fn join(mut self) -> io::Result<()> {
272 // It is okay to ignore the result, as it only errors if the process is already dead
273 let _ = self.child.kill();
274 let exit_status = self.child.wait()?;
275 let read_at_least_one_message = self.thread.join()?;
276 if !exit_status.success() && !read_at_least_one_message {
277 // FIXME: Read the stderr to display the reason, see `read2()` reference in PR comment:
278 // https://github.com/rust-analyzer/rust-analyzer/pull/3632#discussion_r395605298
279 return Err(io::Error::new(
280 io::ErrorKind::Other,
282 "Cargo watcher failed, the command produced no valid metadata (exit code: {:?})",
292 child_stdout: process::ChildStdout,
293 sender: Sender<CargoMessage>,
297 fn new(child_stdout: process::ChildStdout, sender: Sender<CargoMessage>) -> CargoActor {
298 CargoActor { child_stdout, sender }
300 fn run(self) -> io::Result<bool> {
301 // We manually read a line at a time, instead of using serde's
302 // stream deserializers, because the deserializer cannot recover
303 // from an error, resulting in it getting stuck, because we try to
304 // be resilient against failures.
306 // Because cargo only outputs one JSON object per line, we can
307 // simply skip a line if it doesn't parse, which just ignores any
309 let stdout = BufReader::new(self.child_stdout);
310 let mut read_at_least_one_message = false;
311 for message in stdout.lines() {
312 let message = match message {
313 Ok(message) => message,
315 log::error!("Invalid json from cargo check, ignoring ({})", err);
320 read_at_least_one_message = true;
322 // Try to deserialize a message from Cargo or Rustc.
323 let mut deserializer = serde_json::Deserializer::from_str(&message);
324 deserializer.disable_recursion_limit();
325 if let Ok(message) = JsonMessage::deserialize(&mut deserializer) {
327 // Skip certain kinds of messages to only spend time on what's useful
328 JsonMessage::Cargo(message) => match message {
329 cargo_metadata::Message::CompilerArtifact(artifact) if !artifact.fresh => {
330 self.sender.send(CargoMessage::CompilerArtifact(artifact)).unwrap()
332 cargo_metadata::Message::CompilerMessage(msg) => {
333 self.sender.send(CargoMessage::Diagnostic(msg.message)).unwrap()
336 cargo_metadata::Message::CompilerArtifact(_)
337 | cargo_metadata::Message::BuildScriptExecuted(_)
338 | cargo_metadata::Message::BuildFinished(_)
339 | cargo_metadata::Message::TextLine(_)
342 JsonMessage::Rustc(message) => {
343 self.sender.send(CargoMessage::Diagnostic(message)).unwrap()
348 Ok(read_at_least_one_message)
353 CompilerArtifact(cargo_metadata::Artifact),
354 Diagnostic(Diagnostic),
357 #[derive(Deserialize)]
360 Cargo(cargo_metadata::Message),