Skip to content

Commit 4d0b0dd

Browse files
committed
remove targets query
1 parent 80a45c1 commit 4d0b0dd

File tree

4 files changed

+9
-52
lines changed

4 files changed

+9
-52
lines changed

Diff for: tools/rust_analyzer/aquery.rs

-8
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,6 @@ pub fn get_crate_specs(
103103
.args(config_group.map(|s| format!("--config={s}")))
104104
.arg("--include_aspects")
105105
.arg("--include_artifacts")
106-
// This just makes the `rust-analyzer` integration more resilient,
107-
// in particular when being used to auto-discover workspaces.
108-
//
109-
// Identifying only compatible targets is tricky and, while
110-
// that would be ideal, skipping incompatible targets does not
111-
// seem like the worst thing since the purpose of `rust-analyzer`
112-
// is helpful IDE support, not 100% correct results.
113-
.arg("--skip_incompatible_explicit_targets")
114106
.arg(format!(
115107
"--aspects={rules_rust_name}//rust:defs.bzl%rust_analyzer_aspect"
116108
))

Diff for: tools/rust_analyzer/bin/discover_rust_project.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ fn project_discovery() -> anyhow::Result<()> {
4949
let (buildfile, targets) =
5050
ra_arg.query_target_details(&bazel, &output_base, &workspace, config_group.as_deref())?;
5151

52+
let targets = &[targets];
5253
log::debug!("got buildfile: {buildfile}");
5354
log::debug!("got targets: {targets:?}");
5455

@@ -59,7 +60,7 @@ fn project_discovery() -> anyhow::Result<()> {
5960
&workspace,
6061
config_group.as_deref(),
6162
rules_rust_name,
62-
&targets,
63+
targets,
6364
)?;
6465

6566
// Use the generated files to print the rust-project.json.
@@ -70,7 +71,7 @@ fn project_discovery() -> anyhow::Result<()> {
7071
&execution_root,
7172
config_group.as_deref(),
7273
rules_rust_name,
73-
&targets,
74+
targets,
7475
buildfile,
7576
)
7677
}

Diff for: tools/rust_analyzer/lib.rs

-8
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ pub fn generate_crate_info(
3333
.arg("build")
3434
.args(config_group.map(|s| format!("--config={s}")))
3535
.arg("--norun_validations")
36-
// This just makes the `rust-analyzer` integration more resilient,
37-
// in particular when being used to auto-discover workspaces.
38-
//
39-
// Identifying only compatible targets is tricky and, while
40-
// that would be ideal, skipping incompatible targets does not
41-
// seem like the worst thing since the purpose of `rust-analyzer`
42-
// is helpful IDE support, not 100% correct results.
43-
.arg("--skip_incompatible_explicit_targets")
4436
.arg(format!(
4537
"--aspects={rules_rust}//rust:defs.bzl%rust_analyzer_aspect"
4638
))

Diff for: tools/rust_analyzer/ra_arg.rs

+6-34
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl RustAnalyzerArg {
2222
output_base: &Utf8Path,
2323
workspace: &Utf8Path,
2424
config_group: Option<&str>,
25-
) -> anyhow::Result<(Utf8PathBuf, Vec<String>)> {
25+
) -> anyhow::Result<(Utf8PathBuf, String)> {
2626
match self {
2727
Self::Path(file) => {
2828
let buildfile = query_buildfile_for_source_file(
@@ -32,12 +32,10 @@ impl RustAnalyzerArg {
3232
config_group,
3333
&file,
3434
)?;
35-
query_targets(bazel, output_base, workspace, config_group, &buildfile)
36-
.map(|t| (buildfile, t))
35+
buildfile_to_targets(workspace, &buildfile).map(|t| (buildfile, t))
3736
}
3837
Self::Buildfile(buildfile) => {
39-
query_targets(bazel, output_base, workspace, config_group, &buildfile)
40-
.map(|t| (buildfile, t))
38+
buildfile_to_targets(workspace, &buildfile).map(|t| (buildfile, t))
4139
}
4240
}
4341
}
@@ -114,44 +112,18 @@ fn query_buildfile_for_source_file(
114112
bail!("no buildfile found for {file}");
115113
}
116114

117-
fn query_targets(
118-
bazel: &Utf8Path,
119-
output_base: &Utf8Path,
120-
workspace: &Utf8Path,
121-
config_group: Option<&str>,
122-
buildfile: &Utf8Path,
123-
) -> anyhow::Result<Vec<String>> {
124-
log::info!("running bazel query on buildfile: {buildfile}");
115+
fn buildfile_to_targets(workspace: &Utf8Path, buildfile: &Utf8Path) -> anyhow::Result<String> {
116+
log::info!("getting targets for buildfile: {buildfile}");
125117

126118
let parent_dir = buildfile
127119
.strip_prefix(workspace)
128120
.with_context(|| format!("{buildfile} not part of workspace"))?
129121
.parent();
130122

131123
let targets = match parent_dir {
132-
Some(p) if !p.as_str().is_empty() => format!("{p}/..."),
124+
Some(p) if !p.as_str().is_empty() => format!("//{p}/..."),
133125
_ => "//...".to_string(),
134126
};
135127

136-
let query_output = Command::new(bazel)
137-
.current_dir(workspace)
138-
.env_remove("BAZELISK_SKIP_WRAPPER")
139-
.env_remove("BUILD_WORKING_DIRECTORY")
140-
.env_remove("BUILD_WORKSPACE_DIRECTORY")
141-
.arg(format!("--output_base={output_base}"))
142-
.arg("query")
143-
.args(config_group.map(|s| format!("--config={s}")))
144-
.arg(format!(
145-
"kind(\"rust_(library|binary|proc_macro|test)\", {targets})"
146-
))
147-
.output()
148-
.with_context(|| format!("failed to run bazel query for buildfile: {buildfile}"))?;
149-
150-
log::debug!("{}", String::from_utf8_lossy(&query_output.stderr));
151-
log::info!("bazel query for buildfile {buildfile} finished");
152-
153-
let text = String::from_utf8(query_output.stdout)?;
154-
let targets = text.lines().map(ToOwned::to_owned).collect();
155-
156128
Ok(targets)
157129
}

0 commit comments

Comments
 (0)