2020-02-18 11:11:32 +00:00
|
|
|
//! Loads a Cargo project into a static instance of analysis, without support
|
|
|
|
//! for incorporating changes.
|
2020-06-24 13:52:07 +00:00
|
|
|
use std::{path::Path, sync::Arc};
|
2019-02-05 21:54:17 +00:00
|
|
|
|
2020-02-18 11:11:32 +00:00
|
|
|
use anyhow::Result;
|
2019-08-25 10:04:56 +00:00
|
|
|
use crossbeam_channel::{unbounded, Receiver};
|
Enable attribute macro expansion in `analysis-stats`
Before:
```
> $ rust-analyzer -q analysis-stats --with-proc-macro --load-output-dirs .
Database loaded: 19.08s, 277minstr
crates: 34, mods: 688, decls: 13202, fns: 10412
Item Collection: 16.21s, 76ginstr
exprs: 290580, ??ty: 2508 (0%), ?ty: 1814 (0%), !ty: 947
Inference: 27.46s, 108ginstr
Total: 43.67s, 184ginstr
```
After:
```
> $ ./target/release/rust-analyzer -q analysis-stats --with-proc-macro --load-output-dirs .
Database loaded: 1.09s, 277minstr
crates: 34, mods: 688, decls: 14790, fns: 11006
Item Collection: 18.20s, 78ginstr
exprs: 297826, ??ty: 493 (0%), ?ty: 558 (0%), !ty: 342
Inference: 28.34s, 111ginstr
Total: 46.54s, 190ginstr
```
2021-06-05 09:29:24 +00:00
|
|
|
use hir::db::DefDatabase;
|
2020-10-02 13:45:09 +00:00
|
|
|
use ide::{AnalysisHost, Change};
|
2020-10-24 08:39:57 +00:00
|
|
|
use ide_db::base_db::CrateGraph;
|
2021-08-31 12:44:43 +00:00
|
|
|
use proc_macro_api::ProcMacroServer;
|
2021-08-31 11:38:52 +00:00
|
|
|
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace};
|
2020-07-08 16:22:57 +00:00
|
|
|
use vfs::{loader::Handle, AbsPath, AbsPathBuf};
|
2020-02-17 18:07:30 +00:00
|
|
|
|
2021-08-22 11:05:12 +00:00
|
|
|
use crate::reload::{load_proc_macro, ProjectFolders, SourceRootConfig};
|
2019-02-09 12:06:12 +00:00
|
|
|
|
2021-08-01 14:34:51 +00:00
|
|
|
// Note: Since this type is used by external tools that use rust-analyzer as a library
|
|
|
|
// what otherwise would be `pub(crate)` has to be `pub` here instead.
|
|
|
|
pub struct LoadCargoConfig {
|
|
|
|
pub load_out_dirs_from_check: bool,
|
|
|
|
pub with_proc_macro: bool,
|
|
|
|
pub prefill_caches: bool,
|
2021-02-08 10:30:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:34:51 +00:00
|
|
|
// Note: Since this function is used by external tools that use rust-analyzer as a library
|
|
|
|
// what otherwise would be `pub(crate)` has to be `pub` here instead.
|
|
|
|
pub fn load_workspace_at(
|
2021-02-15 23:26:47 +00:00
|
|
|
root: &Path,
|
|
|
|
cargo_config: &CargoConfig,
|
|
|
|
load_config: &LoadCargoConfig,
|
|
|
|
progress: &dyn Fn(String),
|
2021-08-31 12:44:43 +00:00
|
|
|
) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroServer>)> {
|
2020-06-24 11:34:24 +00:00
|
|
|
let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
|
2020-06-03 10:05:50 +00:00
|
|
|
let root = ProjectManifest::discover_single(&root)?;
|
2021-08-31 11:38:52 +00:00
|
|
|
let mut workspace = ProjectWorkspace::load(root, cargo_config, progress)?;
|
2020-03-16 13:51:44 +00:00
|
|
|
|
2021-08-31 11:38:52 +00:00
|
|
|
if load_config.load_out_dirs_from_check {
|
|
|
|
let build_scripts = workspace.run_build_scripts(cargo_config, progress)?;
|
|
|
|
workspace.set_build_scripts(build_scripts)
|
|
|
|
}
|
|
|
|
|
|
|
|
load_workspace(workspace, load_config)
|
2021-02-15 23:26:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 14:34:51 +00:00
|
|
|
// Note: Since this function is used by external tools that use rust-analyzer as a library
|
|
|
|
// what otherwise would be `pub(crate)` has to be `pub` here instead.
|
|
|
|
//
|
|
|
|
// The reason both, `load_workspace_at` and `load_workspace` are `pub` is that some of
|
|
|
|
// these tools need access to `ProjectWorkspace`, too, which `load_workspace_at` hides.
|
|
|
|
pub fn load_workspace(
|
2021-08-31 11:38:52 +00:00
|
|
|
ws: ProjectWorkspace,
|
2021-07-18 08:29:22 +00:00
|
|
|
load_config: &LoadCargoConfig,
|
2021-08-31 12:44:43 +00:00
|
|
|
) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroServer>)> {
|
2019-08-25 10:04:56 +00:00
|
|
|
let (sender, receiver) = unbounded();
|
2020-06-11 09:04:09 +00:00
|
|
|
let mut vfs = vfs::Vfs::default();
|
|
|
|
let mut loader = {
|
|
|
|
let loader =
|
2020-06-25 06:59:55 +00:00
|
|
|
vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
|
2020-06-11 09:04:09 +00:00
|
|
|
Box::new(loader)
|
|
|
|
};
|
2020-03-18 12:56:46 +00:00
|
|
|
|
2021-07-18 08:29:22 +00:00
|
|
|
let proc_macro_client = if load_config.with_proc_macro {
|
2021-07-17 14:40:13 +00:00
|
|
|
let path = AbsPathBuf::assert(std::env::current_exe()?);
|
2022-07-26 09:53:50 +00:00
|
|
|
Ok(ProcMacroServer::spawn(path, &["proc-macro"]).unwrap())
|
2020-06-11 09:04:09 +00:00
|
|
|
} else {
|
2022-07-26 09:53:50 +00:00
|
|
|
Err("proc macro server not started".to_owned())
|
2020-04-12 10:25:31 +00:00
|
|
|
};
|
2020-06-11 09:04:09 +00:00
|
|
|
|
2021-08-22 10:32:00 +00:00
|
|
|
let crate_graph = ws.to_crate_graph(
|
2022-05-19 13:29:35 +00:00
|
|
|
&mut |_, path: &AbsPath| load_proc_macro(proc_macro_client.as_ref(), path, &[]),
|
2021-08-22 10:32:00 +00:00
|
|
|
&mut |path: &AbsPath| {
|
|
|
|
let contents = loader.load_sync(path);
|
|
|
|
let path = vfs::VfsPath::from(path.to_path_buf());
|
|
|
|
vfs.set_file_contents(path.clone(), contents);
|
|
|
|
vfs.file_id(&path)
|
|
|
|
},
|
|
|
|
);
|
2020-06-11 09:04:09 +00:00
|
|
|
|
2021-07-18 10:13:03 +00:00
|
|
|
let project_folders = ProjectFolders::new(&[ws], &[]);
|
2021-02-12 14:58:29 +00:00
|
|
|
loader.set_config(vfs::loader::Config {
|
|
|
|
load: project_folders.load,
|
|
|
|
watch: vec![],
|
|
|
|
version: 0,
|
|
|
|
});
|
2020-06-11 09:04:09 +00:00
|
|
|
|
2021-08-15 12:46:13 +00:00
|
|
|
tracing::debug!("crate graph: {:?}", crate_graph);
|
2021-02-15 23:26:47 +00:00
|
|
|
let host =
|
|
|
|
load_crate_graph(crate_graph, project_folders.source_root_config, &mut vfs, &receiver);
|
2021-06-10 22:35:14 +00:00
|
|
|
|
2021-07-18 08:29:22 +00:00
|
|
|
if load_config.prefill_caches {
|
2022-01-14 09:48:59 +00:00
|
|
|
host.analysis().parallel_prime_caches(1, |_| {})?;
|
2021-06-11 06:27:25 +00:00
|
|
|
}
|
2022-07-26 09:53:50 +00:00
|
|
|
Ok((host, vfs, proc_macro_client.ok()))
|
2019-06-15 13:29:23 +00:00
|
|
|
}
|
2019-02-09 12:06:12 +00:00
|
|
|
|
2021-02-15 23:26:47 +00:00
|
|
|
fn load_crate_graph(
|
2020-06-11 09:04:09 +00:00
|
|
|
crate_graph: CrateGraph,
|
|
|
|
source_root_config: SourceRootConfig,
|
|
|
|
vfs: &mut vfs::Vfs,
|
|
|
|
receiver: &Receiver<vfs::loader::Message>,
|
2019-06-16 16:19:38 +00:00
|
|
|
) -> AnalysisHost {
|
2019-06-15 13:29:23 +00:00
|
|
|
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
|
2020-03-10 17:56:15 +00:00
|
|
|
let mut host = AnalysisHost::new(lru_cap);
|
2020-10-02 13:45:09 +00:00
|
|
|
let mut analysis_change = Change::new();
|
2019-06-15 13:29:23 +00:00
|
|
|
|
Enable attribute macro expansion in `analysis-stats`
Before:
```
> $ rust-analyzer -q analysis-stats --with-proc-macro --load-output-dirs .
Database loaded: 19.08s, 277minstr
crates: 34, mods: 688, decls: 13202, fns: 10412
Item Collection: 16.21s, 76ginstr
exprs: 290580, ??ty: 2508 (0%), ?ty: 1814 (0%), !ty: 947
Inference: 27.46s, 108ginstr
Total: 43.67s, 184ginstr
```
After:
```
> $ ./target/release/rust-analyzer -q analysis-stats --with-proc-macro --load-output-dirs .
Database loaded: 1.09s, 277minstr
crates: 34, mods: 688, decls: 14790, fns: 11006
Item Collection: 18.20s, 78ginstr
exprs: 297826, ??ty: 493 (0%), ?ty: 558 (0%), !ty: 342
Inference: 28.34s, 111ginstr
Total: 46.54s, 190ginstr
```
2021-06-05 09:29:24 +00:00
|
|
|
host.raw_database_mut().set_enable_proc_attr_macros(true);
|
|
|
|
|
2019-06-15 13:29:23 +00:00
|
|
|
// wait until Vfs has loaded all roots
|
|
|
|
for task in receiver {
|
2020-06-11 09:04:09 +00:00
|
|
|
match task {
|
2021-02-12 14:58:29 +00:00
|
|
|
vfs::loader::Message::Progress { n_done, n_total, config_version: _ } => {
|
2020-06-24 14:58:49 +00:00
|
|
|
if n_done == n_total {
|
2020-06-11 09:04:09 +00:00
|
|
|
break;
|
2019-11-24 09:06:00 +00:00
|
|
|
}
|
2020-06-11 09:04:09 +00:00
|
|
|
}
|
|
|
|
vfs::loader::Message::Loaded { files } => {
|
|
|
|
for (path, contents) in files {
|
2020-12-09 16:29:34 +00:00
|
|
|
vfs.set_file_contents(path.into(), contents);
|
2019-06-15 13:29:23 +00:00
|
|
|
}
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-11 09:04:09 +00:00
|
|
|
}
|
|
|
|
let changes = vfs.take_changes();
|
|
|
|
for file in changes {
|
|
|
|
if file.exists() {
|
|
|
|
let contents = vfs.file_contents(file.file_id).to_vec();
|
|
|
|
if let Ok(text) = String::from_utf8(contents) {
|
|
|
|
analysis_change.change_file(file.file_id, Some(Arc::new(text)))
|
|
|
|
}
|
2019-06-15 13:29:23 +00:00
|
|
|
}
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
2021-06-13 03:54:16 +00:00
|
|
|
let source_roots = source_root_config.partition(vfs);
|
2020-06-11 09:04:09 +00:00
|
|
|
analysis_change.set_roots(source_roots);
|
2019-02-09 12:06:12 +00:00
|
|
|
|
2020-03-16 13:51:44 +00:00
|
|
|
analysis_change.set_crate_graph(crate_graph);
|
|
|
|
|
2019-06-15 13:29:23 +00:00
|
|
|
host.apply_change(analysis_change);
|
|
|
|
host
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
2019-02-10 10:44:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-02-17 16:31:09 +00:00
|
|
|
|
|
|
|
use hir::Crate;
|
2019-02-10 10:44:53 +00:00
|
|
|
|
|
|
|
#[test]
|
2021-07-17 14:40:13 +00:00
|
|
|
fn test_loading_rust_analyzer() {
|
2019-04-04 09:57:10 +00:00
|
|
|
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
|
2021-07-18 08:29:22 +00:00
|
|
|
let cargo_config = CargoConfig::default();
|
2021-04-12 08:04:36 +00:00
|
|
|
let load_cargo_config = LoadCargoConfig {
|
|
|
|
load_out_dirs_from_check: false,
|
|
|
|
with_proc_macro: false,
|
2021-06-11 06:27:25 +00:00
|
|
|
prefill_caches: false,
|
2021-04-12 08:04:36 +00:00
|
|
|
};
|
2021-03-02 05:14:05 +00:00
|
|
|
let (host, _vfs, _proc_macro) =
|
2021-07-17 14:40:13 +00:00
|
|
|
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {}).unwrap();
|
2021-02-08 10:30:16 +00:00
|
|
|
|
2019-10-14 12:15:47 +00:00
|
|
|
let n_crates = Crate::all(host.raw_database()).len();
|
2019-02-10 10:44:53 +00:00
|
|
|
// RA has quite a few crates, but the exact count doesn't matter
|
2019-02-17 18:05:33 +00:00
|
|
|
assert!(n_crates > 20);
|
2019-02-10 10:44:53 +00:00
|
|
|
}
|
|
|
|
}
|