Skip to content

Commit 3924c7d

Browse files
bors[bot]kiljacken
andauthored
Merge #2791
2791: Slightly more robust cargo watcher root search r=kiljacken a=kiljacken Fixes #2780 (hopefully). Use the already painstakingly found `workspaces` instead of naively using `folder_roots` from editor. Co-authored-by: Emil Lauridsen <mine809@gmail.com>
2 parents 8670812 + 8e778f9 commit 3924c7d

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

crates/ra_cargo_watch/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ impl CheckWatcher {
5858
CheckWatcher { task_recv, cmd_send: Some(cmd_send), handle: Some(handle), shared }
5959
}
6060

61+
/// Returns a CheckWatcher that doesn't actually do anything
62+
pub fn dummy() -> CheckWatcher {
63+
let shared = Arc::new(RwLock::new(CheckWatcherSharedState::new()));
64+
CheckWatcher { task_recv: never(), cmd_send: None, handle: None, shared }
65+
}
66+
6167
/// Schedule a re-start of the cargo check worker.
6268
pub fn update(&self) {
6369
if let Some(cmd_send) = &self.cmd_send {

crates/ra_lsp_server/src/world.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,20 @@ impl WorldState {
132132
change.set_crate_graph(crate_graph);
133133

134134
// FIXME: Figure out the multi-workspace situation
135-
let check_watcher =
136-
CheckWatcher::new(&options.cargo_watch, folder_roots.first().cloned().unwrap());
135+
let check_watcher = workspaces
136+
.iter()
137+
.find_map(|w| match w {
138+
ProjectWorkspace::Cargo { cargo, .. } => Some(cargo),
139+
ProjectWorkspace::Json { .. } => None,
140+
})
141+
.map(|cargo| {
142+
let cargo_project_root = cargo.workspace_root().to_path_buf();
143+
CheckWatcher::new(&options.cargo_watch, cargo_project_root)
144+
})
145+
.unwrap_or_else(|| {
146+
log::warn!("Cargo check watching only supported for cargo workspaces, disabling");
147+
CheckWatcher::dummy()
148+
});
137149

138150
let mut analysis_host = AnalysisHost::new(lru_capacity, feature_flags);
139151
analysis_host.apply_change(change);

crates/ra_project_model/src/cargo_workspace.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::Result;
2121
pub struct CargoWorkspace {
2222
packages: Arena<Package, PackageData>,
2323
targets: Arena<Target, TargetData>,
24-
pub(crate) workspace_root: PathBuf,
24+
workspace_root: PathBuf,
2525
}
2626

2727
#[derive(Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -225,4 +225,8 @@ impl CargoWorkspace {
225225
pub fn target_by_root(&self, root: &Path) -> Option<Target> {
226226
self.packages().filter_map(|pkg| pkg.targets(self).find(|it| it.root(self) == root)).next()
227227
}
228+
229+
pub fn workspace_root(&self) -> &Path {
230+
&self.workspace_root
231+
}
228232
}

crates/ra_project_model/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl ProjectWorkspace {
333333
pub fn workspace_root_for(&self, path: &Path) -> Option<&Path> {
334334
match self {
335335
ProjectWorkspace::Cargo { cargo, .. } => {
336-
Some(cargo.workspace_root.as_ref()).filter(|root| path.starts_with(root))
336+
Some(cargo.workspace_root()).filter(|root| path.starts_with(root))
337337
}
338338
ProjectWorkspace::Json { project: JsonProject { roots, .. } } => roots
339339
.iter()

0 commit comments

Comments
 (0)