Auto merge of #16510 - Veykril:single-package-check, r=Veykril

feat: Allow cargo check to run on only the current package

cc https://github.com/rust-lang/rust-analyzer/pull/13336, https://github.com/rust-lang/rust-analyzer/issues/12882
This commit is contained in:
bors 2024-02-08 09:43:16 +00:00
commit ae89522928
7 changed files with 68 additions and 28 deletions

View File

@ -100,9 +100,14 @@ pub fn spawn(
FlycheckHandle { id, sender, _thread: thread }
}
/// Schedule a re-start of the cargo check worker.
pub fn restart(&self) {
self.sender.send(StateChange::Restart).unwrap();
/// Schedule a re-start of the cargo check worker to do a workspace wide check.
pub fn restart_workspace(&self) {
self.sender.send(StateChange::Restart(None)).unwrap();
}
/// Schedule a re-start of the cargo check worker to do a package wide check.
pub fn restart_for_package(&self, package: String) {
self.sender.send(StateChange::Restart(Some(package))).unwrap();
}
/// Stop this cargo check worker.
@ -153,7 +158,7 @@ pub enum Progress {
}
enum StateChange {
Restart,
Restart(Option<String>),
Cancel,
}
@ -213,7 +218,7 @@ fn run(mut self, inbox: Receiver<StateChange>) {
tracing::debug!(flycheck_id = self.id, "flycheck cancelled");
self.cancel_check_process();
}
Event::RequestStateChange(StateChange::Restart) => {
Event::RequestStateChange(StateChange::Restart(package)) => {
// Cancel the previously spawned process
self.cancel_check_process();
while let Ok(restart) = inbox.recv_timeout(Duration::from_millis(50)) {
@ -223,7 +228,7 @@ fn run(mut self, inbox: Receiver<StateChange>) {
}
}
let command = self.check_command();
let command = self.check_command(package.as_deref());
let formatted_command = format!("{:?}", command);
tracing::debug!(?command, "will restart flycheck");
@ -297,7 +302,7 @@ fn cancel_check_process(&mut self) {
}
}
fn check_command(&self) -> Command {
fn check_command(&self, package: Option<&str>) -> Command {
let (mut cmd, args) = match &self.config {
FlycheckConfig::CargoCommand {
command,
@ -314,7 +319,11 @@ fn check_command(&self) -> Command {
let mut cmd = Command::new(toolchain::cargo());
cmd.arg(command);
cmd.current_dir(&self.root);
cmd.arg("--workspace");
match package {
Some(pkg) => cmd.arg("-p").arg(pkg),
None => cmd.arg("--workspace"),
};
cmd.arg(if *ansi_color_output {
"--message-format=json-diagnostic-rendered-ansi"

View File

@ -223,6 +223,9 @@ struct ConfigData {
///
/// Aliased as `"checkOnSave.targets"`.
check_targets | checkOnSave_targets | checkOnSave_target: Option<CheckOnSaveTargets> = "null",
/// Whether `--workspace` should be passed to `cargo check`.
/// If false, `-p <package>` will be passed instead.
check_workspace: bool = "true",
/// Toggles the additional completions that automatically add imports when completed.
/// Note that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled.
@ -1323,6 +1326,10 @@ pub fn rustfmt(&self) -> RustfmtConfig {
}
}
pub fn flycheck_workspace(&self) -> bool {
self.data.check_workspace
}
pub fn flycheck(&self) -> FlycheckConfig {
match &self.data.check_overrideCommand {
Some(args) if !args.is_empty() => {

View File

@ -160,7 +160,7 @@ pub(crate) fn handle_did_save_text_document(
} else if state.config.check_on_save() {
// No specific flycheck was triggered, so let's trigger all of them.
for flycheck in state.flycheck.iter() {
flycheck.restart();
flycheck.restart_workspace();
}
}
Ok(())
@ -281,27 +281,40 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
let crate_root_paths: Vec<_> = crate_root_paths.iter().map(Deref::deref).collect();
// Find all workspaces that have at least one target containing the saved file
let workspace_ids = world.workspaces.iter().enumerate().filter(|(_, ws)| match ws {
project_model::ProjectWorkspace::Cargo { cargo, .. } => {
cargo.packages().any(|pkg| {
cargo[pkg]
.targets
.iter()
.any(|&it| crate_root_paths.contains(&cargo[it].root.as_path()))
})
}
project_model::ProjectWorkspace::Json { project, .. } => {
project.crates().any(|(c, _)| crate_ids.iter().any(|&crate_id| crate_id == c))
}
project_model::ProjectWorkspace::DetachedFiles { .. } => false,
let workspace_ids = world.workspaces.iter().enumerate().filter_map(|(idx, ws)| {
let package = match ws {
project_model::ProjectWorkspace::Cargo { cargo, .. } => {
cargo.packages().find_map(|pkg| {
let has_target_with_root = cargo[pkg]
.targets
.iter()
.any(|&it| crate_root_paths.contains(&cargo[it].root.as_path()));
has_target_with_root.then(|| cargo[pkg].name.clone())
})
}
project_model::ProjectWorkspace::Json { project, .. } => {
if !project
.crates()
.any(|(c, _)| crate_ids.iter().any(|&crate_id| crate_id == c))
{
return None;
}
None
}
project_model::ProjectWorkspace::DetachedFiles { .. } => return None,
};
Some((idx, package))
});
// Find and trigger corresponding flychecks
for flycheck in world.flycheck.iter() {
for (id, _) in workspace_ids.clone() {
for (id, package) in workspace_ids.clone() {
if id == flycheck.id() {
updated = true;
flycheck.restart();
match package.filter(|_| !world.config.flycheck_workspace()) {
Some(package) => flycheck.restart_for_package(package),
None => flycheck.restart_workspace(),
}
continue;
}
}
@ -309,7 +322,7 @@ fn run_flycheck(state: &mut GlobalState, vfs_path: VfsPath) -> bool {
// No specific flycheck was triggered, so let's trigger all of them.
if !updated {
for flycheck in world.flycheck.iter() {
flycheck.restart();
flycheck.restart_workspace();
}
}
Ok(())
@ -351,7 +364,7 @@ pub(crate) fn handle_run_flycheck(
}
// No specific flycheck was triggered, so let's trigger all of them.
for flycheck in state.flycheck.iter() {
flycheck.restart();
flycheck.restart_workspace();
}
Ok(())
}

View File

@ -314,7 +314,7 @@ fn handle_event(&mut self, event: Event) -> anyhow::Result<()> {
if became_quiescent {
if self.config.check_on_save() {
// Project has loaded properly, kick off initial flycheck
self.flycheck.iter().for_each(FlycheckHandle::restart);
self.flycheck.iter().for_each(FlycheckHandle::restart_workspace);
}
if self.config.prefill_caches() {
self.prime_caches_queue.request_op("became quiescent".to_string(), ());

View File

@ -503,7 +503,7 @@ fn recreate_crate_graph(&mut self, cause: String) {
let mut crate_graph_file_dependencies = FxHashSet::default();
let mut load = |path: &AbsPath| {
let _p = tracing::span!(tracing::Level::INFO, "switch_workspaces::load").entered();
let _p = tracing::span!(tracing::Level::DEBUG, "switch_workspaces::load").entered();
let vfs_path = vfs::VfsPath::from(path.to_path_buf());
crate_graph_file_dependencies.insert(vfs_path.clone());
match vfs.file_id(&vfs_path) {

View File

@ -251,6 +251,12 @@ Can be a single target, e.g. `"x86_64-unknown-linux-gnu"` or a list of targets,
Aliased as `"checkOnSave.targets"`.
--
[[rust-analyzer.check.workspace]]rust-analyzer.check.workspace (default: `true`)::
+
--
Whether `--workspace` should be passed to `cargo check`.
If false, `-p <package>` will be passed instead.
--
[[rust-analyzer.completion.autoimport.enable]]rust-analyzer.completion.autoimport.enable (default: `true`)::
+
--

View File

@ -803,6 +803,11 @@
}
]
},
"rust-analyzer.check.workspace": {
"markdownDescription": "Whether `--workspace` should be passed to `cargo check`.\nIf false, `-p <package>` will be passed instead.",
"default": true,
"type": "boolean"
},
"rust-analyzer.completion.autoimport.enable": {
"markdownDescription": "Toggles the additional completions that automatically add imports when completed.\nNote that your client must specify the `additionalTextEdits` LSP client capability to truly have this feature enabled.",
"default": true,