Skip to content

Commit 995a014

Browse files
committed
Auto merge of rust-lang#17900 - darichey:exclude-vendored-libraries, r=davidbarsky
Add scip/lsif flag to exclude vendored libaries rust-lang#17809 changed StaticIndex to include vendored libraries. This PR adds a flag to disable that behavior. At work, our monorepo has too many rust targets to index all at once, so we split them up into several shards. Since all of our libraries are vendored, if rust-analyzer includes them, sharding no longer has much benefit, because every shard will have to index the entire transitive dependency graphs of all of its targets. We get around the issue presented in rust-lang#17809 because some other shard will index the libraries directly.
2 parents 6908451 + bf4d31d commit 995a014

File tree

5 files changed

+95
-19
lines changed

5 files changed

+95
-19
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ pub use crate::{
104104
rename::RenameError,
105105
runnables::{Runnable, RunnableKind, TestId},
106106
signature_help::SignatureHelp,
107-
static_index::{StaticIndex, StaticIndexedFile, TokenId, TokenStaticData},
107+
static_index::{
108+
StaticIndex, StaticIndexedFile, TokenId, TokenStaticData, VendoredLibrariesConfig,
109+
},
108110
syntax_highlighting::{
109111
tags::{Highlight, HlMod, HlMods, HlOperator, HlPunct, HlTag},
110112
HighlightConfig, HlRange,

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

+55-10
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ fn documentation_for_definition(
124124
)
125125
}
126126

127+
pub enum VendoredLibrariesConfig<'a> {
128+
Included { workspace_root: &'a VfsPath },
129+
Excluded,
130+
}
131+
127132
impl StaticIndex<'_> {
128133
fn add_file(&mut self, file_id: FileId) {
129134
let current_crate = crates_for(self.db, file_id).pop().map(Into::into);
@@ -240,15 +245,22 @@ impl StaticIndex<'_> {
240245
self.files.push(result);
241246
}
242247

243-
pub fn compute<'a>(analysis: &'a Analysis, workspace_root: &VfsPath) -> StaticIndex<'a> {
248+
pub fn compute<'a>(
249+
analysis: &'a Analysis,
250+
vendored_libs_config: VendoredLibrariesConfig<'_>,
251+
) -> StaticIndex<'a> {
244252
let db = &*analysis.db;
245253
let work = all_modules(db).into_iter().filter(|module| {
246254
let file_id = module.definition_source_file_id(db).original_file(db);
247255
let source_root = db.file_source_root(file_id.into());
248256
let source_root = db.source_root(source_root);
249-
let is_vendored = source_root
250-
.path_for_file(&file_id.into())
251-
.is_some_and(|module_path| module_path.starts_with(workspace_root));
257+
let is_vendored = match vendored_libs_config {
258+
VendoredLibrariesConfig::Included { workspace_root } => source_root
259+
.path_for_file(&file_id.into())
260+
.is_some_and(|module_path| module_path.starts_with(workspace_root)),
261+
VendoredLibrariesConfig::Excluded => false,
262+
};
263+
252264
!source_root.is_library || is_vendored
253265
});
254266
let mut this = StaticIndex {
@@ -278,10 +290,11 @@ mod tests {
278290
use ide_db::{base_db::VfsPath, FileRange, FxHashSet};
279291
use syntax::TextSize;
280292

281-
fn check_all_ranges(ra_fixture: &str) {
293+
use super::VendoredLibrariesConfig;
294+
295+
fn check_all_ranges(ra_fixture: &str, vendored_libs_config: VendoredLibrariesConfig<'_>) {
282296
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
283-
let s =
284-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
297+
let s = StaticIndex::compute(&analysis, vendored_libs_config);
285298
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
286299
for f in s.files {
287300
for (range, _) in f.tokens {
@@ -298,10 +311,9 @@ mod tests {
298311
}
299312

300313
#[track_caller]
301-
fn check_definitions(ra_fixture: &str) {
314+
fn check_definitions(ra_fixture: &str, vendored_libs_config: VendoredLibrariesConfig<'_>) {
302315
let (analysis, ranges) = fixture::annotations_without_marker(ra_fixture);
303-
let s =
304-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
316+
let s = StaticIndex::compute(&analysis, vendored_libs_config);
305317
let mut range_set: FxHashSet<_> = ranges.iter().map(|it| it.0).collect();
306318
for (_, t) in s.tokens.iter() {
307319
if let Some(t) = t.definition {
@@ -329,6 +341,9 @@ struct Foo;
329341
enum E { X(Foo) }
330342
//^ ^ ^^^
331343
"#,
344+
VendoredLibrariesConfig::Included {
345+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
346+
},
332347
);
333348
check_definitions(
334349
r#"
@@ -337,6 +352,9 @@ struct Foo;
337352
enum E { X(Foo) }
338353
//^ ^
339354
"#,
355+
VendoredLibrariesConfig::Included {
356+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
357+
},
340358
);
341359
}
342360

@@ -359,6 +377,9 @@ pub func() {
359377
360378
}
361379
"#,
380+
VendoredLibrariesConfig::Included {
381+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
382+
},
362383
);
363384
}
364385

@@ -377,9 +398,30 @@ struct ExternalLibrary(i32);
377398
struct VendoredLibrary(i32);
378399
//^^^^^^^^^^^^^^^ ^^^
379400
"#,
401+
VendoredLibrariesConfig::Included {
402+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
403+
},
380404
);
381405
}
382406

407+
#[test]
408+
fn vendored_crate_excluded() {
409+
check_all_ranges(
410+
r#"
411+
//- /workspace/main.rs crate:main deps:external,vendored
412+
struct Main(i32);
413+
//^^^^ ^^^
414+
415+
//- /external/lib.rs new_source_root:library crate:external@0.1.0,https://a.b/foo.git library
416+
struct ExternalLibrary(i32);
417+
418+
//- /workspace/vendored/lib.rs new_source_root:library crate:vendored@0.1.0,https://a.b/bar.git library
419+
struct VendoredLibrary(i32);
420+
"#,
421+
VendoredLibrariesConfig::Excluded,
422+
)
423+
}
424+
383425
#[test]
384426
fn derives() {
385427
check_all_ranges(
@@ -394,6 +436,9 @@ pub macro Copy {}
394436
struct Hello(i32);
395437
//^^^^^ ^^^
396438
"#,
439+
VendoredLibrariesConfig::Included {
440+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
441+
},
397442
);
398443
}
399444
}

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

+9
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ xflags::xflags! {
138138

139139
cmd lsif {
140140
required path: PathBuf
141+
142+
/// Exclude code from vendored libraries from the resulting index.
143+
optional --exclude-vendored-libraries
141144
}
142145

143146
cmd scip {
@@ -148,6 +151,9 @@ xflags::xflags! {
148151

149152
/// A path to an json configuration file that can be used to customize cargo behavior.
150153
optional --config-path config_path: PathBuf
154+
155+
/// Exclude code from vendored libraries from the resulting index.
156+
optional --exclude-vendored-libraries
151157
}
152158
}
153159
}
@@ -259,6 +265,8 @@ pub struct Search {
259265
#[derive(Debug)]
260266
pub struct Lsif {
261267
pub path: PathBuf,
268+
269+
pub exclude_vendored_libraries: bool,
262270
}
263271

264272
#[derive(Debug)]
@@ -267,6 +275,7 @@ pub struct Scip {
267275

268276
pub output: Option<PathBuf>,
269277
pub config_path: Option<PathBuf>,
278+
pub exclude_vendored_libraries: bool,
270279
}
271280

272281
impl RustAnalyzer {

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Instant;
55

66
use ide::{
77
Analysis, AnalysisHost, FileId, FileRange, MonikerKind, PackageInformation, RootDatabase,
8-
StaticIndex, StaticIndexedFile, TokenId, TokenStaticData,
8+
StaticIndex, StaticIndexedFile, TokenId, TokenStaticData, VendoredLibrariesConfig,
99
};
1010
use ide_db::{line_index::WideEncoding, LineIndexDatabase};
1111
use load_cargo::{load_workspace, LoadCargoConfig, ProcMacroServerChoice};
@@ -296,7 +296,13 @@ impl flags::Lsif {
296296
let db = host.raw_database();
297297
let analysis = host.analysis();
298298

299-
let si = StaticIndex::compute(&analysis, &path.clone().into());
299+
let vendored_libs_config = if self.exclude_vendored_libraries {
300+
VendoredLibrariesConfig::Excluded
301+
} else {
302+
VendoredLibrariesConfig::Included { workspace_root: &path.clone().into() }
303+
};
304+
305+
let si = StaticIndex::compute(&analysis, vendored_libs_config);
300306

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

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

+20-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{path::PathBuf, time::Instant};
44

55
use ide::{
66
AnalysisHost, LineCol, MonikerDescriptorKind, MonikerResult, StaticIndex, StaticIndexedFile,
7-
SymbolInformationKind, TextRange, TokenId,
7+
SymbolInformationKind, TextRange, TokenId, VendoredLibrariesConfig,
88
};
99
use ide_db::LineIndexDatabase;
1010
use load_cargo::{load_workspace_at, LoadCargoConfig, ProcMacroServerChoice};
@@ -63,7 +63,13 @@ impl flags::Scip {
6363
let db = host.raw_database();
6464
let analysis = host.analysis();
6565

66-
let si = StaticIndex::compute(&analysis, &root.clone().into());
66+
let vendored_libs_config = if self.exclude_vendored_libraries {
67+
VendoredLibrariesConfig::Excluded
68+
} else {
69+
VendoredLibrariesConfig::Included { workspace_root: &root.clone().into() }
70+
};
71+
72+
let si = StaticIndex::compute(&analysis, vendored_libs_config);
6773

6874
let metadata = scip_types::Metadata {
6975
version: scip_types::ProtocolVersion::UnspecifiedProtocolVersion.into(),
@@ -352,8 +358,12 @@ mod test {
352358
let (host, position) = position(ra_fixture);
353359

354360
let analysis = host.analysis();
355-
let si =
356-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
361+
let si = StaticIndex::compute(
362+
&analysis,
363+
VendoredLibrariesConfig::Included {
364+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
365+
},
366+
);
357367

358368
let FilePosition { file_id, offset } = position;
359369

@@ -617,8 +627,12 @@ pub mod example_mod {
617627
host.raw_database_mut().apply_change(change_fixture.change);
618628

619629
let analysis = host.analysis();
620-
let si =
621-
StaticIndex::compute(&analysis, &VfsPath::new_virtual_path("/workspace".to_owned()));
630+
let si = StaticIndex::compute(
631+
&analysis,
632+
VendoredLibrariesConfig::Included {
633+
workspace_root: &VfsPath::new_virtual_path("/workspace".to_owned()),
634+
},
635+
);
622636

623637
let file = si.files.first().unwrap();
624638
let (_, token_id) = file.tokens.first().unwrap();

0 commit comments

Comments
 (0)