Skip to content

Commit ddce6bb

Browse files
committed
Support disabling rustc build scripts
1 parent 877f745 commit ddce6bb

File tree

10 files changed

+61
-12
lines changed

10 files changed

+61
-12
lines changed

crates/project_model/src/workspace.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ impl fmt::Debug for ProjectWorkspace {
5656
match self {
5757
ProjectWorkspace::Cargo { cargo, sysroot, rustc, rustc_cfg } => f
5858
.debug_struct("Cargo")
59+
.field("root", &cargo.workspace_root())
5960
.field("n_packages", &cargo.packages().len())
6061
.field("n_sysroot_crates", &sysroot.crates().len())
6162
.field(
@@ -273,12 +274,19 @@ impl ProjectWorkspace {
273274
crate_graph
274275
}
275276

276-
pub fn collect_build_data_configs(&self, collector: &mut BuildDataCollector) {
277+
pub fn collect_build_data_configs(
278+
&self,
279+
collector: &mut BuildDataCollector,
280+
for_private: bool,
281+
) {
277282
match self {
278283
ProjectWorkspace::Cargo { cargo, rustc, .. } => {
279284
collector.add_config(&cargo.workspace_root(), cargo.build_data_config().clone());
280-
if let Some(rustc) = rustc {
281-
collector.add_config(rustc.workspace_root(), rustc.build_data_config().clone());
285+
if for_private {
286+
if let Some(rustc) = rustc {
287+
collector
288+
.add_config(rustc.workspace_root(), rustc.build_data_config().clone());
289+
}
282290
}
283291
}
284292
_ => {}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ impl BenchCmd {
6868
let load_cargo_config = LoadCargoConfig {
6969
load_out_dirs_from_check: self.load_output_dirs,
7070
with_proc_macro: self.with_proc_macro,
71+
// This will currently never have rustcSource set, however if in
72+
// future it does this will handle that case
73+
run_rustc_build_scripts: true,
7174
};
7275
let (mut host, vfs, _proc_macro) =
7376
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ impl AnalysisStatsCmd {
6363
let load_cargo_config = LoadCargoConfig {
6464
load_out_dirs_from_check: self.load_output_dirs,
6565
with_proc_macro: self.with_proc_macro,
66+
// This will currently never have rustcSource set, however if in
67+
// future it does this will handle that case
68+
run_rustc_build_scripts: true,
6669
};
6770
let (host, vfs, _proc_macro) =
6871
load_workspace_at(&self.path, &cargo_config, &load_cargo_config, &|_| {})?;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ pub fn diagnostics(
3434
with_proc_macro: bool,
3535
) -> Result<()> {
3636
let cargo_config = Default::default();
37-
let load_cargo_config = LoadCargoConfig { load_out_dirs_from_check, with_proc_macro };
37+
let load_cargo_config = LoadCargoConfig {
38+
load_out_dirs_from_check,
39+
with_proc_macro,
40+
// This will currently never have rustcSource set, however if in
41+
// future it does this will handle that case
42+
run_rustc_build_scripts: true,
43+
};
3844
let (host, _vfs, _proc_macro) =
3945
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?;
4046
let db = host.raw_database();

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use vfs::{loader::Handle, AbsPath, AbsPathBuf};
1414
use crate::reload::{ProjectFolders, SourceRootConfig};
1515

1616
pub struct LoadCargoConfig {
17+
pub run_rustc_build_scripts: bool,
1718
pub load_out_dirs_from_check: bool,
1819
pub with_proc_macro: bool,
1920
}
@@ -53,7 +54,7 @@ pub fn load_workspace(
5354

5455
let build_data = if config.load_out_dirs_from_check {
5556
let mut collector = BuildDataCollector::default();
56-
ws.collect_build_data_configs(&mut collector);
57+
ws.collect_build_data_configs(&mut collector, config.run_rustc_build_scripts);
5758
Some(collector.collect(progress)?)
5859
} else {
5960
None
@@ -136,8 +137,11 @@ mod tests {
136137
fn test_loading_rust_analyzer() -> Result<()> {
137138
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
138139
let cargo_config = Default::default();
139-
let load_cargo_config =
140-
LoadCargoConfig { load_out_dirs_from_check: false, with_proc_macro: false };
140+
let load_cargo_config = LoadCargoConfig {
141+
load_out_dirs_from_check: false,
142+
with_proc_macro: false,
143+
run_rustc_build_scripts: false,
144+
};
141145
let (host, _vfs, _proc_macro) =
142146
load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?;
143147

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ use ide_ssr::{MatchFinder, SsrPattern, SsrRule};
99
pub fn apply_ssr_rules(rules: Vec<SsrRule>) -> Result<()> {
1010
use ide_db::base_db::SourceDatabaseExt;
1111
let cargo_config = Default::default();
12-
let load_cargo_config =
13-
LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true };
12+
let load_cargo_config = LoadCargoConfig {
13+
load_out_dirs_from_check: true,
14+
with_proc_macro: true,
15+
// This will currently never have rustcSource set, however if in
16+
// future it does this will handle that case
17+
run_rustc_build_scripts: true,
18+
};
1419
let (host, vfs, _proc_macro) =
1520
load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?;
1621
let db = host.raw_database();
@@ -36,8 +41,13 @@ pub fn search_for_patterns(patterns: Vec<SsrPattern>, debug_snippet: Option<Stri
3641
use ide_db::base_db::SourceDatabaseExt;
3742
use ide_db::symbol_index::SymbolsDatabase;
3843
let cargo_config = Default::default();
39-
let load_cargo_config =
40-
LoadCargoConfig { load_out_dirs_from_check: true, with_proc_macro: true };
44+
let load_cargo_config = LoadCargoConfig {
45+
load_out_dirs_from_check: true,
46+
with_proc_macro: true,
47+
// This will currently never have rustcSource set, however if in
48+
// future it does this will handle that case
49+
run_rustc_build_scripts: true,
50+
};
4151
let (host, _vfs, _proc_macro) =
4252
load_workspace_at(&std::env::current_dir()?, &cargo_config, &load_cargo_config, &|_| {})?;
4353
let db = host.raw_database();

crates/rust-analyzer/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ config_data! {
4949
/// Run build scripts (`build.rs`) for more precise code analysis.
5050
cargo_runBuildScripts |
5151
cargo_loadOutDirsFromCheck: bool = "false",
52+
/// Disable running build scripts (`build.rs`) for the `rustc_private` crates in `rustcSource`.
53+
cargo_disableRustcBuildScripts: bool = "false",
5254
/// Do not activate the `default` feature.
5355
cargo_noDefaultFeatures: bool = "false",
5456
/// Compilation target (target triple).
@@ -482,6 +484,9 @@ impl Config {
482484
pub fn run_build_scripts(&self) -> bool {
483485
self.data.cargo_runBuildScripts || self.data.procMacro_enable
484486
}
487+
pub fn run_rustc_build_scripts(&self) -> bool {
488+
self.run_build_scripts() && !self.data.cargo_disableRustcBuildScripts
489+
}
485490
pub fn cargo(&self) -> CargoConfig {
486491
let rustc_source = self.data.rustcSource.as_ref().map(|rustc_src| {
487492
if rustc_src == "discover" {

crates/rust-analyzer/src/reload.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,10 @@ impl GlobalState {
340340
if self.config.run_build_scripts() && workspace_build_data.is_none() {
341341
let mut collector = BuildDataCollector::default();
342342
for ws in &workspaces {
343-
ws.collect_build_data_configs(&mut collector);
343+
ws.collect_build_data_configs(
344+
&mut collector,
345+
self.config.run_rustc_build_scripts(),
346+
);
344347
}
345348
self.fetch_build_data_request(collector)
346349
}

docs/user/generated_config.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
List of features to activate.
1313
[[rust-analyzer.cargo.runBuildScripts]]rust-analyzer.cargo.runBuildScripts (default: `false`)::
1414
Run build scripts (`build.rs`) for more precise code analysis.
15+
[[rust-analyzer.cargo.disableRustcBuildScripts]]rust-analyzer.cargo.disableRustcBuildScripts (default: `false`)::
16+
Disable running build scripts (`build.rs`) for the `rustc_private` crates in `rustcSource`.
1517
[[rust-analyzer.cargo.noDefaultFeatures]]rust-analyzer.cargo.noDefaultFeatures (default: `false`)::
1618
Do not activate the `default` feature.
1719
[[rust-analyzer.cargo.target]]rust-analyzer.cargo.target (default: `null`)::

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@
413413
"default": false,
414414
"type": "boolean"
415415
},
416+
"rust-analyzer.cargo.disableRustcBuildScripts": {
417+
"markdownDescription": "Disable running build scripts (`build.rs`) for the `rustc_private` crates in `rustcSource`.",
418+
"default": false,
419+
"type": "boolean"
420+
},
416421
"rust-analyzer.cargo.noDefaultFeatures": {
417422
"markdownDescription": "Do not activate the `default` feature.",
418423
"default": false,

0 commit comments

Comments
 (0)