Brian Silverman | cc09f18 | 2022-03-09 15:40:20 -0800 | [diff] [blame^] | 1 | #[[ |
| 2 | ## Overview |
| 3 | ]]# |
| 4 | |
| 5 | [Rustfmt][rustfmt] is a tool for formatting Rust code according to style guidelines. |
| 6 | By default, Rustfmt uses a style which conforms to the [Rust style guide][rsg] that |
| 7 | has been formalized through the [style RFC process][rfcp]. A complete list of all |
| 8 | configuration options can be found in the [Rustfmt GitHub Pages][rgp]. |
| 9 | |
| 10 | |
| 11 | #[[ |
| 12 | ### Setup |
| 13 | ]]# |
| 14 | |
| 15 | Formatting your Rust targets' source code requires no setup outside of loading `rules_rust` |
| 16 | in your workspace. Simply run `bazel run @rules_rust//:rustfmt` to format source code. |
| 17 | |
| 18 | In addition to this formatter, a check can be added to your build phase using the [rustfmt_aspect](#rustfmt-aspect) |
| 19 | aspect. Simply add the following to a `.bazelrc` file to enable this check. |
| 20 | |
| 21 | ```text |
| 22 | build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect |
| 23 | build --output_groups=+rustfmt_checks |
| 24 | ``` |
| 25 | |
| 26 | It's recommended to only enable this aspect in your CI environment so formatting issues do not |
| 27 | impact user's ability to rapidly iterate on changes. |
| 28 | |
| 29 | The `rustfmt_aspect` also uses a `--@rules_rust//:rustfmt.toml` setting which determines the |
| 30 | [configuration file][rgp] used by the formatter (`@rules_rust//tools/rustfmt`) and the aspect |
| 31 | (`rustfmt_aspect`). This flag can be added to your `.bazelrc` file to ensure a consistent config |
| 32 | file is used whenever `rustfmt` is run: |
| 33 | |
| 34 | ```text |
| 35 | build --@rules_rust//:rustfmt.toml=//:rustfmt.toml |
| 36 | ``` |
| 37 | |
| 38 | [rustfmt]: https://github.com/rust-lang/rustfmt#readme |
| 39 | [rsg]: https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/guide.md |
| 40 | [rfcp]: https://github.com/rust-lang-nursery/fmt-rfcs |
| 41 | [rgp]: https://rust-lang.github.io/rustfmt/ |