Skip to content

Commit

Permalink
replace bazelrc with additive config args
Browse files Browse the repository at this point in the history
  • Loading branch information
bobozaur committed Jan 31, 2025
1 parent ce8b9f7 commit 4261a1b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 44 deletions.
9 changes: 6 additions & 3 deletions tools/rust_analyzer/aquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{BTreeMap, BTreeSet};
use camino::{Utf8Path, Utf8PathBuf};
use serde::Deserialize;

use crate::{deserialize_file_content, new_bazel_command};
use crate::{deserialize_file_content, bazel_command};

#[derive(Debug, Deserialize)]
struct AqueryOutput {
Expand Down Expand Up @@ -85,16 +85,19 @@ pub fn get_crate_specs(
output_base: &Utf8Path,
workspace: &Utf8Path,
execution_root: &Utf8Path,
bazelrc: Option<&Utf8Path>,
bazel_startup_options: &[String],
bazel_args: &[String],
targets: &[String],
rules_rust_name: &str,
) -> anyhow::Result<BTreeSet<CrateSpec>> {
log::info!("running bazel aquery...");
log::debug!("Get crate specs with targets: {:?}", targets);
let target_pattern = format!("deps({})", targets.join("+"));

let output = new_bazel_command(bazel, Some(workspace), Some(output_base), bazelrc)
let output = bazel_command(bazel, Some(workspace), Some(output_base))
.args(bazel_startup_options)
.arg("aquery")
.args(bazel_args)
.arg("--include_aspects")
.arg("--include_artifacts")
.arg(format!(
Expand Down
60 changes: 42 additions & 18 deletions tools/rust_analyzer/bin/discover_rust_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use camino::{Utf8Path, Utf8PathBuf};
use clap::Parser;
use env_logger::{fmt::Formatter, Target, WriteStyle};
use gen_rust_project_lib::{
generate_rust_project, get_bazel_info, DiscoverProject, RustAnalyzerArg, BUILD_FILE_NAMES,
bazel_info, generate_rust_project, DiscoverProject, RustAnalyzerArg, BUILD_FILE_NAMES,
WORKSPACE_ROOT_FILE_NAMES,
};
use log::{LevelFilter, Record};
Expand All @@ -37,7 +37,8 @@ fn project_discovery() -> anyhow::Result<DiscoverProject<'static>> {
execution_root,
output_base,
bazel,
bazelrc,
bazel_startup_options,
bazel_args,
rust_analyzer_argument,
} = Config::parse()?;

Expand All @@ -53,20 +54,20 @@ fn project_discovery() -> anyhow::Result<DiscoverProject<'static>> {
log::info!("resolved rust-analyzer argument: {ra_arg:?}");

let (buildfile, targets) = ra_arg.into_target_details(&workspace)?;
let targets = &[targets];

log::debug!("got buildfile: {buildfile}");
log::debug!("got targets: {targets:?}");
log::debug!("got targets: {targets}");

// Use the generated files to print the rust-project.json.
let project = generate_rust_project(
&bazel,
&output_base,
&workspace,
&execution_root,
bazelrc.as_deref(),
&bazel_startup_options,
&bazel_args,
&rules_rust_name,
targets,
&[targets],
)?;

Ok(DiscoverProject::Finished { buildfile, project })
Expand Down Expand Up @@ -114,19 +115,26 @@ fn main() -> anyhow::Result<()> {
#[derive(Debug)]
pub struct Config {
/// The path to the Bazel workspace directory. If not specified, uses the result of `bazel info workspace`.
pub workspace: Utf8PathBuf,
workspace: Utf8PathBuf,

/// The path to the Bazel execution root. If not specified, uses the result of `bazel info execution_root`.
pub execution_root: Utf8PathBuf,
execution_root: Utf8PathBuf,

/// The path to the Bazel output user root. If not specified, uses the result of `bazel info output_base`.
pub output_base: Utf8PathBuf,
output_base: Utf8PathBuf,

/// The path to a Bazel binary.
pub bazel: Utf8PathBuf,
bazel: Utf8PathBuf,

/// Startup options to pass to `bazel` invocations.
/// See the [Command-Line Reference](<https://bazel.build/reference/command-line-reference>)
/// for more details.
bazel_startup_options: Vec<String>,

/// The path to a `bazelrc` configuration file.
bazelrc: Option<Utf8PathBuf>,
/// Arguments to pass to `bazel` invocations.
/// See the [Command-Line Reference](<https://bazel.build/reference/command-line-reference>)
/// for more details.
bazel_args: Vec<String>,

/// The argument that `rust-analyzer` can pass to the binary.
rust_analyzer_argument: Option<RustAnalyzerArg>,
Expand All @@ -138,12 +146,19 @@ impl Config {
let ConfigParser {
workspace,
bazel,
bazelrc,
bazel_startup_options,
bazel_args,
rust_analyzer_argument,
} = ConfigParser::parse();

// We need some info from `bazel info`. Fetch it now.
let mut info_map = get_bazel_info(&bazel, workspace.as_deref(), None, bazelrc.as_deref())?;
let mut info_map = bazel_info(
&bazel,
workspace.as_deref(),
None,
&bazel_startup_options,
&bazel_args,
)?;

let config = Config {
workspace: info_map
Expand All @@ -159,7 +174,8 @@ impl Config {
.expect("'output_base' must exist in bazel info")
.into(),
bazel,
bazelrc,
bazel_startup_options,
bazel_args,
rust_analyzer_argument,
};

Expand All @@ -177,9 +193,17 @@ struct ConfigParser {
#[clap(long, default_value = "bazel")]
bazel: Utf8PathBuf,

/// The path to a `bazelrc` configuration file.
#[clap(long)]
bazelrc: Option<Utf8PathBuf>,
/// Startup options to pass to `bazel` invocations.
/// See the [Command-Line Reference](<https://bazel.build/reference/command-line-reference>)
/// for more details.
#[clap(long = "bazel_startup_option")]
bazel_startup_options: Vec<String>,

/// Arguments to pass to `bazel` invocations.
/// See the [Command-Line Reference](<https://bazel.build/reference/command-line-reference>)
/// for more details.
#[clap(long = "bazel_arg")]
bazel_args: Vec<String>,

/// The argument that `rust-analyzer` can pass to the binary.
rust_analyzer_argument: Option<RustAnalyzerArg>,
Expand Down
15 changes: 8 additions & 7 deletions tools/rust_analyzer/bin/gen_rust_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
use anyhow::{bail, Context};
use camino::{Utf8Path, Utf8PathBuf};
use clap::Parser;
use gen_rust_project_lib::{generate_rust_project, get_bazel_info};
use gen_rust_project_lib::{generate_rust_project, bazel_info};

fn write_rust_project(
bazel: &Utf8Path,
Expand All @@ -23,7 +23,8 @@ fn write_rust_project(
output_base,
workspace,
execution_root,
None,
&[],
&[],
rules_rust_name,
targets,
)?;
Expand Down Expand Up @@ -80,16 +81,16 @@ fn main() -> anyhow::Result<()> {
#[derive(Debug)]
pub struct Config {
/// The path to the Bazel workspace directory. If not specified, uses the result of `bazel info workspace`.
pub workspace: Utf8PathBuf,
workspace: Utf8PathBuf,

/// The path to the Bazel execution root. If not specified, uses the result of `bazel info execution_root`.
pub execution_root: Utf8PathBuf,
execution_root: Utf8PathBuf,

/// The path to the Bazel output user root. If not specified, uses the result of `bazel info output_base`.
pub output_base: Utf8PathBuf,
output_base: Utf8PathBuf,

/// The path to a Bazel binary.
pub bazel: Utf8PathBuf,
bazel: Utf8PathBuf,

/// Space separated list of target patterns that comes after all other args.
targets: Vec<String>,
Expand Down Expand Up @@ -122,7 +123,7 @@ impl Config {

// We need some info from `bazel info`. Fetch it now.
let mut info_map =
get_bazel_info(&bazel, workspace.as_deref(), output_base.as_deref(), None)?;
bazel_info(&bazel, workspace.as_deref(), output_base.as_deref(), &[], &[])?;

let config = Config {
workspace: info_map
Expand Down
38 changes: 22 additions & 16 deletions tools/rust_analyzer/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod aquery;
mod rust_project;

use std::{collections::HashMap, convert::TryInto, fs, process::Command};
use std::{collections::BTreeMap, convert::TryInto, fs, process::Command};

use anyhow::{bail, Context};
use camino::{Utf8Path, Utf8PathBuf};
Expand All @@ -20,15 +20,17 @@ pub fn generate_rust_project(
output_base: &Utf8Path,
workspace: &Utf8Path,
execution_root: &Utf8Path,
bazelrc: Option<&Utf8Path>,
bazel_startup_options: &[String],
bazel_args: &[String],
rules_rust_name: &str,
targets: &[String],
) -> anyhow::Result<RustProject> {
generate_crate_info(
bazel,
output_base,
workspace,
bazelrc,
bazel_startup_options,
bazel_args,
rules_rust_name,
targets,
)?;
Expand All @@ -38,7 +40,8 @@ pub fn generate_rust_project(
output_base,
workspace,
execution_root,
bazelrc,
bazel_startup_options,
bazel_args,
targets,
rules_rust_name,
)?;
Expand All @@ -55,15 +58,18 @@ pub fn generate_rust_project(
rust_project::assemble_rust_project(bazel, workspace, toolchain_info, &crate_specs)
}

/// Executes `bazel info` to get context information.
pub fn get_bazel_info(
/// Executes `bazel info` to get a map of context information.
pub fn bazel_info(
bazel: &Utf8Path,
workspace: Option<&Utf8Path>,
output_base: Option<&Utf8Path>,
bazelrc: Option<&Utf8Path>,
) -> anyhow::Result<HashMap<String, String>> {
let output = new_bazel_command(bazel, workspace, output_base, bazelrc)
bazel_startup_options: &[String],
bazel_args: &[String],
) -> anyhow::Result<BTreeMap<String, String>> {
let output = bazel_command(bazel, workspace, output_base)
.args(bazel_startup_options)
.arg("info")
.args(bazel_args)
.output()?;

if !output.status.success() {
Expand All @@ -87,15 +93,18 @@ fn generate_crate_info(
bazel: &Utf8Path,
output_base: &Utf8Path,
workspace: &Utf8Path,
bazelrc: Option<&Utf8Path>,
bazel_startup_options: &[String],
bazel_args: &[String],
rules_rust: &str,
targets: &[String],
) -> anyhow::Result<()> {
log::info!("running bazel build...");
log::debug!("Building rust_analyzer_crate_spec files for {:?}", targets);

let output = new_bazel_command(bazel, Some(workspace), Some(output_base), bazelrc)
let output = bazel_command(bazel, Some(workspace), Some(output_base))
.args(bazel_startup_options)
.arg("build")
.args(bazel_args)
.arg("--norun_validations")
.arg(format!(
"--aspects={rules_rust}//rust:defs.bzl%rust_analyzer_aspect"
Expand All @@ -115,11 +124,10 @@ fn generate_crate_info(
Ok(())
}

fn new_bazel_command(
fn bazel_command(
bazel: &Utf8Path,
workspace: Option<&Utf8Path>,
output_base: Option<&Utf8Path>,
bazelrc: Option<&Utf8Path>,
) -> Command {
let mut cmd = Command::new(bazel);

Expand All @@ -130,9 +138,7 @@ fn new_bazel_command(
.env_remove("BUILD_WORKING_DIRECTORY")
.env_remove("BUILD_WORKSPACE_DIRECTORY")
// Set the output_base if one was provided.
.args(output_base.map(|s| format!("--output_base={s}")))
// Pass the bazelrc file if any is provided.
.args(bazelrc.map(|s| format!("--bazelrc={s}")));
.args(output_base.map(|s| format!("--output_base={s}")));

cmd
}
Expand Down

0 comments on commit 4261a1b

Please sign in to comment.