Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve consolidation of rust-analyzer crate specs with generated sources #3040

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ mod tests {
.find(|c| &c.display_name == "generated_srcs")
.unwrap();
assert!(gen.root_module.starts_with("/"));
assert!(gen.root_module.ends_with("/lib.rs"));
assert!(gen
.root_module
.ends_with("rules_rust_test_rust_analyzer/lib.rs"));

let include_dirs = &gen.source.as_ref().unwrap().include_dirs;
assert!(include_dirs.len() == 1);
Expand Down
18 changes: 18 additions & 0 deletions tools/rust_analyzer/aquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ fn consolidate_crate_specs(crate_specs: Vec<CrateSpec>) -> anyhow::Result<BTreeS
spec.cfg.retain(|cfg| !existing.cfg.contains(cfg));
existing.cfg.extend(spec.cfg);

match (existing.source.as_mut(), spec.source) {
(None, new @ Some(_)) => existing.source = new,
(Some(existing_source), Some(new_source)) => {
existing_source.exclude_dirs.extend(new_source.exclude_dirs);
existing_source.include_dirs.extend(new_source.include_dirs);
}
_ => (),
}

// display_name should match the library's crate name because Rust Analyzer
// seems to use display_name for matching crate entries in rust-project.json
// against symbols in source files. For more details, see
Expand All @@ -199,6 +208,15 @@ fn consolidate_crate_specs(crate_specs: Vec<CrateSpec>) -> anyhow::Result<BTreeS
existing.proc_macro_dylib_path.replace(dylib_path.clone());
}
}

// Prefer using workspace for root_module path if possible.
// A test crate might get an __EXEC_ROOT__-based root_module when depending
// on a library crate with mixed generated sources, for example.
if existing.root_module.starts_with("__EXEC_ROOT__")
&& spec.root_module.starts_with("__WORKSPACE__")
{
existing.root_module = spec.root_module;
}
} else {
consolidated_specs.insert(spec.crate_id.clone(), spec);
}
Expand Down