Skip to content

Commit 4899e0b

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

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

crates/project_model/src/build_scripts.rs

Lines changed: 28 additions & 15 deletions
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.check_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

Lines changed: 2 additions & 0 deletions
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 check_build_script_command: Option<Vec<String>>,
99101
}
100102

101103
impl CargoConfig {

crates/rust-analyzer/src/config.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ config_data! {
8989
/// Internal config for debugging, disables loading of sysroot crates.
9090
cargo_noSysroot: bool = "false",
9191

92+
/// Advanced option, fully override the command rust-analyzer uses for
93+
/// checking buildscripts and procedural macros. The command should
94+
/// include `--message-format=json` or similar option.
95+
checkBuildScriptOnSave_overrideCommand: Option<Vec<String>> = "null",
96+
9297
/// Run specified `cargo check` command for diagnostics on save.
9398
checkOnSave_enable: bool = "true",
9499
/// Check with all features (`--all-features`).
@@ -112,7 +117,6 @@ config_data! {
112117
/// checking. The command should include `--message-format=json` or
113118
/// similar option.
114119
checkOnSave_overrideCommand: Option<Vec<String>> = "null",
115-
116120
/// Whether to add argument snippets when completing functions.
117121
/// Only applies when `#rust-analyzer.completion.addCallParenthesis#` is set.
118122
completion_addCallArgumentSnippets: bool = "true",
@@ -803,6 +807,7 @@ impl Config {
803807
rustc_source,
804808
unset_test_crates: UnsetTestCrates::Only(self.data.cargo_unsetTest.clone()),
805809
wrap_rustc_in_build_scripts: self.data.cargo_useRustcWrapperForBuildScripts,
810+
check_build_script_command: self.data.checkBuildScriptOnSave_overrideCommand.clone(),
806811
}
807812
}
808813

docs/user/generated_config.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ Compilation target (target triple).
8585
--
8686
Internal config for debugging, disables loading of sysroot crates.
8787
--
88+
[[rust-analyzer.checkBuildScriptOnSave.overrideCommand]]rust-analyzer.checkBuildScriptOnSave.overrideCommand (default: `null`)::
89+
+
90+
--
91+
Advanced option, fully override the command rust-analyzer uses for
92+
checking buildscripts and procedural macros. The command should
93+
include `--message-format=json` or similar option.
94+
--
8895
[[rust-analyzer.checkOnSave.enable]]rust-analyzer.checkOnSave.enable (default: `true`)::
8996
+
9097
--

editors/code/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,17 @@
499499
"default": false,
500500
"type": "boolean"
501501
},
502+
"rust-analyzer.checkBuildScriptOnSave.overrideCommand": {
503+
"markdownDescription": "Advanced option, fully override the command rust-analyzer uses for\nchecking buildscripts and procedural macros. The command should\ninclude `--message-format=json` or similar option.",
504+
"default": null,
505+
"type": [
506+
"null",
507+
"array"
508+
],
509+
"items": {
510+
"type": "string"
511+
}
512+
},
502513
"rust-analyzer.checkOnSave.enable": {
503514
"markdownDescription": "Run specified `cargo check` command for diagnostics on save.",
504515
"default": true,

0 commit comments

Comments
 (0)