Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated //tools/rustfmt to dynamically work within rules_rust itself #883

Merged
merged 3 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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