• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1From 24ad91945214b26ac49be84a80736530641f1ff1 Mon Sep 17 00:00:00 2001
2From: Erik Gilling <konkers@google.com>
3Date: Mon, 10 Feb 2025 16:49:57 -0800
4Subject: [PATCH] rust_analyzer: Support passing a --config option to Bazel
5
6This is useful if you want to analyze code using a different configuration
7than the default.  An example of this might be under a different platform.
8---
9 tools/rust_analyzer/aquery.rs | 6 ++++++
10 tools/rust_analyzer/lib.rs    | 9 +++++++++
11 tools/rust_analyzer/main.rs   | 6 ++++++
12 3 files changed, 21 insertions(+)
13
14diff --git a/tools/rust_analyzer/aquery.rs b/tools/rust_analyzer/aquery.rs
15index bc98913b..74c7d97e 100644
16--- a/tools/rust_analyzer/aquery.rs
17+++ b/tools/rust_analyzer/aquery.rs
18@@ -63,6 +63,7 @@ pub struct CrateSpecSource {
19
20 pub fn get_crate_specs(
21     bazel: &Path,
22+    config: &Option<String>,
23     workspace: &Path,
24     execution_root: &Path,
25     targets: &[String],
26@@ -70,6 +71,10 @@ pub fn get_crate_specs(
27 ) -> anyhow::Result<BTreeSet<CrateSpec>> {
28     log::debug!("Get crate specs with targets: {:?}", targets);
29     let target_pattern = format!("deps({})", targets.join("+"));
30+    let config_args = match config {
31+        Some(config) => vec!["--config", config],
32+        None => Vec::new(),
33+    };
34
35     let aquery_output = Command::new(bazel)
36         .current_dir(workspace)
37@@ -77,6 +82,7 @@ pub fn get_crate_specs(
38         .env_remove("BUILD_WORKING_DIRECTORY")
39         .env_remove("BUILD_WORKSPACE_DIRECTORY")
40         .arg("aquery")
41+        .args(config_args)
42         .arg("--include_aspects")
43         .arg("--include_artifacts")
44         .arg(format!(
45diff --git a/tools/rust_analyzer/lib.rs b/tools/rust_analyzer/lib.rs
46index 7a6eb28d..43b2c190 100644
47--- a/tools/rust_analyzer/lib.rs
48+++ b/tools/rust_analyzer/lib.rs
49@@ -10,11 +10,16 @@ mod rust_project;
50
51 pub fn generate_crate_info(
52     bazel: impl AsRef<Path>,
53+    config: &Option<String>,
54     workspace: impl AsRef<Path>,
55     rules_rust: impl AsRef<str>,
56     targets: &[String],
57 ) -> anyhow::Result<()> {
58     log::debug!("Building rust_analyzer_crate_spec files for {:?}", targets);
59+    let config_args = match config {
60+        Some(config) => vec!["--config", config],
61+        None => Vec::new(),
62+    };
63
64     let output = Command::new(bazel.as_ref())
65         .current_dir(workspace.as_ref())
66@@ -22,6 +27,7 @@ pub fn generate_crate_info(
67         .env_remove("BUILD_WORKING_DIRECTORY")
68         .env_remove("BUILD_WORKSPACE_DIRECTORY")
69         .arg("build")
70+        .args(config_args)
71         .arg("--norun_validations")
72         .arg(format!(
73             "--aspects={}//rust:defs.bzl%rust_analyzer_aspect",
74@@ -42,8 +48,10 @@ pub fn generate_crate_info(
75     Ok(())
76 }
77
78+#[allow(clippy::too_many_arguments)]
79 pub fn write_rust_project(
80     bazel: impl AsRef<Path>,
81+    config: &Option<String>,
82     workspace: impl AsRef<Path>,
83     rules_rust_name: &impl AsRef<str>,
84     targets: &[String],
85@@ -53,6 +61,7 @@ pub fn write_rust_project(
86 ) -> anyhow::Result<()> {
87     let crate_specs = aquery::get_crate_specs(
88         bazel.as_ref(),
89+        config,
90         workspace.as_ref(),
91         execution_root.as_ref(),
92         targets,
93diff --git a/tools/rust_analyzer/main.rs b/tools/rust_analyzer/main.rs
94index df4b2f9b..f1dc0143 100644
95--- a/tools/rust_analyzer/main.rs
96+++ b/tools/rust_analyzer/main.rs
97@@ -36,6 +36,7 @@ fn main() -> anyhow::Result<()> {
98     // Generate the crate specs.
99     generate_crate_info(
100         &config.bazel,
101+        &config.config,
102         workspace_root,
103         rules_rust_name,
104         &config.targets,
105@@ -44,6 +45,7 @@ fn main() -> anyhow::Result<()> {
106     // Use the generated files to write rust-project.json.
107     write_rust_project(
108         &config.bazel,
109+        &config.config,
110         workspace_root,
111         &rules_rust_name,
112         &config.targets,
113@@ -120,6 +122,10 @@ struct Config {
114     #[clap(long, env = "OUTPUT_BASE")]
115     output_base: Option<PathBuf>,
116
117+    /// A config to pass to Bazel invocations with `--config=<config>`.
118+    #[clap(long)]
119+    config: Option<String>,
120+
121     /// The path to a Bazel binary
122     #[clap(long, default_value = "bazel")]
123     bazel: PathBuf,
124--
1252.48.1.502.g6dc24dfdaf-goog
126
127