From 1d1eea217d5b674e6cad77704f912a3a76505d70 Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Fri, 10 Jan 2020 22:10:26 +0100 Subject: Slightly more robust cargo watcher root search --- crates/ra_lsp_server/src/world.rs | 18 +++++++++++++++--- crates/ra_project_model/src/cargo_workspace.rs | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index 121ddfd1f..36b5242d1 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -74,7 +74,7 @@ impl WorldState { lru_capacity: Option, exclude_globs: &[Glob], watch: Watch, - options: Options, + mut options: Options, feature_flags: FeatureFlags, ) -> WorldState { let mut change = AnalysisChange::new(); @@ -132,8 +132,20 @@ impl WorldState { change.set_crate_graph(crate_graph); // FIXME: Figure out the multi-workspace situation - let check_watcher = - CheckWatcher::new(&options.cargo_watch, folder_roots.first().cloned().unwrap()); + let check_watcher = { + let first_workspace = workspaces.first().unwrap(); + let cargo_project_root = match first_workspace { + ProjectWorkspace::Cargo { cargo, .. } => cargo.workspace_root.clone(), + ProjectWorkspace::Json { .. } => { + log::warn!( + "Cargo check watching only supported for cargo workspaces, disabling" + ); + options.cargo_watch.enable = false; + PathBuf::new() + } + }; + CheckWatcher::new(&options.cargo_watch, cargo_project_root) + }; let mut analysis_host = AnalysisHost::new(lru_capacity, feature_flags); analysis_host.apply_change(change); diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 1b3c246c7..18c2f11b7 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -21,7 +21,7 @@ use crate::Result; pub struct CargoWorkspace { packages: Arena, targets: Arena, - pub(crate) workspace_root: PathBuf, + pub workspace_root: PathBuf, } #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] -- cgit v1.2.3 From d6da18e99d2fb4e67e3bc7503059282b5e14bd13 Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Fri, 10 Jan 2020 22:41:52 +0100 Subject: Address nit --- crates/ra_lsp_server/src/world.rs | 2 +- crates/ra_project_model/src/cargo_workspace.rs | 6 +++++- crates/ra_project_model/src/lib.rs | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index 36b5242d1..cea18937f 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -135,7 +135,7 @@ impl WorldState { let check_watcher = { let first_workspace = workspaces.first().unwrap(); let cargo_project_root = match first_workspace { - ProjectWorkspace::Cargo { cargo, .. } => cargo.workspace_root.clone(), + ProjectWorkspace::Cargo { cargo, .. } => cargo.workspace_root().to_path_buf(), ProjectWorkspace::Json { .. } => { log::warn!( "Cargo check watching only supported for cargo workspaces, disabling" diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index 18c2f11b7..1832c101f 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -21,7 +21,7 @@ use crate::Result; pub struct CargoWorkspace { packages: Arena, targets: Arena, - pub workspace_root: PathBuf, + workspace_root: PathBuf, } #[derive(Deserialize, Clone, Debug, PartialEq, Eq)] @@ -225,4 +225,8 @@ impl CargoWorkspace { pub fn target_by_root(&self, root: &Path) -> Option { self.packages().filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)).next() } + + pub fn workspace_root(&self) -> &Path { + &self.workspace_root + } } diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index b7f6a9b57..6a104e6f2 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -333,7 +333,7 @@ impl ProjectWorkspace { pub fn workspace_root_for(&self, path: &Path) -> Option<&Path> { match self { ProjectWorkspace::Cargo { cargo, .. } => { - Some(cargo.workspace_root.as_ref()).filter(|root| path.starts_with(root)) + Some(cargo.workspace_root()).filter(|root| path.starts_with(root)) } ProjectWorkspace::Json { project: JsonProject { roots, .. } } => roots .iter() -- cgit v1.2.3 From 480c44918c73922b134b670cbd33014b8fcfc883 Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Sat, 11 Jan 2020 21:32:40 +0100 Subject: Disable cargo checking in workspaces with no cargo projects --- crates/ra_cargo_watch/src/lib.rs | 6 ++++++ crates/ra_lsp_server/src/world.rs | 28 ++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/crates/ra_cargo_watch/src/lib.rs b/crates/ra_cargo_watch/src/lib.rs index 9bc0fd405..cb0856aa4 100644 --- a/crates/ra_cargo_watch/src/lib.rs +++ b/crates/ra_cargo_watch/src/lib.rs @@ -58,6 +58,12 @@ impl CheckWatcher { CheckWatcher { task_recv, cmd_send: Some(cmd_send), handle: Some(handle), shared } } + /// Returns a CheckWatcher that doesn't actually do anything + pub fn dummy() -> CheckWatcher { + let shared = Arc::new(RwLock::new(CheckWatcherSharedState::new())); + CheckWatcher { task_recv: never(), cmd_send: None, handle: None, shared } + } + /// Schedule a re-start of the cargo check worker. pub fn update(&self) { if let Some(cmd_send) = &self.cmd_send { diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index cea18937f..a52bd2633 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -132,20 +132,20 @@ impl WorldState { change.set_crate_graph(crate_graph); // FIXME: Figure out the multi-workspace situation - let check_watcher = { - let first_workspace = workspaces.first().unwrap(); - let cargo_project_root = match first_workspace { - ProjectWorkspace::Cargo { cargo, .. } => cargo.workspace_root().to_path_buf(), - ProjectWorkspace::Json { .. } => { - log::warn!( - "Cargo check watching only supported for cargo workspaces, disabling" - ); - options.cargo_watch.enable = false; - PathBuf::new() - } - }; - CheckWatcher::new(&options.cargo_watch, cargo_project_root) - }; + let check_watcher = workspaces + .iter() + .find_map(|w| match w { + ProjectWorkspace::Cargo { cargo, .. } => Some(cargo), + ProjectWorkspace::Json { .. } => None, + }) + .map(|cargo| { + let cargo_project_root = cargo.workspace_root().to_path_buf(); + CheckWatcher::new(&options.cargo_watch, cargo_project_root) + }) + .unwrap_or_else(|| { + log::warn!("Cargo check watching only supported for cargo workspaces, disabling"); + CheckWatcher::dummy() + }); let mut analysis_host = AnalysisHost::new(lru_capacity, feature_flags); analysis_host.apply_change(change); -- cgit v1.2.3 From 8e778f9842123e1f688a2632d99e439821801bd2 Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Sat, 11 Jan 2020 21:37:01 +0100 Subject: Clean up straggling mut --- crates/ra_lsp_server/src/world.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ra_lsp_server/src/world.rs b/crates/ra_lsp_server/src/world.rs index a52bd2633..c0175c726 100644 --- a/crates/ra_lsp_server/src/world.rs +++ b/crates/ra_lsp_server/src/world.rs @@ -74,7 +74,7 @@ impl WorldState { lru_capacity: Option, exclude_globs: &[Glob], watch: Watch, - mut options: Options, + options: Options, feature_flags: FeatureFlags, ) -> WorldState { let mut change = AnalysisChange::new(); -- cgit v1.2.3