aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/cli
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-02-16 18:24:28 +0000
committerGitHub <[email protected]>2021-02-16 18:24:28 +0000
commit054caa81c5b3eb28fb99d508d8bd553d9920053b (patch)
tree552f2252a656d93ed244a650b08073559dfb6a70 /crates/rust-analyzer/src/cli
parent80f9618f3775d22fddbfa6fac041aed6519eca4e (diff)
parent1a441682606dc6c5240e2516ac65717cc7f25ecf (diff)
Merge #7690
7690: Extract `fn load_workspace(…)` from `fn load_cargo(…)` r=matklad a=regexident Unfortunately in https://github.com/rust-analyzer/rust-analyzer/pull/7595 I forgot to `pub use` (rather than just `use`) the newly introduced `LoadCargoConfig`. So this PR fixes this now. It also: - splits up `fn load_cargo` into a "workspace loading" and a "project loading" phase - adds a `progress: &dyn Fn(String)` to allow third-parties to provide CLI progress updates, too The motivation behind both of these is the fact that rust-analyzer currently does not support caching. As such any third-party making use of `ra_ap_…` needs to providing a caching layer itself. Unlike for rust-analyzer itself however a common use-pattern of third-parties is to analyze a specific target (`--lib`/`--bin <BIN>`/…) from a specific package (`--package`). The targets/packages of a crate can be obtained via `ProjectWorkspace::load(…)`, which currently is performed inside of `fn load_cargo`, effectively making the returned `ProjectWorkspace` inaccessible to the outer caller. With this information one can then provide early error handling via CLI (in case of ambiguities or invalid arguments, etc), instead of `fn load_cargo` failing with a possibly obscure error message. It also allows for annotating the persisted caches with its specific associated package/target selector and short-circuit quickly if a matching cache is found on disk, significantly cutting load times. Before: ```rust pub struct LoadCargoConfig { pub cargo_config: &CargoConfig, pub load_out_dirs_from_check: bool, pub with_proc_macro: bool, } pub fn load_cargo( root: &Path, config: &LoadCargoConfig ) -> Result<(AnalysisHost, vfs::Vfs)> { // ... } ``` After: ```rust pub fn load_workspace( root: &Path, config: &CargoConfig, progress: &dyn Fn(String), ) -> Result<ProjectWorkspace> { // ... } pub struct LoadCargoConfig { pub load_out_dirs_from_check: bool, pub with_proc_macro: bool, } pub fn load_cargo( ws: ProjectWorkspace, config: &LoadCargoConfig, progress: &dyn Fn(String), ) -> Result<(AnalysisHost, vfs::Vfs)> { // ... } ``` Co-authored-by: Vincent Esche <[email protected]>
Diffstat (limited to 'crates/rust-analyzer/src/cli')
-rw-r--r--crates/rust-analyzer/src/cli/analysis_bench.rs8
-rw-r--r--crates/rust-analyzer/src/cli/analysis_stats.rs7
-rw-r--r--crates/rust-analyzer/src/cli/diagnostics.rs11
-rw-r--r--crates/rust-analyzer/src/cli/load_cargo.rs39
-rw-r--r--crates/rust-analyzer/src/cli/ssr.rs24
5 files changed, 49 insertions, 40 deletions
diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs
index 877abd12b..8991f3bdb 100644
--- a/crates/rust-analyzer/src/cli/analysis_bench.rs
+++ b/crates/rust-analyzer/src/cli/analysis_bench.rs
@@ -17,7 +17,7 @@ use ide_db::{
17use vfs::AbsPathBuf; 17use vfs::AbsPathBuf;
18 18
19use crate::cli::{ 19use crate::cli::{
20 load_cargo::{load_cargo, LoadCargoConfig}, 20 load_cargo::{load_workspace_at, LoadCargoConfig},
21 print_memory_usage, Verbosity, 21 print_memory_usage, Verbosity,
22}; 22};
23 23
@@ -63,13 +63,13 @@ impl BenchCmd {
63 let start = Instant::now(); 63 let start = Instant::now();
64 eprint!("loading: "); 64 eprint!("loading: ");
65 65
66 let cargo_config = Default::default();
66 let load_cargo_config = LoadCargoConfig { 67 let load_cargo_config = LoadCargoConfig {
67 cargo_config: Default::default(),
68 load_out_dirs_from_check: self.load_output_dirs, 68 load_out_dirs_from_check: self.load_output_dirs,
69 with_proc_macro: self.with_proc_macro, 69 with_proc_macro: self.with_proc_macro,
70 }; 70 };
71 71 let (mut host, vfs) =
72 let (mut host, vfs) = load_cargo(&self.path, &load_cargo_config)?; 72 load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
73 eprintln!("{:?}\n", start.elapsed()); 73 eprintln!("{:?}\n", start.elapsed());
74 74
75 let file_id = { 75 let file_id = {
diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs
index 6d6f398f4..9072d8944 100644
--- a/crates/rust-analyzer/src/cli/analysis_stats.rs
+++ b/crates/rust-analyzer/src/cli/analysis_stats.rs
@@ -25,7 +25,7 @@ use stdx::format_to;
25use syntax::AstNode; 25use syntax::AstNode;
26 26
27use crate::cli::{ 27use crate::cli::{
28 load_cargo::{load_cargo, LoadCargoConfig}, 28 load_cargo::{load_workspace_at, LoadCargoConfig},
29 print_memory_usage, 29 print_memory_usage,
30 progress_report::ProgressReport, 30 progress_report::ProgressReport,
31 report_metric, Result, Verbosity, 31 report_metric, Result, Verbosity,
@@ -59,12 +59,13 @@ impl AnalysisStatsCmd {
59 }; 59 };
60 60
61 let mut db_load_sw = self.stop_watch(); 61 let mut db_load_sw = self.stop_watch();
62 let cargo_config = Default::default();
62 let load_cargo_config = LoadCargoConfig { 63 let load_cargo_config = LoadCargoConfig {
63 cargo_config: Default::default(),
64 load_out_dirs_from_check: self.load_output_dirs, 64 load_out_dirs_from_check: self.load_output_dirs,
65 with_proc_macro: self.with_proc_macro, 65 with_proc_macro: self.with_proc_macro,
66 }; 66 };
67 let (host, vfs) = load_cargo(&self.path, &load_cargo_config)?; 67 let (host, vfs) =
68 load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;
68 let db = host.raw_database(); 69 let db = host.raw_database();
69 eprintln!("{:<20} {}", "Database loaded:", db_load_sw.elapsed()); 70 eprintln!("{:<20} {}", "Database loaded:", db_load_sw.elapsed());
70 71
diff --git a/crates/rust-analyzer/src/cli/diagnostics.rs b/crates/rust-analyzer/src/cli/diagnostics.rs
index c60374c24..876f6c44f 100644
--- a/crates/rust-analyzer/src/cli/diagnostics.rs
+++ b/crates/rust-analyzer/src/cli/diagnostics.rs
@@ -11,7 +11,7 @@ use ide::{DiagnosticsConfig, Severity};
11use ide_db::base_db::SourceDatabaseExt; 11use ide_db::base_db::SourceDatabaseExt;
12 12
13use crate::cli::{ 13use crate::cli::{
14 load_cargo::{load_cargo, LoadCargoConfig}, 14 load_cargo::{load_workspace_at, LoadCargoConfig},
15 Result, 15 Result,
16}; 16};
17 17
@@ -33,12 +33,9 @@ pub fn diagnostics(
33 load_out_dirs_from_check: bool, 33 load_out_dirs_from_check: bool,
34 with_proc_macro: bool, 34 with_proc_macro: bool,
35) -> Result<()> { 35) -> Result<()> {
36 let load_cargo_config = LoadCargoConfig { 36 let cargo_config = Default::default();
37 cargo_config: Default::default(), 37 let load_cargo_config = LoadCargoConfig { load_out_dirs_from_check, with_proc_macro };
38 load_out_dirs_from_check, 38 let (host, _vfs) = load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?;
39 with_proc_macro,
40 };
41 let (host, _vfs) = load_cargo(path, &load_cargo_config)?;
42 let db = host.raw_database(); 39 let db = host.raw_database();
43 let analysis = host.analysis(); 40 let analysis = host.analysis();
44 41
diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs
index cf0cd81de..23442afac 100644
--- a/crates/rust-analyzer/src/cli/load_cargo.rs
+++ b/crates/rust-analyzer/src/cli/load_cargo.rs
@@ -14,16 +14,28 @@ use vfs::{loader::Handle, AbsPath, AbsPathBuf};
14use crate::reload::{ProjectFolders, SourceRootConfig}; 14use crate::reload::{ProjectFolders, SourceRootConfig};
15 15
16pub struct LoadCargoConfig { 16pub struct LoadCargoConfig {
17 pub cargo_config: CargoConfig,
18 pub load_out_dirs_from_check: bool, 17 pub load_out_dirs_from_check: bool,
19 pub with_proc_macro: bool, 18 pub with_proc_macro: bool,
20} 19}
21 20
22pub fn load_cargo(root: &Path, config: &LoadCargoConfig) -> Result<(AnalysisHost, vfs::Vfs)> { 21pub fn load_workspace_at(
22 root: &Path,
23 cargo_config: &CargoConfig,
24 load_config: &LoadCargoConfig,
25 progress: &dyn Fn(String),
26) -> Result<(AnalysisHost, vfs::Vfs)> {
23 let root = AbsPathBuf::assert(std::env::current_dir()?.join(root)); 27 let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
24 let root = ProjectManifest::discover_single(&root)?; 28 let root = ProjectManifest::discover_single(&root)?;
25 let ws = ProjectWorkspace::load(root, &config.cargo_config, &|_| {})?; 29 let workspace = ProjectWorkspace::load(root, cargo_config, progress)?;
26 30
31 load_workspace(workspace, load_config, progress)
32}
33
34pub fn load_workspace(
35 ws: ProjectWorkspace,
36 config: &LoadCargoConfig,
37 progress: &dyn Fn(String),
38) -> Result<(AnalysisHost, vfs::Vfs)> {
27 let (sender, receiver) = unbounded(); 39 let (sender, receiver) = unbounded();
28 let mut vfs = vfs::Vfs::default(); 40 let mut vfs = vfs::Vfs::default();
29 let mut loader = { 41 let mut loader = {
@@ -42,7 +54,7 @@ pub fn load_cargo(root: &Path, config: &LoadCargoConfig) -> Result<(AnalysisHost
42 let build_data = if config.load_out_dirs_from_check { 54 let build_data = if config.load_out_dirs_from_check {
43 let mut collector = BuildDataCollector::default(); 55 let mut collector = BuildDataCollector::default();
44 ws.collect_build_data_configs(&mut collector); 56 ws.collect_build_data_configs(&mut collector);
45 Some(collector.collect(&|_| {})?) 57 Some(collector.collect(progress)?)
46 } else { 58 } else {
47 None 59 None
48 }; 60 };
@@ -66,11 +78,12 @@ pub fn load_cargo(root: &Path, config: &LoadCargoConfig) -> Result<(AnalysisHost
66 }); 78 });
67 79
68 log::debug!("crate graph: {:?}", crate_graph); 80 log::debug!("crate graph: {:?}", crate_graph);
69 let host = load(crate_graph, project_folders.source_root_config, &mut vfs, &receiver); 81 let host =
82 load_crate_graph(crate_graph, project_folders.source_root_config, &mut vfs, &receiver);
70 Ok((host, vfs)) 83 Ok((host, vfs))
71} 84}
72 85
73fn load( 86fn load_crate_graph(
74 crate_graph: CrateGraph, 87 crate_graph: CrateGraph,
75 source_root_config: SourceRootConfig, 88 source_root_config: SourceRootConfig,
76 vfs: &mut vfs::Vfs, 89 vfs: &mut vfs::Vfs,
@@ -120,17 +133,17 @@ mod tests {
120 use hir::Crate; 133 use hir::Crate;
121 134
122 #[test] 135 #[test]
123 fn test_loading_rust_analyzer() { 136 fn test_loading_rust_analyzer() -> Result<()> {
124 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); 137 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
125 let load_cargo_config = LoadCargoConfig { 138 let cargo_config = Default::default();
126 cargo_config: Default::default(), 139 let load_cargo_config =
127 load_out_dirs_from_check: false, 140 LoadCargoConfig { load_out_dirs_from_check: false, with_proc_macro: false };
128 with_proc_macro: false, 141 let (host, _vfs) = load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?;
129 };
130 142
131 let (host, _vfs) = load_cargo(path, &load_cargo_config).unwrap();
132 let n_crates = Crate::all(host.raw_database()).len(); 143 let n_crates = Crate::all(host.raw_database()).len();
133 // RA has quite a few crates, but the exact count doesn't matter 144 // RA has quite a few crates, but the exact count doesn't matter
134 assert!(n_crates > 20); 145 assert!(n_crates > 20);
146
147 Ok(())
135 } 148 }
136} 149}
diff --git a/crates/rust-analyzer/src/cli/ssr.rs b/crates/rust-analyzer/src/cli/ssr.rs
index 8729ff0d9..71c61ed58 100644
--- a/crates/rust-analyzer/src/cli/ssr.rs
+++ b/crates/rust-analyzer/src/cli/ssr.rs
@@ -1,19 +1,18 @@
1//! Applies structured search replace rules from the command line. 1//! Applies structured search replace rules from the command line.
2 2
3use crate::cli::{ 3use crate::cli::{
4 load_cargo::{load_cargo, LoadCargoConfig}, 4 load_cargo::{load_workspace_at, LoadCargoConfig},
5 Result, 5 Result,
6}; 6};
7use ssr::{MatchFinder, SsrPattern, SsrRule}; 7use ssr::{MatchFinder, SsrPattern, SsrRule};
8 8
9pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> { 9pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> {
10 use ide_db::base_db::SourceDatabaseExt; 10 use ide_db::base_db::SourceDatabaseExt;
11 let load_cargo_config = LoadCargoConfig { 11 let cargo_config = Default::default();
12 cargo_config: Default::default(), 12 let load_cargo_config =
13 load_out_dirs_from_check: true, 13 LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true };
14 with_proc_macro: true, 14 let (host, vfs) =
15 }; 15 load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?;
16 let (host, vfs) = load_cargo(&std::env::current_dir()?, &load_cargo_config)?;
17 let db = host.raw_database(); 16 let db = host.raw_database();
18 let mut match_finder = MatchFinder::at_first_file(db)?; 17 let mut match_finder = MatchFinder::at_first_file(db)?;
19 for rule in rules { 18 for rule in rules {
@@ -36,12 +35,11 @@ pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> {
36pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<String>) -> Result<()> { 35pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<String>) -> Result<()> {
37 use ide_db::base_db::SourceDatabaseExt; 36 use ide_db::base_db::SourceDatabaseExt;
38 use ide_db::symbol_index::SymbolsDatabase; 37 use ide_db::symbol_index::SymbolsDatabase;
39 let load_cargo_config = LoadCargoConfig { 38 let cargo_config = Default::default();
40 cargo_config: Default::default(), 39 let load_cargo_config =
41 load_out_dirs_from_check: true, 40 LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true };
42 with_proc_macro: true, 41 let (host, _vfs) =
43 }; 42 load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?;
44 let (host, _vfs) = load_cargo(&std::env::current_dir()?, &load_cargo_config)?;
45 let db = host.raw_database(); 43 let db = host.raw_database();
46 let mut match_finder = MatchFinder::at_first_file(db)?; 44 let mut match_finder = MatchFinder::at_first_file(db)?;
47 for pattern in patterns { 45 for pattern in patterns {