xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/rust_fmt.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2*d4726bddSHONG Yifan# Rust Fmt
3*d4726bddSHONG Yifan
4*d4726bddSHONG Yifan* [rustfmt_aspect](#rustfmt_aspect)
5*d4726bddSHONG Yifan* [rustfmt_test](#rustfmt_test)
6*d4726bddSHONG Yifan* [rustfmt_toolchain](#rustfmt_toolchain)
7*d4726bddSHONG Yifan
8*d4726bddSHONG Yifan
9*d4726bddSHONG Yifan## Overview
10*d4726bddSHONG Yifan
11*d4726bddSHONG Yifan
12*d4726bddSHONG Yifan[Rustfmt][rustfmt] is a tool for formatting Rust code according to style guidelines.
13*d4726bddSHONG YifanBy default, Rustfmt uses a style which conforms to the [Rust style guide][rsg] that
14*d4726bddSHONG Yifanhas been formalized through the [style RFC process][rfcp]. A complete list of all
15*d4726bddSHONG Yifanconfiguration options can be found in the [Rustfmt GitHub Pages][rgp].
16*d4726bddSHONG Yifan
17*d4726bddSHONG Yifan
18*d4726bddSHONG Yifan
19*d4726bddSHONG Yifan### Setup
20*d4726bddSHONG Yifan
21*d4726bddSHONG Yifan
22*d4726bddSHONG YifanFormatting your Rust targets' source code requires no setup outside of loading `rules_rust`
23*d4726bddSHONG Yifanin your workspace. Simply run `bazel run @rules_rust//:rustfmt` to format source code.
24*d4726bddSHONG Yifan
25*d4726bddSHONG YifanIn addition to this formatter, a simple check can be performed using the [rustfmt_aspect](#rustfmt-aspect) aspect by running
26*d4726bddSHONG Yifan```text
27*d4726bddSHONG Yifanbazel build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect --output_groups=rustfmt_checks
28*d4726bddSHONG Yifan```
29*d4726bddSHONG Yifan
30*d4726bddSHONG YifanAdd the following to a `.bazelrc` file to enable this check during the build phase.
31*d4726bddSHONG Yifan
32*d4726bddSHONG Yifan```text
33*d4726bddSHONG Yifanbuild --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
34*d4726bddSHONG Yifanbuild --output_groups=+rustfmt_checks
35*d4726bddSHONG Yifan```
36*d4726bddSHONG Yifan
37*d4726bddSHONG YifanIt's recommended to only enable this aspect in your CI environment so formatting issues do not
38*d4726bddSHONG Yifanimpact user's ability to rapidly iterate on changes.
39*d4726bddSHONG Yifan
40*d4726bddSHONG YifanThe `rustfmt_aspect` also uses a `--@rules_rust//:rustfmt.toml` setting which determines the
41*d4726bddSHONG Yifan[configuration file][rgp] used by the formatter (`@rules_rust//tools/rustfmt`) and the aspect
42*d4726bddSHONG Yifan(`rustfmt_aspect`). This flag can be added to your `.bazelrc` file to ensure a consistent config
43*d4726bddSHONG Yifanfile is used whenever `rustfmt` is run:
44*d4726bddSHONG Yifan
45*d4726bddSHONG Yifan```text
46*d4726bddSHONG Yifanbuild --@rules_rust//:rustfmt.toml=//:rustfmt.toml
47*d4726bddSHONG Yifan```
48*d4726bddSHONG Yifan
49*d4726bddSHONG Yifan### Tips
50*d4726bddSHONG Yifan
51*d4726bddSHONG Yifan
52*d4726bddSHONG YifanAny target which uses Bazel generated sources will cause the `@rules_rust//tools/rustfmt` tool to fail with
53*d4726bddSHONG Yifan``failed to resolve mod `MOD` ``. To avoid failures, [`skip_children = true`](https://rust-lang.github.io/rustfmt/?version=v1.6.0&search=skip_chil#skip_children)
54*d4726bddSHONG Yifanis recommended to be set in the workspace's `rustfmt.toml` file which allows rustfmt to run on these targets
55*d4726bddSHONG Yifanwithout failing.
56*d4726bddSHONG Yifan
57*d4726bddSHONG Yifan[rustfmt]: https://github.com/rust-lang/rustfmt#readme
58*d4726bddSHONG Yifan[rsg]: https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/guide.md
59*d4726bddSHONG Yifan[rfcp]: https://github.com/rust-lang-nursery/fmt-rfcs
60*d4726bddSHONG Yifan[rgp]: https://rust-lang.github.io/rustfmt/
61*d4726bddSHONG Yifan
62*d4726bddSHONG Yifan<a id="rustfmt_test"></a>
63*d4726bddSHONG Yifan
64*d4726bddSHONG Yifan## rustfmt_test
65*d4726bddSHONG Yifan
66*d4726bddSHONG Yifan<pre>
67*d4726bddSHONG Yifanrustfmt_test(<a href="#rustfmt_test-name">name</a>, <a href="#rustfmt_test-targets">targets</a>)
68*d4726bddSHONG Yifan</pre>
69*d4726bddSHONG Yifan
70*d4726bddSHONG YifanA test rule for performing `rustfmt --check` on a set of targets
71*d4726bddSHONG Yifan
72*d4726bddSHONG Yifan**ATTRIBUTES**
73*d4726bddSHONG Yifan
74*d4726bddSHONG Yifan
75*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
76*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
77*d4726bddSHONG Yifan| <a id="rustfmt_test-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
78*d4726bddSHONG Yifan| <a id="rustfmt_test-targets"></a>targets |  Rust targets to run `rustfmt --check` on.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
79*d4726bddSHONG Yifan
80*d4726bddSHONG Yifan
81*d4726bddSHONG Yifan<a id="rustfmt_toolchain"></a>
82*d4726bddSHONG Yifan
83*d4726bddSHONG Yifan## rustfmt_toolchain
84*d4726bddSHONG Yifan
85*d4726bddSHONG Yifan<pre>
86*d4726bddSHONG Yifanrustfmt_toolchain(<a href="#rustfmt_toolchain-name">name</a>, <a href="#rustfmt_toolchain-rustc">rustc</a>, <a href="#rustfmt_toolchain-rustc_lib">rustc_lib</a>, <a href="#rustfmt_toolchain-rustfmt">rustfmt</a>)
87*d4726bddSHONG Yifan</pre>
88*d4726bddSHONG Yifan
89*d4726bddSHONG YifanA toolchain for [rustfmt](https://rust-lang.github.io/rustfmt/)
90*d4726bddSHONG Yifan
91*d4726bddSHONG Yifan**ATTRIBUTES**
92*d4726bddSHONG Yifan
93*d4726bddSHONG Yifan
94*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
95*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
96*d4726bddSHONG Yifan| <a id="rustfmt_toolchain-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
97*d4726bddSHONG Yifan| <a id="rustfmt_toolchain-rustc"></a>rustc |  The location of the `rustc` binary. Can be a direct source or a filegroup containing one item.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
98*d4726bddSHONG Yifan| <a id="rustfmt_toolchain-rustc_lib"></a>rustc_lib |  The libraries used by rustc during compilation.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
99*d4726bddSHONG Yifan| <a id="rustfmt_toolchain-rustfmt"></a>rustfmt |  The location of the `rustfmt` binary. Can be a direct source or a filegroup containing one item.   | <a href="https://bazel.build/concepts/labels">Label</a> | required |  |
100*d4726bddSHONG Yifan
101*d4726bddSHONG Yifan
102*d4726bddSHONG Yifan<a id="rustfmt_aspect"></a>
103*d4726bddSHONG Yifan
104*d4726bddSHONG Yifan## rustfmt_aspect
105*d4726bddSHONG Yifan
106*d4726bddSHONG Yifan<pre>
107*d4726bddSHONG Yifanrustfmt_aspect(<a href="#rustfmt_aspect-name">name</a>)
108*d4726bddSHONG Yifan</pre>
109*d4726bddSHONG Yifan
110*d4726bddSHONG YifanThis aspect is used to gather information about a crate for use in rustfmt and perform rustfmt checks
111*d4726bddSHONG Yifan
112*d4726bddSHONG YifanOutput Groups:
113*d4726bddSHONG Yifan
114*d4726bddSHONG Yifan- `rustfmt_checks`: Executes `rustfmt --check` on the specified target.
115*d4726bddSHONG Yifan
116*d4726bddSHONG YifanThe build setting `@rules_rust//:rustfmt.toml` is used to control the Rustfmt [configuration settings][cs]
117*d4726bddSHONG Yifanused at runtime.
118*d4726bddSHONG Yifan
119*d4726bddSHONG Yifan[cs]: https://rust-lang.github.io/rustfmt/
120*d4726bddSHONG Yifan
121*d4726bddSHONG YifanThis aspect is executed on any target which provides the `CrateInfo` provider. However
122*d4726bddSHONG Yifanusers may tag a target with `no-rustfmt` or `no-format` to have it skipped. Additionally,
123*d4726bddSHONG Yifangenerated source files are also ignored by this aspect.
124*d4726bddSHONG Yifan
125*d4726bddSHONG Yifan**ASPECT ATTRIBUTES**
126*d4726bddSHONG Yifan
127*d4726bddSHONG Yifan
128*d4726bddSHONG Yifan
129*d4726bddSHONG Yifan**ATTRIBUTES**
130*d4726bddSHONG Yifan
131*d4726bddSHONG Yifan
132*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
133*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
134*d4726bddSHONG Yifan| <a id="rustfmt_aspect-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
135*d4726bddSHONG Yifan
136*d4726bddSHONG Yifan
137