Skip to content

Commit 24ad919

Browse files
committed
rust_analyzer: Support passing a --config option to Bazel
This is useful if you want to analyze code using a different configuration than the default. An example of this might be under a different platform.
1 parent 7a1f037 commit 24ad919

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

tools/rust_analyzer/aquery.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,26 @@ pub struct CrateSpecSource {
6363

6464
pub fn get_crate_specs(
6565
bazel: &Path,
66+
config: &Option<String>,
6667
workspace: &Path,
6768
execution_root: &Path,
6869
targets: &[String],
6970
rules_rust_name: &str,
7071
) -> anyhow::Result<BTreeSet<CrateSpec>> {
7172
log::debug!("Get crate specs with targets: {:?}", targets);
7273
let target_pattern = format!("deps({})", targets.join("+"));
74+
let config_args = match config {
75+
Some(config) => vec!["--config", config],
76+
None => Vec::new(),
77+
};
7378

7479
let aquery_output = Command::new(bazel)
7580
.current_dir(workspace)
7681
.env_remove("BAZELISK_SKIP_WRAPPER")
7782
.env_remove("BUILD_WORKING_DIRECTORY")
7883
.env_remove("BUILD_WORKSPACE_DIRECTORY")
7984
.arg("aquery")
85+
.args(config_args)
8086
.arg("--include_aspects")
8187
.arg("--include_artifacts")
8288
.arg(format!(

tools/rust_analyzer/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@ mod rust_project;
1010

1111
pub fn generate_crate_info(
1212
bazel: impl AsRef<Path>,
13+
config: &Option<String>,
1314
workspace: impl AsRef<Path>,
1415
rules_rust: impl AsRef<str>,
1516
targets: &[String],
1617
) -> anyhow::Result<()> {
1718
log::debug!("Building rust_analyzer_crate_spec files for {:?}", targets);
19+
let config_args = match config {
20+
Some(config) => vec!["--config", config],
21+
None => Vec::new(),
22+
};
1823

1924
let output = Command::new(bazel.as_ref())
2025
.current_dir(workspace.as_ref())
2126
.env_remove("BAZELISK_SKIP_WRAPPER")
2227
.env_remove("BUILD_WORKING_DIRECTORY")
2328
.env_remove("BUILD_WORKSPACE_DIRECTORY")
2429
.arg("build")
30+
.args(config_args)
2531
.arg("--norun_validations")
2632
.arg(format!(
2733
"--aspects={}//rust:defs.bzl%rust_analyzer_aspect",
@@ -42,8 +48,10 @@ pub fn generate_crate_info(
4248
Ok(())
4349
}
4450

51+
#[allow(clippy::too_many_arguments)]
4552
pub fn write_rust_project(
4653
bazel: impl AsRef<Path>,
54+
config: &Option<String>,
4755
workspace: impl AsRef<Path>,
4856
rules_rust_name: &impl AsRef<str>,
4957
targets: &[String],
@@ -53,6 +61,7 @@ pub fn write_rust_project(
5361
) -> anyhow::Result<()> {
5462
let crate_specs = aquery::get_crate_specs(
5563
bazel.as_ref(),
64+
config,
5665
workspace.as_ref(),
5766
execution_root.as_ref(),
5867
targets,

tools/rust_analyzer/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fn main() -> anyhow::Result<()> {
3636
// Generate the crate specs.
3737
generate_crate_info(
3838
&config.bazel,
39+
&config.config,
3940
workspace_root,
4041
rules_rust_name,
4142
&config.targets,
@@ -44,6 +45,7 @@ fn main() -> anyhow::Result<()> {
4445
// Use the generated files to write rust-project.json.
4546
write_rust_project(
4647
&config.bazel,
48+
&config.config,
4749
workspace_root,
4850
&rules_rust_name,
4951
&config.targets,
@@ -120,6 +122,10 @@ struct Config {
120122
#[clap(long, env = "OUTPUT_BASE")]
121123
output_base: Option<PathBuf>,
122124

125+
/// A config to pass to Bazel invocations with `--config=<config>`.
126+
#[clap(long)]
127+
config: Option<String>,
128+
123129
/// The path to a Bazel binary
124130
#[clap(long, default_value = "bazel")]
125131
bazel: PathBuf,

0 commit comments

Comments
 (0)