Skip to content

Commit 15844bf

Browse files
committed
Auto merge of #11956 - fee1-dead:master, r=flodiebold
feat: allow customizing the command for running build scripts I have tested this locally and it fixed #9201 with some small changes on the compiler side with suggestions from #9201 (comment). I have also added an environment variable `IS_RA_BUILDSCRIPT_CHECK` for crates to detect that it is a check for buildscripts, and allows defaulting to bogus values for expected environment variables.
2 parents 8ab26f6 + 73a033e commit 15844bf

File tree

5 files changed

+53
-15
lines changed

5 files changed

+53
-15
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
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ config_data! {
7878
/// Run build scripts (`build.rs`) for more precise code analysis.
7979
cargo_runBuildScripts |
8080
cargo_loadOutDirsFromCheck: bool = "true",
81+
/// Advanced option, fully override the command rust-analyzer uses to
82+
/// run build scripts and build procedural macros. The command should
83+
/// include `--message-format=json` or a similar option.
84+
cargo_runBuildScriptsCommand: Option<Vec<String>> = "null",
8185
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
8286
/// avoid compiling unnecessary things.
8387
cargo_useRustcWrapperForBuildScripts: bool = "true",
@@ -834,6 +838,7 @@ impl Config {
834838
rustc_source,
835839
unset_test_crates: UnsetTestCrates::Only(self.data.cargo_unsetTest.clone()),
836840
wrap_rustc_in_build_scripts: self.data.cargo_useRustcWrapperForBuildScripts,
841+
run_build_script_command: self.data.cargo_runBuildScriptsCommand.clone(),
837842
}
838843
}
839844

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)