Skip to content

Commit 34cde83

Browse files
committed
Auto merge of rust-lang#17809 - nicolas-guichard:index-vendored, r=Veykril
Include vendored crates in StaticIndex `StaticIndex::compute` filters out modules from libraries. This makes an exceptions for vendored libraries, ie libraries actually defined inside the workspace being indexed. This aims to solve https://bugzilla.mozilla.org/show_bug.cgi?id=1846041 In general StaticIndex is meant for code browsers, which likely want to index all visible source files.
2 parents 0713a47 + 6c1d83b commit 34cde83

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

src/tools/rust-analyzer/crates/ide/src/static_index.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
44
use hir::{db::HirDatabase, Crate, HirFileIdExt, Module, Semantics};
55
use ide_db::{
6-
base_db::SourceRootDatabase, defs::Definition, documentation::Documentation,
7-
famous_defs::FamousDefs, helpers::get_definition, FileId, FileRange, FxHashMap, FxHashSet,
8-
RootDatabase,
6+
base_db::{SourceRootDatabase, VfsPath},
7+
defs::Definition,
8+
documentation::Documentation,
9+
famous_defs::FamousDefs,
10+
helpers::get_definition,
11+
FileId, FileRange, FxHashMap, FxHashSet, RootDatabase,
912
};
1013
use syntax::{AstNode, SyntaxKind::*, SyntaxNode, TextRange, T};
1114

@@ -227,13 +230,16 @@ impl StaticIndex<'_> {
227230
self.files.push(result);
228231
}
229232

230-
pub fn compute(analysis: &Analysis) -> StaticIndex<'_> {
233+
pub fn compute<'a>(analysis: &'a Analysis, workspace_root: &VfsPath) -> StaticIndex<'a> {
231234
let db = &*analysis.db;
232235
let work = all_modules(db).into_iter().filter(|module| {
233236
let file_id = module.definition_source_file_id(db).original_file(db);
234237
let source_root = db.file_source_root(file_id.into());
235238
let source_root = db.source_root(source_root);
236-
!source_root.is_library
239+
let is_vendored = source_root
240+
.path_for_file(&file_id.into())
241+
.is_some_and(|module_path| module_path.starts_with(workspace_root));
242+
!source_root.is_library || is_vendored
237243
});
238244
let mut this = StaticIndex {
239245
files: vec![],
@@ -259,12 +265,13 @@ impl StaticIndex<'_> {
259265
#[cfg(test)]
260266
mod tests {
261267
use crate::{fixture, StaticIndex};
262-
use ide_db::{FileRange, FxHashSet};
268+
use ide_db::{base_db::VfsPath, FileRange, FxHashSet};
263269
use syntax::TextSize;
264270

265271
fn check_all_ranges(ra_fixture: &str) {
266272
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
267-
let s = StaticIndex::compute(&analysis);
273+
let s =
274+
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
268275
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
269276
for f in s.files {
270277
for (range, _) in f.tokens {
@@ -283,7 +290,8 @@ mod tests {
283290
#[track_caller]
284291
fn check_definitions(ra_fixture: &str) {
285292
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
286-
let s = StaticIndex::compute(&analysis);
293+
let s =
294+
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
287295
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
288296
for (_, t) in s.tokens.iter() {
289297
if let Some(t) = t.definition {
@@ -326,7 +334,7 @@ enum E { X(Foo) }
326334
fn multi_crate() {
327335
check_definitions(
328336
r#"
329-
//- /main.rs crate:main deps:foo
337+
//- /workspace/main.rs crate:main deps:foo
330338
331339
332340
use foo::func;
@@ -335,7 +343,7 @@ fn main() {
335343
//^^^^
336344
func();
337345
}
338-
//- /foo/lib.rs crate:foo
346+
//- /workspace/foo/lib.rs crate:foo
339347
340348
pub func() {
341349
@@ -344,6 +352,24 @@ pub func() {
344352
);
345353
}
346354

355+
#[test]
356+
fn vendored_crate() {
357+
check_all_ranges(
358+
r#"
359+
//- /workspace/main.rs crate:main deps:external,vendored
360+
struct Main(i32);
361+
//^^^^ ^^^
362+
363+
//- /external/lib.rs new_source_root:library crate:[email protected],https://a.b/foo.git library
364+
struct ExternalLibrary(i32);
365+
366+
//- /workspace/vendored/lib.rs new_source_root:library crate:[email protected],https://a.b/bar.git library
367+
struct VendoredLibrary(i32);
368+
//^^^^^^^^^^^^^^^ ^^^
369+
"#,
370+
);
371+
}
372+
347373
#[test]
348374
fn derives() {
349375
check_all_ranges(

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/lsif.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,14 +287,15 @@ impl flags::Lsif {
287287
let manifest = ProjectManifest::discover_single(&path)?;
288288

289289
let workspace = ProjectWorkspace::load(manifest, &cargo_config, no_progress)?;
290+
let root = workspace.workspace_root().to_owned();
290291

291292
let (db, vfs, _proc_macro) =
292293
load_workspace(workspace, &cargo_config.extra_env, &load_cargo_config)?;
293294
let host = AnalysisHost::with_database(db);
294295
let db = host.raw_database();
295296
let analysis = host.analysis();
296297

297-
let si = StaticIndex::compute(&analysis);
298+
let si = StaticIndex::compute(&analysis, &root.into());
298299

299300
let mut lsif = LsifManager::new(&analysis, db, &vfs);
300301
lsif.add_vertex(lsif::Vertex::MetaData(lsif::MetaData {

src/tools/rust-analyzer/crates/rust-analyzer/src/cli/scip.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl flags::Scip {
6363
let db = host.raw_database();
6464
let analysis = host.analysis();
6565

66-
let si = StaticIndex::compute(&analysis);
66+
let si = StaticIndex::compute(&analysis, &root.clone().into());
6767

6868
let metadata = scip_types::Metadata {
6969
version: scip_types::ProtocolVersion::UnspecifiedProtocolVersion.into(),
@@ -334,6 +334,7 @@ mod test {
334334
use ide::{FilePosition, TextSize};
335335
use scip::symbol::format_symbol;
336336
use test_fixture::ChangeFixture;
337+
use vfs::VfsPath;
337338

338339
fn position(ra_fixture: &str) -> (AnalysisHost, FilePosition) {
339340
let mut host = AnalysisHost::default();
@@ -351,7 +352,8 @@ mod test {
351352
let (host, position) = position(ra_fixture);
352353

353354
let analysis = host.analysis();
354-
let si = StaticIndex::compute(&analysis);
355+
let si =
356+
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
355357

356358
let FilePosition { file_id, offset } = position;
357359

@@ -384,7 +386,7 @@ mod test {
384386
fn basic() {
385387
check_symbol(
386388
r#"
387-
//- /lib.rs crate:main deps:foo
389+
//- /workspace/lib.rs crate:main deps:foo
388390
use foo::example_mod::func;
389391
fn main() {
390392
func$0();
@@ -485,7 +487,7 @@ pub mod module {
485487
fn symbol_for_field() {
486488
check_symbol(
487489
r#"
488-
//- /lib.rs crate:main deps:foo
490+
//- /workspace/lib.rs crate:main deps:foo
489491
use foo::St;
490492
fn main() {
491493
let x = St { a$0: 2 };
@@ -503,7 +505,7 @@ pub mod module {
503505
fn symbol_for_param() {
504506
check_symbol(
505507
r#"
506-
//- /lib.rs crate:main deps:foo
508+
//- /workspace/lib.rs crate:main deps:foo
507509
use foo::example_mod::func;
508510
fn main() {
509511
func(42);
@@ -521,7 +523,7 @@ pub mod example_mod {
521523
fn symbol_for_closure_param() {
522524
check_symbol(
523525
r#"
524-
//- /lib.rs crate:main deps:foo
526+
//- /workspace/lib.rs crate:main deps:foo
525527
use foo::example_mod::func;
526528
fn main() {
527529
func();
@@ -541,7 +543,7 @@ pub mod example_mod {
541543
fn local_symbol_for_local() {
542544
check_symbol(
543545
r#"
544-
//- /lib.rs crate:main deps:foo
546+
//- /workspace/lib.rs crate:main deps:foo
545547
use foo::module::func;
546548
fn main() {
547549
func();
@@ -561,13 +563,13 @@ pub mod example_mod {
561563
fn global_symbol_for_pub_struct() {
562564
check_symbol(
563565
r#"
564-
//- /lib.rs crate:main
566+
//- /workspace/lib.rs crate:main
565567
mod foo;
566568
567569
fn main() {
568570
let _bar = foo::Bar { i: 0 };
569571
}
570-
//- /foo.rs
572+
//- /workspace/foo.rs
571573
pub struct Bar$0 {
572574
pub i: i32,
573575
}
@@ -580,13 +582,13 @@ pub mod example_mod {
580582
fn global_symbol_for_pub_struct_reference() {
581583
check_symbol(
582584
r#"
583-
//- /lib.rs crate:main
585+
//- /workspace/lib.rs crate:main
584586
mod foo;
585587
586588
fn main() {
587589
let _bar = foo::Bar$0 { i: 0 };
588590
}
589-
//- /foo.rs
591+
//- /workspace/foo.rs
590592
pub struct Bar {
591593
pub i: i32,
592594
}
@@ -599,7 +601,7 @@ pub mod example_mod {
599601
fn symbol_for_for_type_alias() {
600602
check_symbol(
601603
r#"
602-
//- /lib.rs crate:main
604+
//- /workspace/lib.rs crate:main
603605
pub type MyTypeAlias$0 = u8;
604606
"#,
605607
"rust-analyzer cargo main . MyTypeAlias#",
@@ -615,7 +617,8 @@ pub mod example_mod {
615617
host.raw_database_mut().apply_change(change_fixture.change);
616618

617619
let analysis = host.analysis();
618-
let si = StaticIndex::compute(&analysis);
620+
let si =
621+
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
619622

620623
let file = si.files.first().unwrap();
621624
let (_, token_id) = file.tokens.first().unwrap();

0 commit comments

Comments
 (0)