Skip to content

Commit f84deff

Browse files
committed
Support loading OUT_DIR for CLI runs
1 parent 9e7dbb1 commit f84deff

File tree

5 files changed

+81
-48
lines changed

5 files changed

+81
-48
lines changed

crates/rust-analyzer/src/bin/args.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ pub(crate) enum Command {
2828
only: Option<String>,
2929
with_deps: bool,
3030
path: PathBuf,
31+
load_output_dirs: bool,
3132
},
3233
Bench {
3334
path: PathBuf,
3435
what: BenchWhat,
36+
load_output_dirs: bool,
3537
},
3638
RunServer,
3739
Version,
@@ -136,8 +138,9 @@ USAGE:
136138
rust-analyzer analysis-stats [FLAGS] [OPTIONS] [PATH]
137139
138140
FLAGS:
139-
-h, --help Prints help information
141+
-h, --help Prints help information
140142
--memory-usage
143+
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
141144
-v, --verbose
142145
-q, --quiet
143146
@@ -154,6 +157,7 @@ ARGS:
154157
let memory_usage = matches.contains("--memory-usage");
155158
let only: Option<String> = matches.opt_value_from_str(["-o", "--only"])?;
156159
let with_deps: bool = matches.contains("--with-deps");
160+
let load_output_dirs = matches.contains("--load-output-dirs");
157161
let path = {
158162
let mut trailing = matches.free()?;
159163
if trailing.len() != 1 {
@@ -162,7 +166,7 @@ ARGS:
162166
trailing.pop().unwrap().into()
163167
};
164168

165-
Command::Stats { randomize, memory_usage, only, with_deps, path }
169+
Command::Stats { randomize, memory_usage, only, with_deps, path, load_output_dirs }
166170
}
167171
"analysis-bench" => {
168172
if matches.contains(["-h", "--help"]) {
@@ -174,7 +178,8 @@ USAGE:
174178
rust-analyzer analysis-bench [FLAGS] [OPTIONS]
175179
176180
FLAGS:
177-
-h, --help Prints help information
181+
-h, --help Prints help information
182+
--load-output-dirs Load OUT_DIR values by running `cargo check` before analysis
178183
-v, --verbose
179184
180185
OPTIONS:
@@ -201,7 +206,8 @@ ARGS:
201206
"exactly one of `--highlight`, `--complete` or `--goto-def` must be set"
202207
),
203208
};
204-
Command::Bench { path, what }
209+
let load_output_dirs = matches.contains("--load-output-dirs");
210+
Command::Bench { path, what, load_output_dirs }
205211
}
206212
_ => {
207213
eprintln!(

crates/rust-analyzer/src/bin/main.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,25 @@ fn main() -> Result<()> {
1919
args::Command::Parse { no_dump } => cli::parse(no_dump)?,
2020
args::Command::Symbols => cli::symbols()?,
2121
args::Command::Highlight { rainbow } => cli::highlight(rainbow)?,
22-
args::Command::Stats { randomize, memory_usage, only, with_deps, path } => {
23-
cli::analysis_stats(
24-
args.verbosity,
25-
memory_usage,
26-
path.as_ref(),
27-
only.as_ref().map(String::as_ref),
28-
with_deps,
29-
randomize,
30-
)?
31-
}
32-
33-
args::Command::Bench { path, what } => {
34-
cli::analysis_bench(args.verbosity, path.as_ref(), what)?
22+
args::Command::Stats {
23+
randomize,
24+
memory_usage,
25+
only,
26+
with_deps,
27+
path,
28+
load_output_dirs,
29+
} => cli::analysis_stats(
30+
args.verbosity,
31+
memory_usage,
32+
path.as_ref(),
33+
only.as_ref().map(String::as_ref),
34+
with_deps,
35+
randomize,
36+
load_output_dirs,
37+
)?,
38+
39+
args::Command::Bench { path, what, load_output_dirs } => {
40+
cli::analysis_bench(args.verbosity, path.as_ref(), what, load_output_dirs)?
3541
}
3642

3743
args::Command::RunServer => run_server()?,

crates/rust-analyzer/src/cli/analysis_bench.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,17 @@ fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> {
4242
Ok((&s[..idx], &s[idx + 1..]))
4343
}
4444

45-
pub fn analysis_bench(verbosity: Verbosity, path: &Path, what: BenchWhat) -> Result<()> {
45+
pub fn analysis_bench(
46+
verbosity: Verbosity,
47+
path: &Path,
48+
what: BenchWhat,
49+
load_output_dirs: bool,
50+
) -> Result<()> {
4651
ra_prof::init();
4752

4853
let start = Instant::now();
4954
eprint!("loading: ");
50-
let (mut host, roots) = load_cargo(path)?;
55+
let (mut host, roots) = load_cargo(path, load_output_dirs)?;
5156
let db = host.raw_database();
5257
eprintln!("{:?}\n", start.elapsed());
5358

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ pub fn analysis_stats(
2323
only: Option<&str>,
2424
with_deps: bool,
2525
randomize: bool,
26+
load_output_dirs: bool,
2627
) -> Result<()> {
2728
let db_load_time = Instant::now();
28-
let (mut host, roots) = load_cargo(path)?;
29+
let (mut host, roots) = load_cargo(path, load_output_dirs)?;
2930
let db = host.raw_database();
3031
println!("Database loaded, {} roots, {:?}", roots.len(), db_load_time.elapsed());
3132
let analysis_time = Instant::now();

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Loads a Cargo project into a static instance of analysis, without support
22
//! for incorporating changes.
33
4-
use std::path::Path;
4+
use std::path::{Path, PathBuf};
55

66
use anyhow::Result;
77
use crossbeam_channel::{unbounded, Receiver};
8-
use ra_db::{CrateGraph, FileId, SourceRootId};
8+
use ra_db::{ExternSourceId, FileId, SourceRootId};
99
use ra_ide::{AnalysisChange, AnalysisHost};
10-
use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace};
10+
use ra_project_model::{get_rustc_cfg_options, CargoFeatures, PackageRoot, ProjectWorkspace};
1111
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
1212
use rustc_hash::{FxHashMap, FxHashSet};
1313

@@ -22,10 +22,21 @@ fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
2222

2323
pub(crate) fn load_cargo(
2424
root: &Path,
25+
load_out_dirs_from_check: bool,
2526
) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
2627
let root = std::env::current_dir()?.join(root);
27-
let ws = ProjectWorkspace::discover(root.as_ref(), &Default::default())?;
28-
let project_roots = ws.to_roots();
28+
let ws = ProjectWorkspace::discover(
29+
root.as_ref(),
30+
&CargoFeatures { load_out_dirs_from_check, ..Default::default() },
31+
)?;
32+
33+
let mut extern_dirs = FxHashSet::default();
34+
extern_dirs.extend(ws.out_dirs());
35+
36+
let mut project_roots = ws.to_roots();
37+
project_roots
38+
.extend(extern_dirs.iter().map(|path| PackageRoot::new(path.to_path_buf(), false)));
39+
2940
let (sender, receiver) = unbounded();
3041
let sender = Box::new(move |t| sender.send(t).unwrap());
3142
let (mut vfs, roots) = Vfs::new(
@@ -44,25 +55,6 @@ pub(crate) fn load_cargo(
4455
Watch(false),
4556
);
4657

47-
// FIXME: cfg options?
48-
let default_cfg_options = {
49-
let mut opts = get_rustc_cfg_options();
50-
opts.insert_atom("test".into());
51-
opts.insert_atom("debug_assertion".into());
52-
opts
53-
};
54-
55-
// FIXME: outdirs?
56-
let extern_source_roots = FxHashMap::default();
57-
58-
let crate_graph =
59-
ws.to_crate_graph(&default_cfg_options, &extern_source_roots, &mut |path: &Path| {
60-
let vfs_file = vfs.load(path);
61-
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
62-
vfs_file.map(vfs_file_to_id)
63-
});
64-
log::debug!("crate graph: {:?}", crate_graph);
65-
6658
let source_roots = roots
6759
.iter()
6860
.map(|&vfs_root| {
@@ -75,23 +67,24 @@ pub(crate) fn load_cargo(
7567
(source_root_id, project_root)
7668
})
7769
.collect::<FxHashMap<_, _>>();
78-
let host = load(&source_roots, crate_graph, &mut vfs, receiver);
70+
let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs);
7971
Ok((host, source_roots))
8072
}
8173

8274
pub(crate) fn load(
8375
source_roots: &FxHashMap<SourceRootId, PackageRoot>,
84-
crate_graph: CrateGraph,
76+
ws: ProjectWorkspace,
8577
vfs: &mut Vfs,
8678
receiver: Receiver<VfsTask>,
79+
extern_dirs: FxHashSet<PathBuf>,
8780
) -> AnalysisHost {
8881
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
8982
let mut host = AnalysisHost::new(lru_cap);
9083
let mut analysis_change = AnalysisChange::new();
91-
analysis_change.set_crate_graph(crate_graph);
9284

9385
// wait until Vfs has loaded all roots
9486
let mut roots_loaded = FxHashSet::default();
87+
let mut extern_source_roots = FxHashMap::default();
9588
for task in receiver {
9689
vfs.handle_task(task);
9790
let mut done = false;
@@ -111,6 +104,11 @@ pub(crate) fn load(
111104
source_roots[&source_root_id].path().display().to_string(),
112105
);
113106

107+
let vfs_root_path = vfs.root2path(root);
108+
if extern_dirs.contains(&vfs_root_path) {
109+
extern_source_roots.insert(vfs_root_path, ExternSourceId(root.0));
110+
}
111+
114112
let mut file_map = FxHashMap::default();
115113
for (vfs_file, path, text) in files {
116114
let file_id = vfs_file_to_id(vfs_file);
@@ -137,6 +135,23 @@ pub(crate) fn load(
137135
}
138136
}
139137

138+
// FIXME: cfg options?
139+
let default_cfg_options = {
140+
let mut opts = get_rustc_cfg_options();
141+
opts.insert_atom("test".into());
142+
opts.insert_atom("debug_assertion".into());
143+
opts
144+
};
145+
146+
let crate_graph =
147+
ws.to_crate_graph(&default_cfg_options, &extern_source_roots, &mut |path: &Path| {
148+
let vfs_file = vfs.load(path);
149+
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
150+
vfs_file.map(vfs_file_to_id)
151+
});
152+
log::debug!("crate graph: {:?}", crate_graph);
153+
analysis_change.set_crate_graph(crate_graph);
154+
140155
host.apply_change(analysis_change);
141156
host
142157
}
@@ -150,7 +165,7 @@ mod tests {
150165
#[test]
151166
fn test_loading_rust_analyzer() {
152167
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
153-
let (host, _roots) = load_cargo(path).unwrap();
168+
let (host, _roots) = load_cargo(path, false).unwrap();
154169
let n_crates = Crate::all(host.raw_database()).len();
155170
// RA has quite a few crates, but the exact count doesn't matter
156171
assert!(n_crates > 20);

0 commit comments

Comments
 (0)