Skip to content

Commit 70f91ac

Browse files
authored
rust_analyzer: Support passing a --config option to Bazel (#3254)
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 ac13d65 commit 70f91ac

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
@@ -62,13 +62,18 @@ pub struct CrateSpecSource {
6262

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

7378
let mut aquery_command = Command::new(bazel);
7479
aquery_command
@@ -77,6 +82,7 @@ pub fn get_crate_specs(
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("--remote_download_all")
2733
.arg(format!(
@@ -43,8 +49,10 @@ pub fn generate_crate_info(
4349
Ok(())
4450
}
4551

52+
#[allow(clippy::too_many_arguments)]
4653
pub fn write_rust_project(
4754
bazel: impl AsRef<Path>,
55+
config: &Option<String>,
4856
workspace: impl AsRef<Path>,
4957
rules_rust_name: &impl AsRef<str>,
5058
targets: &[String],
@@ -54,6 +62,7 @@ pub fn write_rust_project(
5462
) -> anyhow::Result<()> {
5563
let crate_specs = aquery::get_crate_specs(
5664
bazel.as_ref(),
65+
config,
5766
workspace.as_ref(),
5867
execution_root.as_ref(),
5968
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)