Skip to content

Commit

Permalink
Updated //tools/rustfmt to dynamically work within rules_rust its…
Browse files Browse the repository at this point in the history
…elf (#883)
  • Loading branch information
UebelAndre committed Aug 12, 2021
1 parent 1e4fbe3 commit d5a9d8c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 8 additions & 1 deletion tools/rustfmt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
load("//rust:defs.bzl", "rust_binary", "rust_clippy", "rust_library")
load(":rustfmt_utils.bzl", "aspect_repository")

package(default_visibility = ["//visibility:public"])

exports_files(["rustfmt.toml"])
exports_files([
"rustfmt.toml",
"rustfmt_utils.bzl",
])

rust_library(
name = "rustfmt_lib",
Expand Down Expand Up @@ -30,6 +34,9 @@ rust_binary(
"//:rustfmt.toml",
],
edition = "2018",
rustc_env = {
"ASPECT_REPOSITORY": aspect_repository(),
},
deps = [
":rustfmt_lib",
"//util/label",
Expand Down
18 changes: 18 additions & 0 deletions tools/rustfmt/rustfmt_utils.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""A helper module for the `@rules_rust//tools/rustfmt` package"""

def aspect_repository():
"""Determines the repository name to use for the `rustfmt_manifest` aspect in `rustfmt` binaries.
The `//tools/rustfmt` target has a hard coded `--aspects` command built into it.
This function is designed to allow for this aspect to be updated to work within
the `rules_rust` repository itself since aspects do not work if the repository name
is explicitly set.
https://github.com/bazelbuild/rules_rust/issues/749
Returns:
str: The string to use for the `rustfmt_aspect` repository
"""
if native.repository_name() == "@":
return ""
return native.repository_name()
9 changes: 6 additions & 3 deletions tools/rustfmt/srcs/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ fn query_rustfmt_targets(options: &Config) -> Vec<String> {
/// arguments to use when formatting the sources of those targets.
fn generate_rustfmt_target_manifests(options: &Config, targets: &[String]) {
let build_args = vec![
"build",
"--aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect",
"--output_groups=rustfmt_manifest",
"build".to_owned(),
format!(
"--aspects={}//rust:defs.bzl%rustfmt_aspect",
env!("ASPECT_REPOSITORY")
),
"--output_groups=rustfmt_manifest".to_owned(),
];

let child = Command::new(&options.bazel)
Expand Down

0 comments on commit d5a9d8c

Please sign in to comment.