Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 25c59b8

Browse files
committed
address PR comments
1 parent e5bfd7e commit 25c59b8

File tree

4 files changed

+19
-58
lines changed

4 files changed

+19
-58
lines changed

crates/base-db/src/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use vfs::{file_set::FileSet, AbsPathBuf, AnchoredPath, FileId, VfsPath};
1717

1818
// Map from crate id to the name of the crate and path of the proc-macro. If the value is `None`,
1919
// then the crate for the proc-macro hasn't been build yet as the build data is missing.
20-
pub type ProcMacroPaths = FxHashMap<CrateId, Option<(Option<String>, AbsPathBuf)>>;
20+
pub type ProcMacroPaths = FxHashMap<CrateId, Result<(Option<String>, AbsPathBuf), String>>;
2121
pub type ProcMacros = FxHashMap<CrateId, ProcMacroLoadResult>;
2222

2323
/// Files are grouped into source roots. A source root is a directory on the

crates/project-model/src/workspace.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ fn project_json_to_crate_graph(
691691
target_layout: TargetLayoutLoadResult,
692692
) -> (CrateGraph, ProcMacroPaths) {
693693
let mut crate_graph = CrateGraph::default();
694-
let mut proc_macros = FxHashMap::default();
694+
let mut proc_macros: ProcMacroPaths = FxHashMap::default();
695695
let sysroot_deps = sysroot.as_ref().map(|sysroot| {
696696
sysroot_to_crate_graph(
697697
&mut crate_graph,
@@ -743,13 +743,11 @@ fn project_json_to_crate_graph(
743743
);
744744
if krate.is_proc_macro {
745745
if let Some(path) = krate.proc_macro_dylib_path.clone() {
746-
proc_macros.insert(
747-
crate_graph_crate_id,
748-
Some((
749-
krate.display_name.as_ref().map(|it| it.canonical_name().to_owned()),
750-
path,
751-
)),
752-
);
746+
let node = Ok((
747+
krate.display_name.as_ref().map(|it| it.canonical_name().to_owned()),
748+
path,
749+
));
750+
proc_macros.insert(crate_graph_crate_id, node);
753751
}
754752
}
755753
(crate_id, crate_graph_crate_id)
@@ -1193,8 +1191,8 @@ fn add_target_crate_root(
11931191
);
11941192
if is_proc_macro {
11951193
let proc_macro = match build_data.as_ref().map(|it| it.proc_macro_dylib_path.as_ref()) {
1196-
Some(it) => it.cloned().map(|path| Some((Some(cargo_name.to_owned()), path))),
1197-
None => Some(None),
1194+
Some(it) => it.cloned().map(|path| Ok((Some(cargo_name.to_owned()), path))),
1195+
None => Some(Err("crate has not yet been build".to_owned())),
11981196
};
11991197
if let Some(proc_macro) = proc_macro {
12001198
proc_macros.insert(crate_id, proc_macro);

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub fn load_workspace(
102102
(
103103
crate_id,
104104
path.map_or_else(
105-
|| Err("proc macro crate is missing dylib".to_owned()),
105+
|_| Err("proc macro crate is missing dylib".to_owned()),
106106
|(_, path)| load_proc_macro(proc_macro_server, &path, &[]),
107107
),
108108
)

crates/rust-analyzer/src/reload.rs

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl GlobalState {
251251
(
252252
crate_id,
253253
res.map_or_else(
254-
|| Err("proc macro crate is missing dylib".to_owned()),
254+
|_| Err("proc macro crate is missing dylib".to_owned()),
255255
|(crate_name, path)| {
256256
progress(path.display().to_string());
257257
load_proc_macro(
@@ -296,25 +296,11 @@ impl GlobalState {
296296
let workspaces =
297297
workspaces.iter().filter_map(|res| res.as_ref().ok().cloned()).collect::<Vec<_>>();
298298

299-
// `different_workspaces` is used to determine whether to spawn a a new proc macro server for
300-
// a newly-added rust workspace (most commonly sourced from a `rust-project.json`). While the
301-
// algorithm to find the new workspaces is quadratic, we generally expect that the number of total
302-
// workspaces to remain in the low single digits. the `cloned_workspace` is needed for borrowck
303-
// reasons.
304-
let cloned_workspaces = workspaces.clone();
305-
let different_workspaces = cloned_workspaces
306-
.iter()
307-
.filter(|ws| {
308-
!self
309-
.workspaces
310-
.iter()
311-
.find(|existing_ws| ws.eq_ignore_build_data(&existing_ws))
312-
.is_some()
313-
})
314-
.collect::<Vec<_>>();
315-
let same_workspaces = different_workspaces.is_empty();
316-
317-
tracing::debug!(current_workspaces = ?self.workspaces, new_workspaces = ?workspaces, ?same_workspaces, "comparing workspaces");
299+
let same_workspaces = workspaces.len() == self.workspaces.len()
300+
&& workspaces
301+
.iter()
302+
.zip(self.workspaces.iter())
303+
.all(|(l, r)| l.eq_ignore_build_data(r));
318304

319305
if same_workspaces {
320306
let (workspaces, build_scripts) = self.fetch_build_data_queue.last_op_result();
@@ -384,7 +370,7 @@ impl GlobalState {
384370
let files_config = self.config.files();
385371
let project_folders = ProjectFolders::new(&self.workspaces, &files_config.exclude);
386372

387-
if self.proc_macro_clients.is_empty() || !different_workspaces.is_empty() {
373+
if self.proc_macro_clients.is_empty() || !same_workspaces {
388374
if let Some((path, path_manually_set)) = self.config.proc_macro_srv() {
389375
tracing::info!("Spawning proc-macro servers");
390376
self.proc_macro_clients = self
@@ -462,31 +448,8 @@ impl GlobalState {
462448
};
463449
let mut change = Change::new();
464450

465-
// `self.fetch_proc_macros_queue.request_op(cause, proc_macro_paths)` is only called in
466-
// when `switch_workspaces` is called _without_ changing workspaces. This typically occurs
467-
// when build scripts have finishing building, but when rust-analyzer is used with a
468-
// rust-project.json, the build scripts have already been built by the external build system
469-
// that generated the `rust-project.json`.
470-
471-
// Therefore, in order to allow _new_ workspaces added via rust-project.json (e.g., after
472-
// a workspace was already added), we check whether this is the same workspace _or_
473-
// if any of the new workspaces is a `rust-project.json`.
474-
//
475-
// The else branch is used to provide better diagnostics to users while procedural macros
476-
// are still being built.
477-
if same_workspaces || different_workspaces.iter().any(|ws| ws.is_json()) {
478-
if self.config.expand_proc_macros() {
479-
self.fetch_proc_macros_queue.request_op(cause, proc_macro_paths);
480-
}
481-
} else {
482-
// Set up errors for proc-macros upfront that we haven't run build scripts yet
483-
let mut proc_macros = FxHashMap::default();
484-
for paths in proc_macro_paths {
485-
proc_macros.extend(paths.into_iter().map(move |(crate_id, _)| {
486-
(crate_id, Err("crate has not yet been build".to_owned()))
487-
}));
488-
}
489-
change.set_proc_macros(proc_macros);
451+
if self.config.expand_proc_macros() {
452+
self.fetch_proc_macros_queue.request_op(cause, proc_macro_paths);
490453
}
491454
change.set_crate_graph(crate_graph);
492455
self.analysis_host.apply_change(change);

0 commit comments

Comments
 (0)