|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Formatting the compiler tree" |
| 4 | +author: Mark Rousskov |
| 5 | +description: "How to rebase and what happened" |
| 6 | +team: the compiler team <https://www.rust-lang.org/governance/teams/compiler> |
| 7 | +--- |
| 8 | + |
| 9 | +## What happened |
| 10 | + |
| 11 | +We recently landed two PRs which together reformatted essentially all code in the compiler tree. |
| 12 | + |
| 13 | +The first one, [#65939], contained the initial formatting infrastructure. We currently use `rustfmt` |
| 14 | +directly, pinned to a version specified in `src/stage0.txt`. We expect to update it as needed, and |
| 15 | +otherwise once per cycle (coinciding with the bootstrap bump, most likely). |
| 16 | + |
| 17 | +The second one which reformatted the majority of the codebase is [#67540]. |
| 18 | + |
| 19 | +This change landed with the following rustfmt config. Note that this this configuration is subject |
| 20 | +to change (in particular, merge_derives may be removed in the future), but should be fairly stable. |
| 21 | +Your editor should automatically pick this configuration up inside the rust-lang/rust repository (it |
| 22 | +is located in the `rustfmt.toml` file in the root). |
| 23 | + |
| 24 | +``` |
| 25 | +version = "Two" |
| 26 | +use_small_heuristics = "Max" |
| 27 | +merge_derives = false |
| 28 | +``` |
| 29 | + |
| 30 | +## How to use formatting |
| 31 | + |
| 32 | +You can reformat the repository with `x.py fmt` and `x.py fmt --check` to verify formatting; these |
| 33 | +commands are unfortunately somewhat slow today. Tidy will also currently run the latter of these two |
| 34 | +checks (`x.py fmt --check`) internally, but this may change in the future if we can't improve the |
| 35 | +speed of formatting the entire codebase. |
| 36 | + |
| 37 | +## Resolving conflicts |
| 38 | + |
| 39 | +If you have an ongoing branch, you're likely to have merge conflicts. The following should help you |
| 40 | +resolve them: |
| 41 | + |
| 42 | +``` |
| 43 | +# This should be the name of the remote for rust-lang/rust |
| 44 | +git fetch upstream |
| 45 | +# This rebases up to the bors merge right before formatting landed; |
| 46 | +# it needs to be done manually. |
| 47 | +git rebase -i 9b98af84c4aa66392236fff59c86da2130d46d46 |
| 48 | +# This rebases onto the formatting PR (given the previous command, only that). |
| 49 | +# We tell git to resolve all conflicts in favor of your code (`-Xtheirs`), |
| 50 | +# and the `--exec` command specifies that after each commit lands, it will be formatted. |
| 51 | +# This command will fail if your PR has intermediary commits with syntax conflicts. |
| 52 | +git rebase -i a916ac22b9f7f1f0f7aba0a41a789b3ecd765018 \ |
| 53 | + --exec './x.py fmt && git add -u && git commit --amend` \ |
| 54 | + # This exec is optional, and won't work if your intermediate commits don't build, |
| 55 | + # but it helps make sure that the formatting resolution didn't introduce any errors. |
| 56 | + # It's recommended to run it afterwards before pushing at least. |
| 57 | + --exec './x.py check' \ |
| 58 | + -Xtheirs |
| 59 | +``` |
| 60 | + |
| 61 | +This should mostly resolve conflicts correctly, but occasionally if you've edited something in |
| 62 | +imports (a common case I've encountered) or otherwise this will not resolve quite right. Usually |
| 63 | +though this will solve 99% of the problems and the rest can be fixed up manually afterwards. |
| 64 | + |
| 65 | +[#65939]: https://github.com/rust-lang/rust/pull/65939 |
| 66 | +[#67540]: https://github.com/rust-lang/rust/pull/67540 |
0 commit comments