xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/rust_fmt.vm (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan#[[
2*d4726bddSHONG Yifan## Overview
3*d4726bddSHONG Yifan]]#
4*d4726bddSHONG Yifan
5*d4726bddSHONG Yifan[Rustfmt][rustfmt] is a tool for formatting Rust code according to style guidelines.
6*d4726bddSHONG YifanBy default, Rustfmt uses a style which conforms to the [Rust style guide][rsg] that
7*d4726bddSHONG Yifanhas been formalized through the [style RFC process][rfcp]. A complete list of all
8*d4726bddSHONG Yifanconfiguration options can be found in the [Rustfmt GitHub Pages][rgp].
9*d4726bddSHONG Yifan
10*d4726bddSHONG Yifan
11*d4726bddSHONG Yifan#[[
12*d4726bddSHONG Yifan### Setup
13*d4726bddSHONG Yifan]]#
14*d4726bddSHONG Yifan
15*d4726bddSHONG YifanFormatting your Rust targets' source code requires no setup outside of loading `rules_rust`
16*d4726bddSHONG Yifanin your workspace. Simply run `bazel run @rules_rust//:rustfmt` to format source code.
17*d4726bddSHONG Yifan
18*d4726bddSHONG YifanIn addition to this formatter, a simple check can be performed using the [rustfmt_aspect](#rustfmt-aspect) aspect by running
19*d4726bddSHONG Yifan```text
20*d4726bddSHONG Yifanbazel build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect --output_groups=rustfmt_checks
21*d4726bddSHONG Yifan```
22*d4726bddSHONG Yifan
23*d4726bddSHONG YifanAdd the following to a `.bazelrc` file to enable this check during the build phase.
24*d4726bddSHONG Yifan
25*d4726bddSHONG Yifan```text
26*d4726bddSHONG Yifanbuild --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect
27*d4726bddSHONG Yifanbuild --output_groups=+rustfmt_checks
28*d4726bddSHONG Yifan```
29*d4726bddSHONG Yifan
30*d4726bddSHONG YifanIt's recommended to only enable this aspect in your CI environment so formatting issues do not
31*d4726bddSHONG Yifanimpact user's ability to rapidly iterate on changes.
32*d4726bddSHONG Yifan
33*d4726bddSHONG YifanThe `rustfmt_aspect` also uses a `--@rules_rust//:rustfmt.toml` setting which determines the
34*d4726bddSHONG Yifan[configuration file][rgp] used by the formatter (`@rules_rust//tools/rustfmt`) and the aspect
35*d4726bddSHONG Yifan(`rustfmt_aspect`). This flag can be added to your `.bazelrc` file to ensure a consistent config
36*d4726bddSHONG Yifanfile is used whenever `rustfmt` is run:
37*d4726bddSHONG Yifan
38*d4726bddSHONG Yifan```text
39*d4726bddSHONG Yifanbuild --@rules_rust//:rustfmt.toml=//:rustfmt.toml
40*d4726bddSHONG Yifan```
41*d4726bddSHONG Yifan#[[
42*d4726bddSHONG Yifan### Tips
43*d4726bddSHONG Yifan]]#
44*d4726bddSHONG Yifan
45*d4726bddSHONG YifanAny target which uses Bazel generated sources will cause the `@rules_rust//tools/rustfmt` tool to fail with
46*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)
47*d4726bddSHONG Yifanis recommended to be set in the workspace's `rustfmt.toml` file which allows rustfmt to run on these targets
48*d4726bddSHONG Yifanwithout failing.
49*d4726bddSHONG Yifan
50*d4726bddSHONG Yifan[rustfmt]: https://github.com/rust-lang/rustfmt#readme
51*d4726bddSHONG Yifan[rsg]: https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/guide.md
52*d4726bddSHONG Yifan[rfcp]: https://github.com/rust-lang-nursery/fmt-rfcs
53*d4726bddSHONG Yifan[rgp]: https://rust-lang.github.io/rustfmt/
54