UebelAndre | eb06a79 | 2021-05-17 10:14:42 -0700 | [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` |
UebelAndre | 7545e98 | 2021-06-02 06:38:52 -0700 | [diff] [blame] | 16 | in your workspace. Simply run `bazel run @rules_rust//:rustfmt` to format source code. |
UebelAndre | eb06a79 | 2021-05-17 10:14:42 -0700 | [diff] [blame] | 17 | |
FĂ©lix C. Morency | 905731a | 2023-02-07 16:58:46 -0500 | [diff] [blame] | 18 | In addition to this formatter, a simple check can be performed using the [rustfmt_aspect](#rustfmt-aspect) aspect by running |
| 19 | ```text |
| 20 | bazel build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect --output_groups=rustfmt_checks |
| 21 | ``` |
| 22 | |
| 23 | Add the following to a `.bazelrc` file to enable this check during the build phase. |
UebelAndre | eb06a79 | 2021-05-17 10:14:42 -0700 | [diff] [blame] | 24 | |
| 25 | ```text |
| 26 | build --aspects=@rules_rust//rust:defs.bzl%rustfmt_aspect |
| 27 | build --output_groups=+rustfmt_checks |
| 28 | ``` |
| 29 | |
| 30 | It's recommended to only enable this aspect in your CI environment so formatting issues do not |
| 31 | impact user's ability to rapidly iterate on changes. |
| 32 | |
| 33 | The `rustfmt_aspect` also uses a `--@rules_rust//:rustfmt.toml` setting which determines the |
| 34 | [configuration file][rgp] used by the formatter (`@rules_rust//tools/rustfmt`) and the aspect |
| 35 | (`rustfmt_aspect`). This flag can be added to your `.bazelrc` file to ensure a consistent config |
| 36 | file is used whenever `rustfmt` is run: |
| 37 | |
| 38 | ```text |
| 39 | build --@rules_rust//:rustfmt.toml=//:rustfmt.toml |
| 40 | ``` |
UebelAndre | ce1b842 | 2023-11-16 04:40:34 -0600 | [diff] [blame] | 41 | #[[ |
| 42 | ### Tips |
| 43 | ]]# |
| 44 | |
| 45 | Any target which uses Bazel generated sources will cause the `@rules_rust//tools/rustfmt` tool to fail with |
| 46 | ``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 | is recommended to be set in the workspace's `rustfmt.toml` file which allows rustfmt to run on these targets |
| 48 | without failing. |
UebelAndre | eb06a79 | 2021-05-17 10:14:42 -0700 | [diff] [blame] | 49 | |
| 50 | [rustfmt]: https://github.com/rust-lang/rustfmt#readme |
| 51 | [rsg]: https://github.com/rust-lang-nursery/fmt-rfcs/blob/master/guide/guide.md |
| 52 | [rfcp]: https://github.com/rust-lang-nursery/fmt-rfcs |
| 53 | [rgp]: https://rust-lang.github.io/rustfmt/ |