Skip to content

Commit d5a9d8c

Browse files
authored
Updated //tools/rustfmt to dynamically work within rules_rust itself (#883)
1 parent 1e4fbe3 commit d5a9d8c

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

tools/rustfmt/BUILD.bazel

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
load("//rust:defs.bzl", "rust_binary", "rust_clippy", "rust_library")
2+
load(":rustfmt_utils.bzl", "aspect_repository")
23

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

5-
exports_files(["rustfmt.toml"])
6+
exports_files([
7+
"rustfmt.toml",
8+
"rustfmt_utils.bzl",
9+
])
610

711
rust_library(
812
name = "rustfmt_lib",
@@ -30,6 +34,9 @@ rust_binary(
3034
"//:rustfmt.toml",
3135
],
3236
edition = "2018",
37+
rustc_env = {
38+
"ASPECT_REPOSITORY": aspect_repository(),
39+
},
3340
deps = [
3441
":rustfmt_lib",
3542
"//util/label",

tools/rustfmt/rustfmt_utils.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""A helper module for the `@rules_rust//tools/rustfmt` package"""
2+
3+
def aspect_repository():
4+
"""Determines the repository name to use for the `rustfmt_manifest` aspect in `rustfmt` binaries.
5+
6+
The `//tools/rustfmt` target has a hard coded `--aspects` command built into it.
7+
This function is designed to allow for this aspect to be updated to work within
8+
the `rules_rust` repository itself since aspects do not work if the repository name
9+
is explicitly set.
10+
11+
https://github.com/bazelbuild/rules_rust/issues/749
12+
13+
Returns:
14+
str: The string to use for the `rustfmt_aspect` repository
15+
"""
16+
if native.repository_name() == "@":
17+
return ""
18+
return native.repository_name()

tools/rustfmt/srcs/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,12 @@ fn query_rustfmt_targets(options: &Config) -> Vec<String> {
7575
/// arguments to use when formatting the sources of those targets.
7676
fn generate_rustfmt_target_manifests(options: &Config, targets: &[String]) {
7777
let build_args = vec![
78-
"build",
79-
"--aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect",
80-
"--output_groups=rustfmt_manifest",
78+
"build".to_owned(),
79+
format!(
80+
"--aspects={}//rust:defs.bzl%rustfmt_aspect",
81+
env!("ASPECT_REPOSITORY")
82+
),
83+
"--output_groups=rustfmt_manifest".to_owned(),
8184
];
8285

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

0 commit comments

Comments
 (0)