Skip to content

Commit e83eb26

Browse files
committed
feat: allow selecting which packages to check for buildscripts
1 parent 24cf957 commit e83eb26

File tree

5 files changed

+53
-16
lines changed

5 files changed

+53
-16
lines changed

crates/project_model/src/build_scripts.rs

+28-15
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,15 @@ pub(crate) struct BuildScriptOutput {
4242
}
4343

4444
impl WorkspaceBuildScripts {
45-
pub(crate) fn run(
46-
config: &CargoConfig,
47-
workspace: &CargoWorkspace,
48-
progress: &dyn Fn(String),
49-
) -> Result<WorkspaceBuildScripts> {
45+
fn build_command(config: &CargoConfig) -> Command {
46+
if let Some([program, args @ ..]) = config.run_build_script_command.as_deref() {
47+
let mut cmd = Command::new(program);
48+
cmd.args(args);
49+
return cmd;
50+
}
51+
5052
let mut cmd = Command::new(toolchain::cargo());
5153

52-
if config.wrap_rustc_in_build_scripts {
53-
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
54-
// that to compile only proc macros and build scripts during the initial
55-
// `cargo check`.
56-
let myself = std::env::current_exe()?;
57-
cmd.env("RUSTC_WRAPPER", myself);
58-
cmd.env("RA_RUSTC_WRAPPER", "1");
59-
}
60-
cmd.current_dir(workspace.workspace_root());
6154
cmd.args(&["check", "--quiet", "--workspace", "--message-format=json"]);
6255

6356
// --all-targets includes tests, benches and examples in addition to the
@@ -81,6 +74,26 @@ impl WorkspaceBuildScripts {
8174
}
8275
}
8376

77+
cmd
78+
}
79+
pub(crate) fn run(
80+
config: &CargoConfig,
81+
workspace: &CargoWorkspace,
82+
progress: &dyn Fn(String),
83+
) -> Result<WorkspaceBuildScripts> {
84+
let mut cmd = Self::build_command(config);
85+
86+
if config.wrap_rustc_in_build_scripts {
87+
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
88+
// that to compile only proc macros and build scripts during the initial
89+
// `cargo check`.
90+
let myself = std::env::current_exe()?;
91+
cmd.env("RUSTC_WRAPPER", myself);
92+
cmd.env("RA_RUSTC_WRAPPER", "1");
93+
}
94+
95+
cmd.current_dir(workspace.workspace_root());
96+
8497
cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());
8598

8699
let mut res = WorkspaceBuildScripts::default();
@@ -104,7 +117,7 @@ impl WorkspaceBuildScripts {
104117
}
105118

106119
// Copy-pasted from existing cargo_metadata. It seems like we
107-
// should be using sered_stacker here?
120+
// should be using serde_stacker here?
108121
let mut deserializer = serde_json::Deserializer::from_str(line);
109122
deserializer.disable_recursion_limit();
110123
let message = Message::deserialize(&mut deserializer)

crates/project_model/src/cargo_workspace.rs

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ pub struct CargoConfig {
9696
pub unset_test_crates: UnsetTestCrates,
9797

9898
pub wrap_rustc_in_build_scripts: bool,
99+
100+
pub run_build_script_command: Option<Vec<String>>,
99101
}
100102

101103
impl CargoConfig {

crates/rust-analyzer/src/config.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ config_data! {
7979
/// Run build scripts (`build.rs`) for more precise code analysis.
8080
cargo_runBuildScripts |
8181
cargo_loadOutDirsFromCheck: bool = "true",
82+
/// Advanced option, fully override the command rust-analyzer uses to
83+
/// run build scripts and build procedural macros. The command should
84+
/// include `--message-format=json` or a similar option.
85+
cargo_runBuildScriptsCommand: Option<Vec<String>> = "null",
8286
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
8387
/// avoid compiling unnecessary things.
8488
cargo_useRustcWrapperForBuildScripts: bool = "true",
@@ -112,7 +116,6 @@ config_data! {
112116
/// checking. The command should include `--message-format=json` or
113117
/// similar option.
114118
checkOnSave_overrideCommand: Option<Vec<String>> = "null",
115-
116119
/// Whether to add argument snippets when completing functions.
117120
/// Only applies when `#rust-analyzer.completion.addCallParenthesis#` is set.
118121
completion_addCallArgumentSnippets: bool = "true",
@@ -803,6 +806,7 @@ impl Config {
803806
rustc_source,
804807
unset_test_crates: UnsetTestCrates::Only(self.data.cargo_unsetTest.clone()),
805808
wrap_rustc_in_build_scripts: self.data.cargo_useRustcWrapperForBuildScripts,
809+
run_build_script_command: self.data.cargo_runBuildScriptsCommand.clone(),
806810
}
807811
}
808812

docs/user/generated_config.adoc

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ List of features to activate.
6464
--
6565
Run build scripts (`build.rs`) for more precise code analysis.
6666
--
67+
[[rust-analyzer.cargo.runBuildScriptsCommand]]rust-analyzer.cargo.runBuildScriptsCommand (default: `null`)::
68+
+
69+
--
70+
Advanced option, fully override the command rust-analyzer uses to
71+
run build scripts and build procedural macros. The command should
72+
include `--message-format=json` or a similar option.
73+
--
6774
[[rust-analyzer.cargo.useRustcWrapperForBuildScripts]]rust-analyzer.cargo.useRustcWrapperForBuildScripts (default: `true`)::
6875
+
6976
--

editors/code/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,17 @@
476476
"default": true,
477477
"type": "boolean"
478478
},
479+
"rust-analyzer.cargo.runBuildScriptsCommand": {
480+
"markdownDescription": "Advanced option, fully override the command rust-analyzer uses to\nrun build scripts and build procedural macros. The command should\ninclude `--message-format=json` or a similar option.",
481+
"default": null,
482+
"type": [
483+
"null",
484+
"array"
485+
],
486+
"items": {
487+
"type": "string"
488+
}
489+
},
479490
"rust-analyzer.cargo.useRustcWrapperForBuildScripts": {
480491
"markdownDescription": "Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to\navoid compiling unnecessary things.",
481492
"default": true,

0 commit comments

Comments
 (0)