Skip to content

Commit

Permalink
Merge pull request #8 from djmarcin/rust_analyzer
Browse files Browse the repository at this point in the history
Make sysroot_src a data dependency
  • Loading branch information
UebelAndre committed Nov 16, 2021
2 parents 00e0700 + 875823f commit 6cbd381
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 50 deletions.
6 changes: 1 addition & 5 deletions rust/private/rust_analyzer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ RustAnalyzerInfo = provider(
)

def _rust_analyzer_aspect_impl(target, ctx):
if OutputGroupInfo in target:
if "rust_analyzer_sysroot_src" in target[OutputGroupInfo]:
return [target[OutputGroupInfo]]

if rust_common.crate_info not in target:
return []

Expand Down Expand Up @@ -213,7 +209,7 @@ def _rust_analyzer_detect_sysroot_impl(ctx):
content = sysroot_src,
)

return [OutputGroupInfo(rust_analyzer_sysroot_src = depset([sysroot_src_file]))]
return [DefaultInfo(files = depset([sysroot_src_file]))]

rust_analyzer_detect_sysroot = rule(
implementation = _rust_analyzer_detect_sysroot_impl,
Expand Down
4 changes: 4 additions & 0 deletions tools/rust_analyzer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ rust_library(
["**/*.rs"],
exclude = ["main.rs"],
),
data = [
"//rust/private:rust_analyzer_detect_sysroot",
],
edition = "2018",
deps = [
"//tools/runfiles",
"//tools/rust_analyzer/raze:anyhow",
"//tools/rust_analyzer/raze:log",
"//tools/rust_analyzer/raze:serde",
Expand Down
30 changes: 0 additions & 30 deletions tools/rust_analyzer/aquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,36 +110,6 @@ pub fn get_crate_specs(
Ok(crate_specs.into_values().collect())
}

pub fn get_sysroot_src(
bazel: &Path,
workspace: &Path,
execution_root: &Path,
rules_rust_name: &str,
) -> anyhow::Result<String> {
let aquery_output = Command::new(bazel)
.current_dir(workspace)
.arg("aquery")
.arg("--include_aspects")
.arg(format!(
"--aspects={}//rust:defs.bzl%rust_analyzer_aspect",
rules_rust_name
))
.arg("--output_groups=rust_analyzer_sysroot_src")
.arg(format!(
r#"outputs(".*[.]rust_analyzer_sysroot_src",{}//rust/private:rust_analyzer_detect_sysroot)"#,
rules_rust_name
))
.arg("--output=jsonproto")
.output()?;

let sysroot_src_files =
parse_aquery_output_files(execution_root, &String::from_utf8(aquery_output.stdout)?)?;
log::debug!("sysroot_src_files: {:?}", sysroot_src_files);
debug_assert!(sysroot_src_files.len() == 1);

Ok(std::fs::read_to_string(&sysroot_src_files[0])?)
}

fn parse_aquery_output_files(
execution_root: &Path,
aquery_stdout: &str,
Expand Down
26 changes: 14 additions & 12 deletions tools/rust_analyzer/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use std::path::Path;
use std::process::Command;

use anyhow::anyhow;
use runfiles::Runfiles;

mod aquery;
mod rust_project;

pub fn generate_crate_and_sysroot_info(
pub fn generate_crate_info(
bazel: impl AsRef<Path>,
workspace: impl AsRef<Path>,
rules_rust: impl AsRef<str>,
Expand All @@ -21,11 +22,7 @@ pub fn generate_crate_and_sysroot_info(
"--aspects={}//rust:defs.bzl%rust_analyzer_aspect",
rules_rust.as_ref()
))
.arg("--output_groups=rust_analyzer_crate_spec,rust_analyzer_sysroot_src")
.arg(format!(
"{}//rust/private:rust_analyzer_detect_sysroot",
rules_rust.as_ref()
))
.arg("--output_groups=rust_analyzer_crate_spec")
.args(targets)
.output()?;

Expand Down Expand Up @@ -56,12 +53,17 @@ pub fn write_rust_project(
rules_rust_name.as_ref(),
)?;

let sysroot_src = aquery::get_sysroot_src(
bazel.as_ref(),
workspace.as_ref(),
execution_root.as_ref(),
rules_rust_name.as_ref(),
)?;
let workspace_name = match rules_rust_name.as_ref().trim_start_matches("@") {
"" => "rules_rust",
s => s,
};
let sysroot_path = format!(
"{}/rust/private/rust_analyzer_detect_sysroot.rust_analyzer_sysroot_src",
workspace_name
);
let r = Runfiles::create()?;
let path = r.rlocation(sysroot_path);
let sysroot_src = std::fs::read_to_string(&path)?;

let rust_project = rust_project::generate_rust_project(&sysroot_src, &crate_specs)?;

Expand Down
6 changes: 3 additions & 3 deletions tools/rust_analyzer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;
use std::process::Command;

use anyhow::anyhow;
use gen_rust_project_lib::generate_crate_and_sysroot_info;
use gen_rust_project_lib::generate_crate_info;
use gen_rust_project_lib::write_rust_project;
use structopt::StructOpt;

Expand All @@ -30,8 +30,8 @@ fn main() -> anyhow::Result<()> {

let rules_rust_name = env!("ASPECT_REPOSITORY");

// Generate the crate specs and sysroot src.
generate_crate_and_sysroot_info(&config.bazel, &workspace_root, &rules_rust_name, &targets)?;
// Generate the crate specs.
generate_crate_info(&config.bazel, &workspace_root, &rules_rust_name, &targets)?;

// Use the generated files to write rust-project.json.
write_rust_project(
Expand Down

0 comments on commit 6cbd381

Please sign in to comment.