Skip to content

Commit

Permalink
allow passing in a config group
Browse files Browse the repository at this point in the history
  • Loading branch information
bobozaur committed Dec 10, 2024
1 parent 070a409 commit d8ef7e4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
2 changes: 2 additions & 0 deletions tools/rust_analyzer/aquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BTreeSet<CrateSpec>> {
Expand All @@ -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,
Expand Down
14 changes: 12 additions & 2 deletions tools/rust_analyzer/bin/discover_rust_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn project_discovery() -> anyhow::Result<()> {
execution_root,
output_base,
bazel,
config_group,
specific,
} = Config::parse_and_refine()?;

Expand All @@ -45,20 +46,29 @@ 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(
&bazel,
&output_base,
&workspace,
&execution_root,
config_group.as_deref(),
rules_rust_name,
&targets,
buildfile,
Expand Down
11 changes: 10 additions & 1 deletion tools/rust_analyzer/bin/gen_rust_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn main() -> anyhow::Result<()> {
execution_root,
output_base,
bazel,
config_group,
specific,
} = Config::parse_and_refine()?;

Expand All @@ -29,14 +30,22 @@ 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(
&bazel,
&output_base,
&workspace,
&execution_root,
config_group.as_deref(),
rules_rust_name,
&targets,
&workspace.join("rust-project.json"),
Expand Down
35 changes: 20 additions & 15 deletions tools/rust_analyzer/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::process::Command;

use anyhow::bail;
use camino::Utf8PathBuf;
use camino::{Utf8Path, Utf8PathBuf};
use clap::{Args, Parser};

#[derive(Debug)]
Expand All @@ -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<String>,

/// Binary specific config options
pub specific: T,
}
Expand All @@ -36,6 +39,7 @@ where
mut execution_root,
mut output_base,
bazel,
config_group,
specific,
} = ConfigParser::parse();

Expand All @@ -45,31 +49,27 @@ where
execution_root: execution_root.unwrap(),
output_base: output_base.unwrap(),
bazel,
config_group,
specific,
});
}

// 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);
Expand Down Expand Up @@ -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,
};

Expand Down Expand Up @@ -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<String>,

/// Binary specific config options
#[command(flatten)]
specific: T,
Expand Down
8 changes: 8 additions & 0 deletions tools/rust_analyzer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -73,6 +76,7 @@ pub fn discover_rust_project(
output_base,
workspace,
execution_root,
config_group,
rules_rust_name,
targets,
)?;
Expand Down Expand Up @@ -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,
Expand All @@ -118,6 +123,7 @@ pub fn write_rust_project(
output_base,
workspace,
execution_root,
config_group,
rules_rust_name,
targets,
)?;
Expand All @@ -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<RustProject> {
Expand All @@ -146,6 +153,7 @@ fn generate_rust_project(
output_base,
workspace,
execution_root,
config_group,
targets,
rules_rust_name,
)?;
Expand Down
20 changes: 16 additions & 4 deletions tools/rust_analyzer/ra_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ impl RustAnalyzerArg {
bazel: &Utf8Path,
output_base: &Utf8Path,
workspace: &Utf8Path,
config_group: Option<&str>,
) -> anyhow::Result<(Utf8PathBuf, Vec<String>)> {
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))
}
}
}
Expand All @@ -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<Utf8PathBuf> {
log::info!("running bazel query on source file: {file}");
Expand All @@ -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()
Expand Down Expand Up @@ -108,6 +118,7 @@ fn query_targets(
bazel: &Utf8Path,
output_base: &Utf8Path,
workspace: &Utf8Path,
config_group: Option<&str>,
buildfile: &Utf8Path,
) -> anyhow::Result<Vec<String>> {
log::info!("running bazel query on buildfile: {buildfile}");
Expand All @@ -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})"
))
Expand Down

0 comments on commit d8ef7e4

Please sign in to comment.