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 {} impl Eq for BuildDataConfig {}
#[derive(Debug, Default)] #[derive(Debug)]
pub struct BuildDataCollector { pub struct BuildDataCollector {
wrap_rustc: bool,
configs: FxHashMap<AbsPathBuf, BuildDataConfig>, configs: FxHashMap<AbsPathBuf, BuildDataConfig>,
} }
impl BuildDataCollector { 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) { pub(crate) fn add_config(&mut self, workspace_root: &AbsPath, config: BuildDataConfig) {
self.configs.insert(workspace_root.to_path_buf(), config); 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> { pub fn collect(&mut self, progress: &dyn Fn(String)) -> Result<BuildDataResult> {
let mut res = BuildDataResult::default(); let mut res = BuildDataResult::default();
for (path, config) in self.configs.iter() { for (path, config) in self.configs.iter() {
res.per_workspace.insert( let workspace_build_data = WorkspaceBuildData::collect(
path.clone(), &config.cargo_toml,
collect_from_workspace( &config.cargo_features,
&config.cargo_toml, &config.packages,
&config.cargo_features, self.wrap_rustc,
&config.packages, progress,
progress, )?;
)?, res.per_workspace.insert(path.clone(), workspace_build_data);
);
} }
Ok(res) Ok(res)
} }
@ -120,119 +124,137 @@ impl BuildDataConfig {
} }
} }
fn collect_from_workspace( impl WorkspaceBuildData {
cargo_toml: &AbsPath, fn collect(
cargo_features: &CargoConfig, cargo_toml: &AbsPath,
packages: &Vec<cargo_metadata::Package>, cargo_features: &CargoConfig,
progress: &dyn Fn(String), packages: &Vec<cargo_metadata::Package>,
) -> Result<WorkspaceBuildData> { wrap_rustc: bool,
let mut cmd = Command::new(toolchain::cargo()); progress: &dyn Fn(String),
cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"]) ) -> Result<WorkspaceBuildData> {
.arg(cargo_toml.as_ref()); let mut cmd = Command::new(toolchain::cargo());
// --all-targets includes tests, benches and examples in addition to the if wrap_rustc {
// default lib and bins. This is an independent concept from the --targets // Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
// flag below. // that to compile only proc macros and build scripts during the initial
cmd.arg("--all-targets"); // `cargo check`.
let myself = std::env::current_exe()?;
if let Some(target) = &cargo_features.target { cmd.env("RUSTC_WRAPPER", myself);
cmd.args(&["--target", target]); cmd.env("RA_RUSTC_WRAPPER", "1");
}
if cargo_features.all_features {
cmd.arg("--all-features");
} else {
if cargo_features.no_default_features {
// FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
// https://github.com/oli-obk/cargo_metadata/issues/79
cmd.arg("--no-default-features");
} }
if !cargo_features.features.is_empty() {
cmd.arg("--features"); cmd.args(&["check", "--workspace", "--message-format=json", "--manifest-path"])
cmd.arg(cargo_features.features.join(" ")); .arg(cargo_toml.as_ref());
// --all-targets includes tests, benches and examples in addition to the
// default lib and bins. This is an independent concept from the --targets
// flag below.
cmd.arg("--all-targets");
if let Some(target) = &cargo_features.target {
cmd.args(&["--target", target]);
} }
}
cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null()); if cargo_features.all_features {
cmd.arg("--all-features");
let mut child = cmd.spawn().map(JodChild)?; } else {
let child_stdout = child.stdout.take().unwrap(); if cargo_features.no_default_features {
let stdout = BufReader::new(child_stdout); // FIXME: `NoDefaultFeatures` is mutual exclusive with `SomeFeatures`
// https://github.com/oli-obk/cargo_metadata/issues/79
let mut res = WorkspaceBuildData::default(); cmd.arg("--no-default-features");
for message in cargo_metadata::Message::parse_stream(stdout).flatten() {
match message {
Message::BuildScriptExecuted(BuildScript {
package_id, out_dir, cfgs, env, ..
}) => {
let cfgs = {
let mut acc = Vec::new();
for cfg in cfgs {
match cfg.parse::<CfgFlag>() {
Ok(it) => acc.push(it),
Err(err) => {
anyhow::bail!("invalid cfg from cargo-metadata: {}", err)
}
};
}
acc
};
let package_build_data =
res.per_package.entry(package_id.repr.clone()).or_default();
// cargo_metadata crate returns default (empty) path for
// older cargos, which is not absolute, so work around that.
if !out_dir.as_str().is_empty() {
let out_dir = AbsPathBuf::assert(PathBuf::from(out_dir.into_os_string()));
package_build_data.out_dir = Some(out_dir);
package_build_data.cfgs = cfgs;
}
package_build_data.envs = env;
} }
Message::CompilerArtifact(message) => { if !cargo_features.features.is_empty() {
progress(format!("metadata {}", message.target.name)); cmd.arg("--features");
cmd.arg(cargo_features.features.join(" "));
}
}
if message.target.kind.contains(&"proc-macro".to_string()) { cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());
let package_id = message.package_id;
// Skip rmeta file let mut child = cmd.spawn().map(JodChild)?;
if let Some(filename) = message.filenames.iter().find(|name| is_dylib(name)) { let child_stdout = child.stdout.take().unwrap();
let filename = AbsPathBuf::assert(PathBuf::from(&filename)); let stdout = BufReader::new(child_stdout);
let package_build_data =
res.per_package.entry(package_id.repr.clone()).or_default(); let mut res = WorkspaceBuildData::default();
package_build_data.proc_macro_dylib_path = Some(filename); for message in cargo_metadata::Message::parse_stream(stdout).flatten() {
match message {
Message::BuildScriptExecuted(BuildScript {
package_id,
out_dir,
cfgs,
env,
..
}) => {
let cfgs = {
let mut acc = Vec::new();
for cfg in cfgs {
match cfg.parse::<CfgFlag>() {
Ok(it) => acc.push(it),
Err(err) => {
anyhow::bail!("invalid cfg from cargo-metadata: {}", err)
}
};
}
acc
};
let package_build_data =
res.per_package.entry(package_id.repr.clone()).or_default();
// cargo_metadata crate returns default (empty) path for
// older cargos, which is not absolute, so work around that.
if !out_dir.as_str().is_empty() {
let out_dir = AbsPathBuf::assert(PathBuf::from(out_dir.into_os_string()));
package_build_data.out_dir = Some(out_dir);
package_build_data.cfgs = cfgs;
}
package_build_data.envs = env;
}
Message::CompilerArtifact(message) => {
progress(format!("metadata {}", message.target.name));
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))
{
let filename = AbsPathBuf::assert(PathBuf::from(&filename));
let package_build_data =
res.per_package.entry(package_id.repr.clone()).or_default();
package_build_data.proc_macro_dylib_path = Some(filename);
}
} }
} }
} Message::CompilerMessage(message) => {
Message::CompilerMessage(message) => { progress(message.target.name.clone());
progress(message.target.name.clone()); }
} Message::BuildFinished(_) => {}
Message::BuildFinished(_) => {} Message::TextLine(_) => {}
Message::TextLine(_) => {} _ => {}
_ => {}
}
}
for package in packages {
let package_build_data = res.per_package.entry(package.id.repr.clone()).or_default();
inject_cargo_env(package, package_build_data);
if let Some(out_dir) = &package_build_data.out_dir {
// NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
package_build_data.envs.push(("OUT_DIR".to_string(), out_dir));
} }
} }
}
let output = child.into_inner().wait_with_output()?; for package in packages {
if !output.status.success() { let package_build_data = res.per_package.entry(package.id.repr.clone()).or_default();
let mut stderr = String::from_utf8(output.stderr).unwrap_or_default(); inject_cargo_env(package, package_build_data);
if stderr.is_empty() { if let Some(out_dir) = &package_build_data.out_dir {
stderr = "cargo check failed".to_string(); // NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
package_build_data.envs.push(("OUT_DIR".to_string(), out_dir));
}
}
} }
res.error = Some(stderr)
}
Ok(res) let output = child.into_inner().wait_with_output()?;
if !output.status.success() {
let mut stderr = String::from_utf8(output.stderr).unwrap_or_default();
if stderr.is_empty() {
stderr = "cargo check failed".to_string();
}
res.error = Some(stderr)
}
Ok(res)
}
} }
// FIXME: File a better way to know if it is a dylib // FIXME: File a better way to know if it is a dylib

View File

@ -30,8 +30,11 @@ fn benchmark_integrated_highlighting() {
let file = "./crates/ide_db/src/apply_change.rs"; let file = "./crates/ide_db/src/apply_change.rs";
let cargo_config = Default::default(); let cargo_config = Default::default();
let load_cargo_config = let load_cargo_config = LoadCargoConfig {
LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: false }; load_out_dirs_from_check: true,
wrap_rustc: false,
with_proc_macro: false,
};
let (mut host, vfs, _proc_macro) = { let (mut host, vfs, _proc_macro) = {
let _it = stdx::timeit("workspace loading"); 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 //! Based on cli flags, either spawns an LSP server, or runs a batch analysis
mod flags; mod flags;
mod logger; mod logger;
mod rustc_wrapper;
use std::{convert::TryFrom, env, fs, path::Path, process}; 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; static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
fn main() { 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() { if let Err(err) = try_main() {
log::error!("Unexpected error: {}", err); log::error!("Unexpected error: {}", err);
eprintln!("{}", 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; cargo_config.no_sysroot = self.no_sysroot;
let load_cargo_config = LoadCargoConfig { let load_cargo_config = LoadCargoConfig {
load_out_dirs_from_check: self.load_output_dirs, load_out_dirs_from_check: self.load_output_dirs,
wrap_rustc: false,
with_proc_macro: self.with_proc_macro, with_proc_macro: self.with_proc_macro,
}; };
let (host, vfs, _proc_macro) = let (host, vfs, _proc_macro) =

View File

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

View File

@ -15,6 +15,7 @@ use crate::reload::{ProjectFolders, SourceRootConfig};
pub struct LoadCargoConfig { pub struct LoadCargoConfig {
pub load_out_dirs_from_check: bool, pub load_out_dirs_from_check: bool,
pub wrap_rustc: bool,
pub with_proc_macro: 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 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); ws.collect_build_data_configs(&mut collector);
Some(collector.collect(progress)?) Some(collector.collect(progress)?)
} else { } else {
@ -136,8 +137,11 @@ mod tests {
fn test_loading_rust_analyzer() -> Result<()> { fn test_loading_rust_analyzer() -> Result<()> {
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap(); let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
let cargo_config = Default::default(); let cargo_config = Default::default();
let load_cargo_config = let load_cargo_config = LoadCargoConfig {
LoadCargoConfig { load_out_dirs_from_check: false, with_proc_macro: false }; load_out_dirs_from_check: false,
wrap_rustc: false,
with_proc_macro: false,
};
let (host, _vfs, _proc_macro) = let (host, _vfs, _proc_macro) =
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; 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<()> { pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> {
use ide_db::base_db::SourceDatabaseExt; use ide_db::base_db::SourceDatabaseExt;
let cargo_config = Default::default(); let cargo_config = Default::default();
let load_cargo_config = let load_cargo_config = LoadCargoConfig {
LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true }; load_out_dirs_from_check: true,
wrap_rustc: false,
with_proc_macro: true,
};
let (host, vfs, _proc_macro) = let (host, vfs, _proc_macro) =
load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?;
let db = host.raw_database(); 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; use ide_db::symbol_index::SymbolsDatabase;
let cargo_config = Default::default(); let cargo_config = Default::default();
let load_cargo_config = 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) = let (host, _vfs, _proc_macro) =
load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?;
let db = host.raw_database(); let db = host.raw_database();

View File

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

View File

@ -236,7 +236,8 @@ impl GlobalState {
let workspaces_updated = !Arc::ptr_eq(&old, &self.workspaces); let workspaces_updated = !Arc::ptr_eq(&old, &self.workspaces);
if self.config.run_build_scripts() && workspaces_updated { 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() { for ws in self.workspaces.iter() {
ws.collect_build_data_configs(&mut collector); ws.collect_build_data_configs(&mut collector);
} }

View File

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

View File

@ -32,8 +32,12 @@ impl<'a> Project<'a> {
tmp_dir: None, tmp_dir: None,
roots: vec![], roots: vec![],
config: serde_json::json!({ config: serde_json::json!({
// Loading standard library is costly, let's ignore it by default "cargo": {
"cargo": { "noSysroot": true } // Loading standard library is costly, let's ignore it by default
"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> { 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 self
} }

View File

@ -39,6 +39,12 @@ List of features to activate.
-- --
Run build scripts (`build.rs`) for more precise code analysis. 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`):: [[rust-analyzer.cargo.noDefaultFeatures]]rust-analyzer.cargo.noDefaultFeatures (default: `false`)::
+ +
-- --

View File

@ -434,6 +434,11 @@
"default": true, "default": true,
"type": "boolean" "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": { "rust-analyzer.cargo.noDefaultFeatures": {
"markdownDescription": "Do not activate the `default` feature.", "markdownDescription": "Do not activate the `default` feature.",
"default": false, "default": false,