From ddce6bb282764692d53b719bff4c37e3512d4556 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Mon, 8 Mar 2021 09:05:19 +0000 Subject: Support disabling rustc build scripts --- crates/project_model/src/workspace.rs | 14 +++++++++++--- crates/rust-analyzer/src/cli/analysis_bench.rs | 3 +++ crates/rust-analyzer/src/cli/analysis_stats.rs | 3 +++ crates/rust-analyzer/src/cli/diagnostics.rs | 8 +++++++- crates/rust-analyzer/src/cli/load_cargo.rs | 10 +++++++--- crates/rust-analyzer/src/cli/ssr.rs | 18 ++++++++++++++---- crates/rust-analyzer/src/config.rs | 5 +++++ crates/rust-analyzer/src/reload.rs | 5 ++++- docs/user/generated_config.adoc | 2 ++ editors/code/package.json | 5 +++++ 10 files changed, 61 insertions(+), 12 deletions(-) diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs index ea0c9ff60..d754c8b55 100644 --- a/crates/project_model/src/workspace.rs +++ b/crates/project_model/src/workspace.rs @@ -56,6 +56,7 @@ impl fmt::Debug for ProjectWorkspace { match self { ProjectWorkspace::Cargo { cargo, sysroot, rustc, rustc_cfg } => f .debug_struct("Cargo") + .field("root", &cargo.workspace_root()) .field("n_packages", &cargo.packages().len()) .field("n_sysroot_crates", &sysroot.crates().len()) .field( @@ -273,12 +274,19 @@ impl ProjectWorkspace { crate_graph } - pub fn collect_build_data_configs(&self, collector: &mut BuildDataCollector) { + pub fn collect_build_data_configs( + &self, + collector: &mut BuildDataCollector, + for_private: bool, + ) { match self { ProjectWorkspace::Cargo { cargo, rustc, .. } => { collector.add_config(&cargo.workspace_root(), cargo.build_data_config().clone()); - if let Some(rustc) = rustc { - collector.add_config(rustc.workspace_root(), rustc.build_data_config().clone()); + if for_private { + if let Some(rustc) = rustc { + collector + .add_config(rustc.workspace_root(), rustc.build_data_config().clone()); + } } } _ => {} diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs index 3bd7e678d..104f2e8a2 100644 --- a/crates/rust-analyzer/src/cli/analysis_bench.rs +++ b/crates/rust-analyzer/src/cli/analysis_bench.rs @@ -68,6 +68,9 @@ impl BenchCmd { let load_cargo_config = LoadCargoConfig { load_out_dirs_from_check: self.load_output_dirs, with_proc_macro: self.with_proc_macro, + // This will currently never have rustcSource set, however if in + // future it does this will handle that case + run_rustc_build_scripts: true, }; let (mut host, vfs, _proc_macro) = load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs index ad0759bda..5a8432960 100644 --- a/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -63,6 +63,9 @@ impl AnalysisStatsCmd { let load_cargo_config = LoadCargoConfig { load_out_dirs_from_check: self.load_output_dirs, with_proc_macro: self.with_proc_macro, + // This will currently never have rustcSource set, however if in + // future it does this will handle that case + run_rustc_build_scripts: true, }; let (host, vfs, _proc_macro) = load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?; diff --git a/crates/rust-analyzer/src/cli/diagnostics.rs b/crates/rust-analyzer/src/cli/diagnostics.rs index 8b985716b..e1966b5a7 100644 --- a/crates/rust-analyzer/src/cli/diagnostics.rs +++ b/crates/rust-analyzer/src/cli/diagnostics.rs @@ -34,7 +34,13 @@ 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, + // This will currently never have rustcSource set, however if in + // future it does this will handle that case + run_rustc_build_scripts: true, + }; let (host, _vfs, _proc_macro) = load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; let db = host.raw_database(); diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index 310c36904..3d7b5031a 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs @@ -14,6 +14,7 @@ use vfs::{loader::Handle, AbsPath, AbsPathBuf}; use crate::reload::{ProjectFolders, SourceRootConfig}; pub struct LoadCargoConfig { + pub run_rustc_build_scripts: bool, pub load_out_dirs_from_check: bool, pub with_proc_macro: bool, } @@ -53,7 +54,7 @@ pub fn load_workspace( let build_data = if config.load_out_dirs_from_check { let mut collector = BuildDataCollector::default(); - ws.collect_build_data_configs(&mut collector); + ws.collect_build_data_configs(&mut collector, config.run_rustc_build_scripts); Some(collector.collect(progress)?) } else { None @@ -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, + with_proc_macro: false, + run_rustc_build_scripts: false, + }; let (host, _vfs, _proc_macro) = load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?; diff --git a/crates/rust-analyzer/src/cli/ssr.rs b/crates/rust-analyzer/src/cli/ssr.rs index 79f426fff..8cc4eefc2 100644 --- a/crates/rust-analyzer/src/cli/ssr.rs +++ b/crates/rust-analyzer/src/cli/ssr.rs @@ -9,8 +9,13 @@ use ide_ssr::{MatchFinder, SsrPattern, SsrRule}; pub fn apply_ssr_rules(rules: Vec) -> 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, + with_proc_macro: true, + // This will currently never have rustcSource set, however if in + // future it does this will handle that case + run_rustc_build_scripts: true, + }; let (host, vfs, _proc_macro) = load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?; let db = host.raw_database(); @@ -36,8 +41,13 @@ pub fn search_for_patterns(patterns: Vec, debug_snippet: Option bool { self.data.cargo_runBuildScripts || self.data.procMacro_enable } + pub fn run_rustc_build_scripts(&self) -> bool { + self.run_build_scripts() && !self.data.cargo_disableRustcBuildScripts + } pub fn cargo(&self) -> CargoConfig { let rustc_source = self.data.rustcSource.as_ref().map(|rustc_src| { if rustc_src == "discover" { diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs index aa8504c3d..da02be2de 100644 --- a/crates/rust-analyzer/src/reload.rs +++ b/crates/rust-analyzer/src/reload.rs @@ -340,7 +340,10 @@ impl GlobalState { if self.config.run_build_scripts() && workspace_build_data.is_none() { let mut collector = BuildDataCollector::default(); for ws in &workspaces { - ws.collect_build_data_configs(&mut collector); + ws.collect_build_data_configs( + &mut collector, + self.config.run_rustc_build_scripts(), + ); } self.fetch_build_data_request(collector) } diff --git a/docs/user/generated_config.adoc b/docs/user/generated_config.adoc index 1dbf2a611..602c9432e 100644 --- a/docs/user/generated_config.adoc +++ b/docs/user/generated_config.adoc @@ -12,6 +12,8 @@ List of features to activate. [[rust-analyzer.cargo.runBuildScripts]]rust-analyzer.cargo.runBuildScripts (default: `false`):: Run build scripts (`build.rs`) for more precise code analysis. +[[rust-analyzer.cargo.disableRustcBuildScripts]]rust-analyzer.cargo.disableRustcBuildScripts (default: `false`):: + Disable running build scripts (`build.rs`) for the `rustc_private` crates in `rustcSource`. [[rust-analyzer.cargo.noDefaultFeatures]]rust-analyzer.cargo.noDefaultFeatures (default: `false`):: Do not activate the `default` feature. [[rust-analyzer.cargo.target]]rust-analyzer.cargo.target (default: `null`):: diff --git a/editors/code/package.json b/editors/code/package.json index 1987364bc..82011bb44 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -413,6 +413,11 @@ "default": false, "type": "boolean" }, + "rust-analyzer.cargo.disableRustcBuildScripts": { + "markdownDescription": "Disable running build scripts (`build.rs`) for the `rustc_private` crates in `rustcSource`.", + "default": false, + "type": "boolean" + }, "rust-analyzer.cargo.noDefaultFeatures": { "markdownDescription": "Do not activate the `default` feature.", "default": false, -- cgit v1.2.3