Idea: rustfmtbot


So I was thinking about the problem of how to ensure that all PRs are rustfmt’d for some of my repos. It occurred to me that the simplest thing would be to have some kind of GitHub hook that automatically runs rustfmt. Ideally, this would be something that one can very, very easily integrate into your repo in one of two modes:

  • Run rustfmt on every PR automatically when it is opened
    • if any changes occur, push a commit to the user’s branch, and leave a comment
  • Run rustfmt on PRs when requested (e.g., @rustfmtbot format)
    • if any changes occur, push a commit to the user’s branch, and leave a comment

This all (hopefully) works because GH now allows you to push to other people’s branches, if you own the repo on which they have opened a PR. I would love to write this but I don’t know much about how GH works and I don’t really have the time. Anybody interested? Any reasons this can’t work?

cc @nrc


The only thing they’d need is to add the bot as a collaborator, then this would totally work.

See also

which is similar and very interesting.


Can’t you include in your CI to run rustfmt --diff, and error if there is a diff?


This is optional by the submitter, “allow edits from maintainers”, but I think it is enabled by default.


You could, but that’s annoying – it forces the person opening the PR to fix it (or me), and so forth. Then I have to wait for them to respond etc. I’d prefer to just make it automatic.


Yes. Seems fine, everybody leaves it on in practice.


It could also be a step of a bors merge, no? I guess you want the diff to follow the formatting as well.


Indeed, but I want this to be usable on any project, not just those that use bors. But I guess if we made bors mega easy to install that might be ok too. (In my ideal world, doing cargo new would give you whatever files you need to have everything be setup already (along with a suitable travis configuration).)


This sounds like it would be a fun project - there is no reason why it shouldn’t work - both the GH APIs and Rustfmt have everything you’d need (although the feature for formatting a selection of text in Rustfmt is a bit flakey, so this would probably find a bunch of bugs).

I’m not sure how easy it is to get a set of ranges of modified lines from a PR. At worst you could process the diff which wouldn’t be too bad, I assume there are libs to help with that. (Because Rustfmt can’t format a diff, you’d need to format the selections in the PR’s branch).


For a first implementation it seems like it could just run rustfmt over the whole codebase, if you’re using this bot then it’s likely that you’re keeping the entire codebase formatted correctly, so the only parts that might need reformatting are the files modified in the PR.