Skip to content

Commit 4261a1b

Browse files
committed
replace bazelrc with additive config args
1 parent ce8b9f7 commit 4261a1b

4 files changed

Lines changed: 78 additions & 44 deletions

File tree

tools/rust_analyzer/aquery.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::{BTreeMap, BTreeSet};
33
use camino::{Utf8Path, Utf8PathBuf};
44
use serde::Deserialize;
55

6-
use crate::{deserialize_file_content, new_bazel_command};
6+
use crate::{deserialize_file_content, bazel_command};
77

88
#[derive(Debug, Deserialize)]
99
struct AqueryOutput {
@@ -85,16 +85,19 @@ pub fn get_crate_specs(
8585
output_base: &Utf8Path,
8686
workspace: &Utf8Path,
8787
execution_root: &Utf8Path,
88-
bazelrc: Option<&Utf8Path>,
88+
bazel_startup_options: &[String],
89+
bazel_args: &[String],
8990
targets: &[String],
9091
rules_rust_name: &str,
9192
) -> anyhow::Result<BTreeSet<CrateSpec>> {
9293
log::info!("running bazel aquery...");
9394
log::debug!("Get crate specs with targets: {:?}", targets);
9495
let target_pattern = format!("deps({})", targets.join("+"));
9596

96-
let output = new_bazel_command(bazel, Some(workspace), Some(output_base), bazelrc)
97+
let output = bazel_command(bazel, Some(workspace), Some(output_base))
98+
.args(bazel_startup_options)
9799
.arg("aquery")
100+
.args(bazel_args)
98101
.arg("--include_aspects")
99102
.arg("--include_artifacts")
100103
.arg(format!(

tools/rust_analyzer/bin/discover_rust_project.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use camino::{Utf8Path, Utf8PathBuf};
1212
use clap::Parser;
1313
use env_logger::{fmt::Formatter, Target, WriteStyle};
1414
use gen_rust_project_lib::{
15-
generate_rust_project, get_bazel_info, DiscoverProject, RustAnalyzerArg, BUILD_FILE_NAMES,
15+
bazel_info, generate_rust_project, DiscoverProject, RustAnalyzerArg, BUILD_FILE_NAMES,
1616
WORKSPACE_ROOT_FILE_NAMES,
1717
};
1818
use log::{LevelFilter, Record};
@@ -37,7 +37,8 @@ fn project_discovery() -> anyhow::Result<DiscoverProject<'static>> {
3737
execution_root,
3838
output_base,
3939
bazel,
40-
bazelrc,
40+
bazel_startup_options,
41+
bazel_args,
4142
rust_analyzer_argument,
4243
} = Config::parse()?;
4344

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

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

5858
log::debug!("got buildfile: {buildfile}");
59-
log::debug!("got targets: {targets:?}");
59+
log::debug!("got targets: {targets}");
6060

6161
// Use the generated files to print the rust-project.json.
6262
let project = generate_rust_project(
6363
&bazel,
6464
&output_base,
6565
&workspace,
6666
&execution_root,
67-
bazelrc.as_deref(),
67+
&bazel_startup_options,
68+
&bazel_args,
6869
&rules_rust_name,
69-
targets,
70+
&[targets],
7071
)?;
7172

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

119120
/// The path to the Bazel execution root. If not specified, uses the result of `bazel info execution_root`.
120-
pub execution_root: Utf8PathBuf,
121+
execution_root: Utf8PathBuf,
121122

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

125126
/// The path to a Bazel binary.
126-
pub bazel: Utf8PathBuf,
127+
bazel: Utf8PathBuf,
128+
129+
/// Startup options to pass to `bazel` invocations.
130+
/// See the [Command-Line Reference](<https://bazel.build/reference/command-line-reference>)
131+
/// for more details.
132+
bazel_startup_options: Vec<String>,
127133

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

131139
/// The argument that `rust-analyzer` can pass to the binary.
132140
rust_analyzer_argument: Option<RustAnalyzerArg>,
@@ -138,12 +146,19 @@ impl Config {
138146
let ConfigParser {
139147
workspace,
140148
bazel,
141-
bazelrc,
149+
bazel_startup_options,
150+
bazel_args,
142151
rust_analyzer_argument,
143152
} = ConfigParser::parse();
144153

145154
// We need some info from `bazel info`. Fetch it now.
146-
let mut info_map = get_bazel_info(&bazel, workspace.as_deref(), None, bazelrc.as_deref())?;
155+
let mut info_map = bazel_info(
156+
&bazel,
157+
workspace.as_deref(),
158+
None,
159+
&bazel_startup_options,
160+
&bazel_args,
161+
)?;
147162

148163
let config = Config {
149164
workspace: info_map
@@ -159,7 +174,8 @@ impl Config {
159174
.expect("'output_base' must exist in bazel info")
160175
.into(),
161176
bazel,
162-
bazelrc,
177+
bazel_startup_options,
178+
bazel_args,
163179
rust_analyzer_argument,
164180
};
165181

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

180-
/// The path to a `bazelrc` configuration file.
181-
#[clap(long)]
182-
bazelrc: Option<Utf8PathBuf>,
196+
/// Startup options to pass to `bazel` invocations.
197+
/// See the [Command-Line Reference](<https://bazel.build/reference/command-line-reference>)
198+
/// for more details.
199+
#[clap(long = "bazel_startup_option")]
200+
bazel_startup_options: Vec<String>,
201+
202+
/// Arguments to pass to `bazel` invocations.
203+
/// See the [Command-Line Reference](<https://bazel.build/reference/command-line-reference>)
204+
/// for more details.
205+
#[clap(long = "bazel_arg")]
206+
bazel_args: Vec<String>,
183207

184208
/// The argument that `rust-analyzer` can pass to the binary.
185209
rust_analyzer_argument: Option<RustAnalyzerArg>,

tools/rust_analyzer/bin/gen_rust_project.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77
use anyhow::{bail, Context};
88
use camino::{Utf8Path, Utf8PathBuf};
99
use clap::Parser;
10-
use gen_rust_project_lib::{generate_rust_project, get_bazel_info};
10+
use gen_rust_project_lib::{generate_rust_project, bazel_info};
1111

1212
fn write_rust_project(
1313
bazel: &Utf8Path,
@@ -23,7 +23,8 @@ fn write_rust_project(
2323
output_base,
2424
workspace,
2525
execution_root,
26-
None,
26+
&[],
27+
&[],
2728
rules_rust_name,
2829
targets,
2930
)?;
@@ -80,16 +81,16 @@ fn main() -> anyhow::Result<()> {
8081
#[derive(Debug)]
8182
pub struct Config {
8283
/// The path to the Bazel workspace directory. If not specified, uses the result of `bazel info workspace`.
83-
pub workspace: Utf8PathBuf,
84+
workspace: Utf8PathBuf,
8485

8586
/// The path to the Bazel execution root. If not specified, uses the result of `bazel info execution_root`.
86-
pub execution_root: Utf8PathBuf,
87+
execution_root: Utf8PathBuf,
8788

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

9192
/// The path to a Bazel binary.
92-
pub bazel: Utf8PathBuf,
93+
bazel: Utf8PathBuf,
9394

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

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

127128
let config = Config {
128129
workspace: info_map

tools/rust_analyzer/lib.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod aquery;
22
mod rust_project;
33

4-
use std::{collections::HashMap, convert::TryInto, fs, process::Command};
4+
use std::{collections::BTreeMap, convert::TryInto, fs, process::Command};
55

66
use anyhow::{bail, Context};
77
use camino::{Utf8Path, Utf8PathBuf};
@@ -20,15 +20,17 @@ pub fn generate_rust_project(
2020
output_base: &Utf8Path,
2121
workspace: &Utf8Path,
2222
execution_root: &Utf8Path,
23-
bazelrc: Option<&Utf8Path>,
23+
bazel_startup_options: &[String],
24+
bazel_args: &[String],
2425
rules_rust_name: &str,
2526
targets: &[String],
2627
) -> anyhow::Result<RustProject> {
2728
generate_crate_info(
2829
bazel,
2930
output_base,
3031
workspace,
31-
bazelrc,
32+
bazel_startup_options,
33+
bazel_args,
3234
rules_rust_name,
3335
targets,
3436
)?;
@@ -38,7 +40,8 @@ pub fn generate_rust_project(
3840
output_base,
3941
workspace,
4042
execution_root,
41-
bazelrc,
43+
bazel_startup_options,
44+
bazel_args,
4245
targets,
4346
rules_rust_name,
4447
)?;
@@ -55,15 +58,18 @@ pub fn generate_rust_project(
5558
rust_project::assemble_rust_project(bazel, workspace, toolchain_info, &crate_specs)
5659
}
5760

58-
/// Executes `bazel info` to get context information.
59-
pub fn get_bazel_info(
61+
/// Executes `bazel info` to get a map of context information.
62+
pub fn bazel_info(
6063
bazel: &Utf8Path,
6164
workspace: Option<&Utf8Path>,
6265
output_base: Option<&Utf8Path>,
63-
bazelrc: Option<&Utf8Path>,
64-
) -> anyhow::Result<HashMap<String, String>> {
65-
let output = new_bazel_command(bazel, workspace, output_base, bazelrc)
66+
bazel_startup_options: &[String],
67+
bazel_args: &[String],
68+
) -> anyhow::Result<BTreeMap<String, String>> {
69+
let output = bazel_command(bazel, workspace, output_base)
70+
.args(bazel_startup_options)
6671
.arg("info")
72+
.args(bazel_args)
6773
.output()?;
6874

6975
if !output.status.success() {
@@ -87,15 +93,18 @@ fn generate_crate_info(
8793
bazel: &Utf8Path,
8894
output_base: &Utf8Path,
8995
workspace: &Utf8Path,
90-
bazelrc: Option<&Utf8Path>,
96+
bazel_startup_options: &[String],
97+
bazel_args: &[String],
9198
rules_rust: &str,
9299
targets: &[String],
93100
) -> anyhow::Result<()> {
94101
log::info!("running bazel build...");
95102
log::debug!("Building rust_analyzer_crate_spec files for {:?}", targets);
96103

97-
let output = new_bazel_command(bazel, Some(workspace), Some(output_base), bazelrc)
104+
let output = bazel_command(bazel, Some(workspace), Some(output_base))
105+
.args(bazel_startup_options)
98106
.arg("build")
107+
.args(bazel_args)
99108
.arg("--norun_validations")
100109
.arg(format!(
101110
"--aspects={rules_rust}//rust:defs.bzl%rust_analyzer_aspect"
@@ -115,11 +124,10 @@ fn generate_crate_info(
115124
Ok(())
116125
}
117126

118-
fn new_bazel_command(
127+
fn bazel_command(
119128
bazel: &Utf8Path,
120129
workspace: Option<&Utf8Path>,
121130
output_base: Option<&Utf8Path>,
122-
bazelrc: Option<&Utf8Path>,
123131
) -> Command {
124132
let mut cmd = Command::new(bazel);
125133

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

137143
cmd
138144
}

0 commit comments

Comments
 (0)