Skip to content

Commit

Permalink
feat: allow customizing the command for running build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
fee1-dead committed Apr 13, 2022
1 parent 24cf957 commit 73a033e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
43 changes: 28 additions & 15 deletions crates/project_model/src/build_scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,15 @@ pub(crate) struct BuildScriptOutput {
}

impl WorkspaceBuildScripts {
pub(crate) fn run(
config: &CargoConfig,
workspace: &CargoWorkspace,
progress: &dyn Fn(String),
) -> Result<WorkspaceBuildScripts> {
fn build_command(config: &CargoConfig) -> Command {
if let Some([program, args @ ..]) = config.run_build_script_command.as_deref() {
let mut cmd = Command::new(program);
cmd.args(args);
return cmd;
}

let mut cmd = Command::new(toolchain::cargo());

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

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

cmd
}
pub(crate) fn run(
config: &CargoConfig,
workspace: &CargoWorkspace,
progress: &dyn Fn(String),
) -> Result<WorkspaceBuildScripts> {
let mut cmd = Self::build_command(config);

if config.wrap_rustc_in_build_scripts {
// Setup RUSTC_WRAPPER to point to `rust-analyzer` binary itself. We use
// that to compile only proc macros and build scripts during the initial
// `cargo check`.
let myself = std::env::current_exe()?;
cmd.env("RUSTC_WRAPPER", myself);
cmd.env("RA_RUSTC_WRAPPER", "1");
}

cmd.current_dir(workspace.workspace_root());

cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).stdin(Stdio::null());

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

// Copy-pasted from existing cargo_metadata. It seems like we
// should be using sered_stacker here?
// should be using serde_stacker here?
let mut deserializer = serde_json::Deserializer::from_str(line);
deserializer.disable_recursion_limit();
let message = Message::deserialize(&mut deserializer)
Expand Down
2 changes: 2 additions & 0 deletions crates/project_model/src/cargo_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ pub struct CargoConfig {
pub unset_test_crates: UnsetTestCrates,

pub wrap_rustc_in_build_scripts: bool,

pub run_build_script_command: Option<Vec<String>>,
}

impl CargoConfig {
Expand Down
5 changes: 5 additions & 0 deletions crates/rust-analyzer/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ config_data! {
/// Run build scripts (`build.rs`) for more precise code analysis.
cargo_runBuildScripts |
cargo_loadOutDirsFromCheck: bool = "true",
/// Advanced option, fully override the command rust-analyzer uses to
/// run build scripts and build procedural macros. The command should
/// include `--message-format=json` or a similar option.
cargo_runBuildScriptsCommand: Option<Vec<String>> = "null",
/// Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to
/// avoid compiling unnecessary things.
cargo_useRustcWrapperForBuildScripts: bool = "true",
Expand Down Expand Up @@ -803,6 +807,7 @@ impl Config {
rustc_source,
unset_test_crates: UnsetTestCrates::Only(self.data.cargo_unsetTest.clone()),
wrap_rustc_in_build_scripts: self.data.cargo_useRustcWrapperForBuildScripts,
run_build_script_command: self.data.cargo_runBuildScriptsCommand.clone(),
}
}

Expand Down
7 changes: 7 additions & 0 deletions docs/user/generated_config.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ List of features to activate.
--
Run build scripts (`build.rs`) for more precise code analysis.
--
[[rust-analyzer.cargo.runBuildScriptsCommand]]rust-analyzer.cargo.runBuildScriptsCommand (default: `null`)::
+
--
Advanced option, fully override the command rust-analyzer uses to
run build scripts and build procedural macros. The command should
include `--message-format=json` or a similar option.
--
[[rust-analyzer.cargo.useRustcWrapperForBuildScripts]]rust-analyzer.cargo.useRustcWrapperForBuildScripts (default: `true`)::
+
--
Expand Down
11 changes: 11 additions & 0 deletions editors/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,17 @@
"default": true,
"type": "boolean"
},
"rust-analyzer.cargo.runBuildScriptsCommand": {
"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.",
"default": null,
"type": [
"null",
"array"
],
"items": {
"type": "string"
}
},
"rust-analyzer.cargo.useRustcWrapperForBuildScripts": {
"markdownDescription": "Use `RUSTC_WRAPPER=rust-analyzer` when running build scripts to\navoid compiling unnecessary things.",
"default": true,
Expand Down

0 comments on commit 73a033e

Please sign in to comment.