feat: avoid checking the whole project during initial loading

This commit is contained in:
Aleksey Kladov 2021-04-12 11:04:36 +03:00
parent 7be06139b6
commit 186c5c47cb
14 changed files with 251 additions and 124 deletions

View File

@ -58,12 +58,17 @@ impl PartialEq for BuildDataConfig {
impl Eq for BuildDataConfig {}
#[derive(Debug, Default)]
#[derive(Debug)]
pub struct BuildDataCollector {
wrap_rustc: bool,
configs: FxHashMap<AbsPathBuf, BuildDataConfig>,
}
impl BuildDataCollector {
pub fn new(wrap_rustc: bool) -> Self {
Self { wrap_rustc, configs: FxHashMap::default() }
}
pub(crate) fn add_config(&mut self, workspace_root: &AbsPath, config: BuildDataConfig) {
self.configs.insert(workspace_root.to_path_buf(), config);
}
@ -71,15 +76,14 @@ impl BuildDataCollector {
pub fn collect(&mut self, progress: &dyn Fn(String)) -> Result<BuildDataResult> {
let mut res = BuildDataResult::default();
for (path, config) in self.configs.iter() {
res.per_workspace.insert(
path.clone(),
collect_from_workspace(
let workspace_build_data = WorkspaceBuildData::collect(
&config.cargo_toml,
&config.cargo_features,
&config.packages,
self.wrap_rustc,
progress,
)?,
);
)?;
res.per_workspace.insert(path.clone(), workspace_build_data);
}
Ok(res)
}
@ -120,13 +124,25 @@ impl BuildDataConfig {
}
}
fn collect_from_workspace(
impl WorkspaceBuildData {
fn collect(
cargo_toml: &AbsPath,
cargo_features: &CargoConfig,
packages: &Vec<cargo_metadata::Package>,
wrap_rustc: bool,
progress: &dyn Fn(String),
) -> Result<WorkspaceBuildData> {
let mut cmd = Command::new(toolchain::cargo());
if wrap_rustc {
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
// that to compile only proc macros and build scripts during the initial
// `cargo check`.
let myself = std::env::current_exe()?;
cmd.env("RUSTC_WRAPPER", myself);
cmd.env("RA_RUSTC_WRAPPER", "1");
}
cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"])
.arg(cargo_toml.as_ref());
@ -163,7 +179,11 @@ fn collect_from_workspace(
for message in cargo_metadata::Message::parse_stream(stdout).flatten() {
match message {
Message::BuildScriptExecuted(BuildScript {
package_id, out_dir, cfgs, env, ..
package_id,
out_dir,
cfgs,
env,
..
}) => {
let cfgs = {
let mut acc = Vec::new();
@ -195,7 +215,8 @@ fn collect_from_workspace(
if message.target.kind.contains(&"proc-macro".to_string()) {
let package_id = message.package_id;
// Skip rmeta file
if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) {
if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name))
{
let filename = AbsPathBuf::assert(PathBuf::from(&filename));
let package_build_data =
res.per_package.entry(package_id.repr.clone()).or_default();
@ -234,6 +255,7 @@ fn collect_from_workspace(
Ok(res)
}
}
// FIXME: File a better way to know if it is a dylib
fn is_dylib(path: &Utf8Path) -> bool {

View File

@ -30,8 +30,11 @@ fn benchmark_integrated_highlighting() {
let file = "./crates/ide_db/src/apply_change.rs";
let cargo_config = Default::default();
let load_cargo_config =
LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: false };
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
wrap_rustc: false,
with_proc_macro: false,
};
let (mut host, vfs, _proc_macro) = {
let _it = stdx::timeit("workspace loading");

View File

@ -3,6 +3,7 @@
//! Based on cli flags, either spawns an LSP server, or runs a batch analysis
mod flags;
mod logger;
mod rustc_wrapper;
use std::{convert::TryFrom, env, fs, path::Path, process};
@ -26,6 +27,20 @@ static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
fn main() {
if std::env::var("RA_RUSTC_WRAPPER").is_ok() {
let mut args = std::env::args_os();
let _me = args.next().unwrap();
let rustc = args.next().unwrap();
let code = match rustc_wrapper::run_rustc_skipping_cargo_checking(rustc, args.collect()) {
Ok(rustc_wrapper::ExitCode(code)) => code.unwrap_or(102),
Err(err) => {
eprintln!("{}", err);
101
}
};
process::exit(code);
}
if let Err(err) = try_main() {
log::error!("Unexpected error: {}", err);
eprintln!("{}", err);

View File

@ -0,0 +1,46 @@
//! We setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself during the
//! initial `cargo check`. That way, we avoid checking the actual project, and
//! only build proc macros and build.rs.
//!
//! Code taken from IntelliJ :0)
//! https://github.com/intellij-rust/intellij-rust/blob/master/native-helper/src/main.rs
use std::{
ffi::OsString,
io,
process::{Command, Stdio},
};
/// ExitCode/ExitStatus are impossible to create :(.
pub(crate) struct ExitCode(pub(crate) Option<i32>);
pub(crate) fn run_rustc_skipping_cargo_checking(
rustc_executable: OsString,
args: Vec<OsString>,
) -> io::Result<ExitCode> {
let is_cargo_check = args.iter().any(|arg| {
let arg = arg.to_string_lossy();
// `cargo check` invokes `rustc` with `--emit=metadata` argument.
//
// https://doc.rust-lang.org/rustc/command-line-arguments.html#--emit-specifies-the-types-of-output-files-to-generate
// link — Generates the crates specified by --crate-type. The default
// output filenames depend on the crate type and platform. This
// is the default if --emit is not specified.
// metadata — Generates a file containing metadata about the crate.
// The default output filename is CRATE_NAME.rmeta.
arg.starts_with("--emit=") && arg.contains("metadata") && !arg.contains("link")
});
if is_cargo_check {
return Ok(ExitCode(Some(0)));
}
run_rustc(rustc_executable, args)
}
fn run_rustc(rustc_executable: OsString, args: Vec<OsString>) -> io::Result<ExitCode> {
let mut child = Command::new(rustc_executable)
.args(args)
.stdin(Stdio::inherit())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?;
Ok(ExitCode(child.wait()?.code()))
}

View File

@ -68,6 +68,7 @@ impl AnalysisStatsCmd {
cargo_config.no_sysroot = self.no_sysroot;
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: self.load_output_dirs,
wrap_rustc: false,
with_proc_macro: self.with_proc_macro,
};
let (host, vfs, _proc_macro) =

View File

@ -34,7 +34,8 @@ pub fn diagnostics(
with_proc_macro: bool,
) -> Result<()> {
let cargo_config = Default::default();
let load_cargo_config = LoadCargoConfig { load_out_dirs_from_check, with_proc_macro };
let load_cargo_config =
LoadCargoConfig { load_out_dirs_from_check, with_proc_macro, wrap_rustc: false };
let (host, _vfs, _proc_macro) =
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?;
let db = host.raw_database();

View File

@ -15,6 +15,7 @@ use crate::reload::{ProjectFolders, SourceRootConfig};
pub struct LoadCargoConfig {
pub load_out_dirs_from_check: bool,
pub wrap_rustc: bool,
pub with_proc_macro: bool,
}
@ -52,7 +53,7 @@ pub fn load_workspace(
};
let build_data = if config.load_out_dirs_from_check {
let mut collector = BuildDataCollector::default();
let mut collector = BuildDataCollector::new(config.wrap_rustc);
ws.collect_build_data_configs(&mut collector);
Some(collector.collect(progress)?)
} else {
@ -136,8 +137,11 @@ mod tests {
fn test_loading_rust_analyzer() -> Result<()> {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
let cargo_config = Default::default();
let load_cargo_config =
LoadCargoConfig { load_out_dirs_from_check: false, with_proc_macro: false };
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: false,
wrap_rustc: false,
with_proc_macro: false,
};
let (host, _vfs, _proc_macro) =
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?;

View File

@ -9,8 +9,11 @@ use ide_ssr::{MatchFinder, SsrPattern, SsrRule};
pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> {
use ide_db::base_db::SourceDatabaseExt;
let cargo_config = Default::default();
let load_cargo_config =
LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true };
let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: true,
wrap_rustc: false,
with_proc_macro: true,
};
let (host, vfs, _proc_macro) =
load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?;
let db = host.raw_database();
@ -37,7 +40,7 @@ pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<Stri
use ide_db::symbol_index::SymbolsDatabase;
let cargo_config = Default::default();
let load_cargo_config =
LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true };
LoadCargoConfig { load_out_dirs_from_check: true, wrap_rustc: true, with_proc_macro: true };
let (host, _vfs, _proc_macro) =
load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?;
let db = host.raw_database();

View File

@ -48,6 +48,9 @@ config_data! {
/// Run build scripts (`build.rs`) for more precise code analysis.
cargo_runBuildScripts |
cargo_loadOutDirsFromCheck: bool = "true",
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
/// avoid compiling unnecessary things.
cargo_useRustcWrapperForBuildScripts: bool = "true",
/// Do not activate the `default` feature.
cargo_noDefaultFeatures: bool = "false",
/// Compilation target (target triple).
@ -493,6 +496,9 @@ impl Config {
pub fn run_build_scripts(&self) -> bool {
self.data.cargo_runBuildScripts || self.data.procMacro_enable
}
pub fn wrap_rustc(&self) -> bool {
self.data.cargo_useRustcWrapperForBuildScripts
}
pub fn cargo(&self) -> CargoConfig {
let rustc_source = self.data.rustcSource.as_ref().map(|rustc_src| {
if rustc_src == "discover" {

View File

@ -236,7 +236,8 @@ impl GlobalState {
let workspaces_updated = !Arc::ptr_eq(&old, &self.workspaces);
if self.config.run_build_scripts() && workspaces_updated {
let mut collector = BuildDataCollector::default();
let mut collector =
BuildDataCollector::new(self.config.wrap_rustc());
for ws in self.workspaces.iter() {
ws.collect_build_data_configs(&mut collector);
}

View File

@ -527,7 +527,7 @@ version = \"0.0.0\"
#[test]
fn out_dirs_check() {
if skip_slow_tests() {
return;
// return;
}
let server = Project::with_fixture(

View File

@ -32,8 +32,12 @@ impl<'a> Project<'a> {
tmp_dir: None,
roots: vec![],
config: serde_json::json!({
"cargo": {
// Loading standard library is costly, let's ignore it by default
"cargo": { "noSysroot": true }
"noSysroot": true,
// Can't use test binary as rustc wrapper.
"useRustcWrapperForBuildScripts": false,
}
}),
}
}
@ -49,7 +53,17 @@ impl<'a> Project<'a> {
}
pub(crate) fn with_config(mut self, config: serde_json::Value) -> Project<'a> {
self.config = config;
fn merge(dst: &mut serde_json::Value, src: serde_json::Value) {
match (dst, src) {
(Value::Object(dst), Value::Object(src)) => {
for (k, v) in src {
merge(dst.entry(k).or_insert(v.clone()), v)
}
}
(dst, src) => *dst = src,
}
}
merge(&mut self.config, config);
self
}

View File

@ -39,6 +39,12 @@ List of features to activate.
--
Run build scripts (`build.rs`) for more precise code analysis.
--
[[rust-analyzer.cargo.useRustcWrapperForBuildScripts]]rust-analyzer.cargo.useRustcWrapperForBuildScripts (default: `true`)::
+
--
Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
avoid compiling unnecessary things.
--
[[rust-analyzer.cargo.noDefaultFeatures]]rust-analyzer.cargo.noDefaultFeatures (default: `false`)::
+
--

View File

@ -434,6 +434,11 @@
"default": true,
"type": "boolean"
},
"rust-analyzer.cargo.useRustcWrapperForBuildScripts": {
"markdownDescription": "Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to\navoid compiling unnecessary things.",
"default": true,
"type": "boolean"
},
"rust-analyzer.cargo.noDefaultFeatures": {
"markdownDescription": "Do not activate the `default` feature.",
"default": false,