From 20431706a863f92cb37dc512fef6e48d192aaf2c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:11:38 +0200 Subject: Merging upstream version 1.66.0+dfsg1. Signed-off-by: Daniel Baumann --- src/tools/rust-analyzer/crates/flycheck/Cargo.toml | 5 +- src/tools/rust-analyzer/crates/flycheck/src/lib.rs | 103 ++++++++++++++++----- 2 files changed, 84 insertions(+), 24 deletions(-) (limited to 'src/tools/rust-analyzer/crates/flycheck') diff --git a/src/tools/rust-analyzer/crates/flycheck/Cargo.toml b/src/tools/rust-analyzer/crates/flycheck/Cargo.toml index d3d180ece..2ad32d248 100644 --- a/src/tools/rust-analyzer/crates/flycheck/Cargo.toml +++ b/src/tools/rust-analyzer/crates/flycheck/Cargo.toml @@ -11,10 +11,11 @@ doctest = false [dependencies] crossbeam-channel = "0.5.5" -tracing = "0.1.35" +tracing = "0.1.37" cargo_metadata = "0.15.0" +rustc-hash = "1.1.0" serde = { version = "1.0.137", features = ["derive"] } -serde_json = "1.0.81" +serde_json = "1.0.86" jod-thread = "0.1.2" toolchain = { path = "../toolchain", version = "0.0.0" } diff --git a/src/tools/rust-analyzer/crates/flycheck/src/lib.rs b/src/tools/rust-analyzer/crates/flycheck/src/lib.rs index d9f4ef5b7..73c3a48b4 100644 --- a/src/tools/rust-analyzer/crates/flycheck/src/lib.rs +++ b/src/tools/rust-analyzer/crates/flycheck/src/lib.rs @@ -12,6 +12,7 @@ use std::{ use crossbeam_channel::{never, select, unbounded, Receiver, Sender}; use paths::AbsPathBuf; +use rustc_hash::FxHashMap; use serde::Deserialize; use stdx::{process::streaming_output, JodChild}; @@ -20,6 +21,20 @@ pub use cargo_metadata::diagnostic::{ DiagnosticSpanMacroExpansion, }; +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] +pub enum InvocationStrategy { + Once, + #[default] + PerWorkspace, +} + +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub enum InvocationLocation { + Root(AbsPathBuf), + #[default] + Workspace, +} + #[derive(Clone, Debug, PartialEq, Eq)] pub enum FlycheckConfig { CargoCommand { @@ -30,10 +45,14 @@ pub enum FlycheckConfig { all_features: bool, features: Vec, extra_args: Vec, + extra_env: FxHashMap, }, CustomCommand { command: String, args: Vec, + extra_env: FxHashMap, + invocation_strategy: InvocationStrategy, + invocation_location: InvocationLocation, }, } @@ -41,7 +60,7 @@ impl fmt::Display for FlycheckConfig { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { FlycheckConfig::CargoCommand { command, .. } => write!(f, "cargo {}", command), - FlycheckConfig::CustomCommand { command, args } => { + FlycheckConfig::CustomCommand { command, args, .. } => { write!(f, "{} {}", command, args.join(" ")) } } @@ -133,11 +152,15 @@ enum Restart { No, } +/// A [`FlycheckActor`] is a single check instance of a workspace. struct FlycheckActor { + /// The workspace id of this flycheck instance. id: usize, sender: Box, config: FlycheckConfig, - workspace_root: AbsPathBuf, + /// Either the workspace root of the workspace we are flychecking, + /// or the project root of the project. + root: AbsPathBuf, /// CargoHandle exists to wrap around the communication needed to be able to /// run `cargo check` without blocking. Currently the Rust standard library /// doesn't provide a way to read sub-process output without blocking, so we @@ -159,20 +182,27 @@ impl FlycheckActor { workspace_root: AbsPathBuf, ) -> FlycheckActor { tracing::info!(%id, ?workspace_root, "Spawning flycheck"); - FlycheckActor { id, sender, config, workspace_root, cargo_handle: None } + FlycheckActor { id, sender, config, root: workspace_root, cargo_handle: None } } - fn progress(&self, progress: Progress) { + + fn report_progress(&self, progress: Progress) { self.send(Message::Progress { id: self.id, progress }); } + fn next_event(&self, inbox: &Receiver) -> Option { let check_chan = self.cargo_handle.as_ref().map(|cargo| &cargo.receiver); + if let Ok(msg) = inbox.try_recv() { + // give restarts a preference so check outputs don't block a restart or stop + return Some(Event::Restart(msg)); + } select! { recv(inbox) -> msg => msg.ok().map(Event::Restart), recv(check_chan.unwrap_or(&never())) -> msg => Some(Event::CheckEvent(msg.ok())), } } + fn run(mut self, inbox: Receiver) { - while let Some(event) = self.next_event(&inbox) { + 'event: while let Some(event) = self.next_event(&inbox) { match event { Event::Restart(Restart::No) => { self.cancel_check_process(); @@ -180,7 +210,12 @@ impl FlycheckActor { Event::Restart(Restart::Yes) => { // Cancel the previously spawned process self.cancel_check_process(); - while let Ok(_) = inbox.recv_timeout(Duration::from_millis(50)) {} + while let Ok(restart) = inbox.recv_timeout(Duration::from_millis(50)) { + // restart chained with a stop, so just cancel + if let Restart::No = restart { + continue 'event; + } + } let command = self.check_command(); tracing::debug!(?command, "will restart flycheck"); @@ -191,10 +226,10 @@ impl FlycheckActor { "did restart flycheck" ); self.cargo_handle = Some(cargo_handle); - self.progress(Progress::DidStart); + self.report_progress(Progress::DidStart); } Err(error) => { - self.progress(Progress::DidFailToRestart(format!( + self.report_progress(Progress::DidFailToRestart(format!( "Failed to run the following command: {:?} error={}", self.check_command(), error @@ -214,17 +249,17 @@ impl FlycheckActor { self.check_command() ); } - self.progress(Progress::DidFinish(res)); + self.report_progress(Progress::DidFinish(res)); } Event::CheckEvent(Some(message)) => match message { CargoMessage::CompilerArtifact(msg) => { - self.progress(Progress::DidCheckCrate(msg.target.name)); + self.report_progress(Progress::DidCheckCrate(msg.target.name)); } CargoMessage::Diagnostic(msg) => { self.send(Message::AddDiagnostic { id: self.id, - workspace_root: self.workspace_root.clone(), + workspace_root: self.root.clone(), diagnostic: msg, }); } @@ -242,12 +277,12 @@ impl FlycheckActor { "did cancel flycheck" ); cargo_handle.cancel(); - self.progress(Progress::DidCancel); + self.report_progress(Progress::DidCancel); } } fn check_command(&self) -> Command { - let mut cmd = match &self.config { + let (mut cmd, args) = match &self.config { FlycheckConfig::CargoCommand { command, target_triple, @@ -256,12 +291,11 @@ impl FlycheckActor { all_features, extra_args, features, + extra_env, } => { let mut cmd = Command::new(toolchain::cargo()); cmd.arg(command); - cmd.current_dir(&self.workspace_root); - cmd.args(&["--workspace", "--message-format=json", "--manifest-path"]) - .arg(self.workspace_root.join("Cargo.toml").as_os_str()); + cmd.args(&["--workspace", "--message-format=json"]); if let Some(target) = target_triple { cmd.args(&["--target", target.as_str()]); @@ -280,16 +314,41 @@ impl FlycheckActor { cmd.arg(features.join(" ")); } } - cmd.args(extra_args); - cmd + cmd.envs(extra_env); + (cmd, extra_args) } - FlycheckConfig::CustomCommand { command, args } => { + FlycheckConfig::CustomCommand { + command, + args, + extra_env, + invocation_strategy, + invocation_location, + } => { let mut cmd = Command::new(command); - cmd.args(args); - cmd + cmd.envs(extra_env); + + match invocation_location { + InvocationLocation::Workspace => { + match invocation_strategy { + InvocationStrategy::Once => { + cmd.current_dir(&self.root); + } + InvocationStrategy::PerWorkspace => { + // FIXME: cmd.current_dir(&affected_workspace); + cmd.current_dir(&self.root); + } + } + } + InvocationLocation::Root(root) => { + cmd.current_dir(root); + } + } + + (cmd, args) } }; - cmd.current_dir(&self.workspace_root); + + cmd.args(args); cmd } -- cgit v1.2.3