mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 22:01:37 +00:00
feat: avoid checking the whole project during initial loading
This commit is contained in:
parent
7be06139b6
commit
186c5c47cb
14 changed files with 251 additions and 124 deletions
|
@ -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");
|
||||
|
|
|
@ -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);
|
||||
|
|
46
crates/rust-analyzer/src/bin/rustc_wrapper.rs
Normal file
46
crates/rust-analyzer/src/bin/rustc_wrapper.rs
Normal 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()))
|
||||
}
|
|
@ -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) =
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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, &|_| {})?;
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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" {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -527,7 +527,7 @@ version = \"0.0.0\"
|
|||
#[test]
|
||||
fn out_dirs_check() {
|
||||
if skip_slow_tests() {
|
||||
return;
|
||||
// return;
|
||||
}
|
||||
|
||||
let server = Project::with_fixture(
|
||||
|
|
|
@ -32,8 +32,12 @@ impl<'a> Project<'a> {
|
|||
tmp_dir: None,
|
||||
roots: vec![],
|
||||
config: serde_json::json!({
|
||||
// Loading standard library is costly, let's ignore it by default
|
||||
"cargo": { "noSysroot": true }
|
||||
"cargo": {
|
||||
// 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> {
|
||||
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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue