Skip to content

Commit 865e224

Browse files
committed
fix: load rust-analyzer.toml for virtual workspaces
1 parent ff041aa commit 865e224

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

crates/load-cargo/src/lib.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,19 @@ impl ProjectFolders {
282282
}
283283
}
284284

285+
// Collect workspace roots not already covered by a local PackageRoot
286+
// (e.g. virtual workspaces where no package lives at the workspace root).
287+
// We need these to load workspace-root rust-analyzer.toml into a local source root.
288+
let uncovered_ws_roots: Vec<AbsPathBuf> = workspaces
289+
.iter()
290+
.filter_map(|ws| {
291+
let ws_root = ws.workspace_root().to_path_buf();
292+
let dominated =
293+
roots.iter().any(|root| root.is_local && root.include.contains(&ws_root));
294+
(!dominated).then_some(ws_root)
295+
})
296+
.collect();
297+
285298
for root in roots.into_iter().filter(|it| !it.include.is_empty()) {
286299
let file_set_roots: Vec<VfsPath> =
287300
root.include.iter().cloned().map(VfsPath::from).collect();
@@ -334,6 +347,20 @@ impl ProjectFolders {
334347
}
335348
}
336349

350+
// For virtual workspaces, the workspace root has no local PackageRoot, so
351+
// rust-analyzer.toml there would fall into a library source root and be
352+
// ignored. Load it explicitly via Entry::Files and register the workspace
353+
// root as a local file-set root so the file is classified as local.
354+
for ws_root in &uncovered_ws_roots {
355+
let ratoml_path = ws_root.join("rust-analyzer.toml");
356+
let file_set_roots = vec![VfsPath::from(ws_root.clone())];
357+
let entry = vfs::loader::Entry::Files(vec![ratoml_path]);
358+
res.watch.push(res.load.len());
359+
res.load.push(entry);
360+
local_filesets.push(fsc.len() as u64);
361+
fsc.add_file_set(file_set_roots);
362+
}
363+
337364
if let Some(user_config_path) = user_config_dir_path {
338365
let ratoml_path = {
339366
let mut p = user_config_path.to_path_buf();

crates/rust-analyzer/tests/slow-tests/ratoml.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,3 +1008,45 @@ fn main() {
10081008
InternalTestingFetchConfigResponse::CheckWorkspace(true),
10091009
);
10101010
}
1011+
1012+
#[test]
1013+
fn ratoml_virtual_workspace() {
1014+
if skip_slow_tests() {
1015+
return;
1016+
}
1017+
1018+
let server = RatomlTest::new(
1019+
vec![
1020+
r#"
1021+
//- /p1/Cargo.toml
1022+
[workspace]
1023+
members = ["member"]
1024+
"#,
1025+
r#"
1026+
//- /p1/rust-analyzer.toml
1027+
assist.emitMustUse = true
1028+
"#,
1029+
r#"
1030+
//- /p1/member/Cargo.toml
1031+
[package]
1032+
name = "member"
1033+
version = "0.1.0"
1034+
edition = "2021"
1035+
"#,
1036+
r#"
1037+
//- /p1/member/src/lib.rs
1038+
pub fn add(left: usize, right: usize) -> usize {
1039+
left + right
1040+
}
1041+
"#,
1042+
],
1043+
vec!["p1"],
1044+
None,
1045+
);
1046+
1047+
server.query(
1048+
InternalTestingFetchConfigOption::AssistEmitMustUse,
1049+
3,
1050+
InternalTestingFetchConfigResponse::AssistEmitMustUse(true),
1051+
);
1052+
}

0 commit comments

Comments
 (0)