From d8ef7e4a3fdbea1537174ae4accb7b814cdde926 Mon Sep 17 00:00:00 2001 From: Bogdan Mircea Date: Tue, 10 Dec 2024 18:15:54 +0100 Subject: [PATCH] allow passing in a config group --- tools/rust_analyzer/aquery.rs | 2 ++ .../bin/discover_rust_project.rs | 14 ++++++-- tools/rust_analyzer/bin/gen_rust_project.rs | 11 +++++- tools/rust_analyzer/config.rs | 35 +++++++++++-------- tools/rust_analyzer/lib.rs | 8 +++++ tools/rust_analyzer/ra_arg.rs | 20 ++++++++--- 6 files changed, 68 insertions(+), 22 deletions(-) diff --git a/tools/rust_analyzer/aquery.rs b/tools/rust_analyzer/aquery.rs index d9e0d7dd39..99ca6cc98d 100644 --- a/tools/rust_analyzer/aquery.rs +++ b/tools/rust_analyzer/aquery.rs @@ -85,6 +85,7 @@ pub fn get_crate_specs( output_base: &Utf8Path, workspace: &Utf8Path, execution_root: &Utf8Path, + config_group: Option<&str>, targets: &[String], rules_rust_name: &str, ) -> anyhow::Result> { @@ -99,6 +100,7 @@ pub fn get_crate_specs( .env_remove("BUILD_WORKSPACE_DIRECTORY") .arg(format!("--output_base={output_base}")) .arg("aquery") + .args(config_group.map(|s| format!("--config={s}"))) .arg("--include_aspects") .arg("--include_artifacts") // This just makes the `rust-analyzer` integration more resilient, diff --git a/tools/rust_analyzer/bin/discover_rust_project.rs b/tools/rust_analyzer/bin/discover_rust_project.rs index 7513bfce08..9fb9283c71 100644 --- a/tools/rust_analyzer/bin/discover_rust_project.rs +++ b/tools/rust_analyzer/bin/discover_rust_project.rs @@ -28,6 +28,7 @@ fn project_discovery() -> anyhow::Result<()> { execution_root, output_base, bazel, + config_group, specific, } = Config::parse_and_refine()?; @@ -45,13 +46,21 @@ fn project_discovery() -> anyhow::Result<()> { log::info!("got rust-analyzer argument: {ra_arg}"); - let (buildfile, targets) = ra_arg.query_target_details(&bazel, &output_base, &workspace)?; + let (buildfile, targets) = + ra_arg.query_target_details(&bazel, &output_base, &workspace, config_group.as_deref())?; log::debug!("got buildfile: {buildfile}"); log::debug!("got targets: {targets:?}"); // Generate the crate specs. - generate_crate_info(&bazel, &output_base, &workspace, rules_rust_name, &targets)?; + generate_crate_info( + &bazel, + &output_base, + &workspace, + config_group.as_deref(), + rules_rust_name, + &targets, + )?; // Use the generated files to print the rust-project.json. discover_rust_project( @@ -59,6 +68,7 @@ fn project_discovery() -> anyhow::Result<()> { &output_base, &workspace, &execution_root, + config_group.as_deref(), rules_rust_name, &targets, buildfile, diff --git a/tools/rust_analyzer/bin/gen_rust_project.rs b/tools/rust_analyzer/bin/gen_rust_project.rs index 09bed9a42b..6a821a6fcd 100644 --- a/tools/rust_analyzer/bin/gen_rust_project.rs +++ b/tools/rust_analyzer/bin/gen_rust_project.rs @@ -21,6 +21,7 @@ fn main() -> anyhow::Result<()> { execution_root, output_base, bazel, + config_group, specific, } = Config::parse_and_refine()?; @@ -29,7 +30,14 @@ fn main() -> anyhow::Result<()> { let rules_rust_name = env!("ASPECT_REPOSITORY"); // Generate the crate specs. - generate_crate_info(&bazel, &output_base, &workspace, rules_rust_name, &targets)?; + generate_crate_info( + &bazel, + &output_base, + &workspace, + config_group.as_deref(), + rules_rust_name, + &targets, + )?; // Use the generated files to write rust-project.json. write_rust_project( @@ -37,6 +45,7 @@ fn main() -> anyhow::Result<()> { &output_base, &workspace, &execution_root, + config_group.as_deref(), rules_rust_name, &targets, &workspace.join("rust-project.json"), diff --git a/tools/rust_analyzer/config.rs b/tools/rust_analyzer/config.rs index c695643816..1572672e29 100644 --- a/tools/rust_analyzer/config.rs +++ b/tools/rust_analyzer/config.rs @@ -1,7 +1,7 @@ use std::process::Command; use anyhow::bail; -use camino::Utf8PathBuf; +use camino::{Utf8Path, Utf8PathBuf}; use clap::{Args, Parser}; #[derive(Debug)] @@ -21,6 +21,9 @@ where /// The path to a Bazel binary pub bazel: Utf8PathBuf, + /// A `--config` directive that gets passed to Bazel to be able to pass custom configurations. + pub config_group: Option, + /// Binary specific config options pub specific: T, } @@ -36,6 +39,7 @@ where mut execution_root, mut output_base, bazel, + config_group, specific, } = ConfigParser::parse(); @@ -45,6 +49,7 @@ where execution_root: execution_root.unwrap(), output_base: output_base.unwrap(), bazel, + config_group, specific, }); } @@ -52,24 +57,19 @@ where // We need some info from `bazel info`. Fetch it now. let mut bazel_info_command = Command::new(&bazel); - // Switch to the workspace directory if one was provided. - if let Some(workspace) = &workspace { - bazel_info_command.current_dir(workspace); - } - - // Set the output_base if one was provided. - if let Some(output_base) = &output_base { - bazel_info_command.arg(format!("--output_base={output_base}")); - } - - bazel_info_command + // Execute bazel info. + let output = bazel_info_command + // Switch to the workspace directory if one was provided. + .current_dir(workspace.as_deref().unwrap_or(Utf8Path::new("."))) .env_remove("BAZELISK_SKIP_WRAPPER") .env_remove("BUILD_WORKING_DIRECTORY") .env_remove("BUILD_WORKSPACE_DIRECTORY") - .arg("info"); + // Set the output_base if one was provided. + .args(output_base.as_ref().map(|s| format!("--output_base={s}"))) + .arg("info") + .args(config_group.as_ref().map(|s| format!("--config={s}"))) + .output()?; - // Execute bazel info. - let output = bazel_info_command.output()?; if !output.status.success() { let status = output.status; let stderr = String::from_utf8_lossy(&output.stderr); @@ -99,6 +99,7 @@ where execution_root: execution_root.expect("'execution_root' must exist in bazel info"), output_base: output_base.expect("'output_base' must exist in bazel info"), bazel, + config_group, specific, }; @@ -127,6 +128,10 @@ where #[clap(long, default_value = "bazel")] bazel: Utf8PathBuf, + /// A `--config` directive that gets passed to Bazel to be able to pass custom configurations. + #[clap(long)] + config_group: Option, + /// Binary specific config options #[command(flatten)] specific: T, diff --git a/tools/rust_analyzer/lib.rs b/tools/rust_analyzer/lib.rs index 9d734d93f6..ae793b2d6d 100644 --- a/tools/rust_analyzer/lib.rs +++ b/tools/rust_analyzer/lib.rs @@ -17,6 +17,7 @@ pub fn generate_crate_info( bazel: &Utf8Path, output_base: &Utf8Path, workspace: &Utf8Path, + config_group: Option<&str>, rules_rust: &str, targets: &[String], ) -> anyhow::Result<()> { @@ -30,6 +31,7 @@ pub fn generate_crate_info( .env_remove("BUILD_WORKSPACE_DIRECTORY") .arg(format!("--output_base={output_base}")) .arg("build") + .args(config_group.map(|s| format!("--config={s}"))) .arg("--norun_validations") // This just makes the `rust-analyzer` integration more resilient, // in particular when being used to auto-discover workspaces. @@ -64,6 +66,7 @@ pub fn discover_rust_project( output_base: &Utf8Path, workspace: &Utf8Path, execution_root: &Utf8Path, + config_group: Option<&str>, rules_rust_name: &str, targets: &[String], buildfile: Utf8PathBuf, @@ -73,6 +76,7 @@ pub fn discover_rust_project( output_base, workspace, execution_root, + config_group, rules_rust_name, targets, )?; @@ -109,6 +113,7 @@ pub fn write_rust_project( output_base: &Utf8Path, workspace: &Utf8Path, execution_root: &Utf8Path, + config_group: Option<&str>, rules_rust_name: &str, targets: &[String], rust_project_path: &Utf8Path, @@ -118,6 +123,7 @@ pub fn write_rust_project( output_base, workspace, execution_root, + config_group, rules_rust_name, targets, )?; @@ -138,6 +144,7 @@ fn generate_rust_project( output_base: &Utf8Path, workspace: &Utf8Path, execution_root: &Utf8Path, + config_group: Option<&str>, rules_rust_name: &str, targets: &[String], ) -> anyhow::Result { @@ -146,6 +153,7 @@ fn generate_rust_project( output_base, workspace, execution_root, + config_group, targets, rules_rust_name, )?; diff --git a/tools/rust_analyzer/ra_arg.rs b/tools/rust_analyzer/ra_arg.rs index aba068c557..2466cdfb4b 100644 --- a/tools/rust_analyzer/ra_arg.rs +++ b/tools/rust_analyzer/ra_arg.rs @@ -21,15 +21,23 @@ impl RustAnalyzerArg { bazel: &Utf8Path, output_base: &Utf8Path, workspace: &Utf8Path, + config_group: Option<&str>, ) -> anyhow::Result<(Utf8PathBuf, Vec)> { match self { Self::Path(file) => { - let buildfile = - query_buildfile_for_source_file(bazel, output_base, workspace, &file)?; - query_targets(bazel, output_base, workspace, &buildfile).map(|t| (buildfile, t)) + let buildfile = query_buildfile_for_source_file( + bazel, + output_base, + workspace, + config_group, + &file, + )?; + query_targets(bazel, output_base, workspace, config_group, &buildfile) + .map(|t| (buildfile, t)) } Self::Buildfile(buildfile) => { - query_targets(bazel, output_base, workspace, &buildfile).map(|t| (buildfile, t)) + query_targets(bazel, output_base, workspace, config_group, &buildfile) + .map(|t| (buildfile, t)) } } } @@ -56,6 +64,7 @@ fn query_buildfile_for_source_file( bazel: &Utf8Path, output_base: &Utf8Path, workspace: &Utf8Path, + config_group: Option<&str>, file: &Utf8Path, ) -> anyhow::Result { log::info!("running bazel query on source file: {file}"); @@ -71,6 +80,7 @@ fn query_buildfile_for_source_file( .env_remove("BUILD_WORKSPACE_DIRECTORY") .arg(format!("--output_base={output_base}")) .arg("query") + .args(config_group.map(|s| format!("--config={s}"))) .arg("--output=package") .arg(stripped_file) .output() @@ -108,6 +118,7 @@ fn query_targets( bazel: &Utf8Path, output_base: &Utf8Path, workspace: &Utf8Path, + config_group: Option<&str>, buildfile: &Utf8Path, ) -> anyhow::Result> { log::info!("running bazel query on buildfile: {buildfile}"); @@ -129,6 +140,7 @@ fn query_targets( .env_remove("BUILD_WORKSPACE_DIRECTORY") .arg(format!("--output_base={output_base}")) .arg("query") + .args(config_group.map(|s| format!("--config={s}"))) .arg(format!( "kind(\"rust_(library|binary|proc_macro|test)\", {targets})" ))