-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Update clang-format automation #25700
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This change updates the `tools/clang_format.py` driver script to exit with non-zero status on error, fix running with `--check-all`, use some of the tricks that have been added to `tools/clang_tidy.py` over time, and more. It also: * Removes the jit paths from the whitelist (they are not formatted). * Updates the pre-commit hook to deal with the non-zero exit status. * Adds `tools/run-clang-format-in-ci.sh`. * Adds an Azure pipeline to run clang-format in CI.
This change updates the `tools/clang_format.py` driver script to exit with non-zero status on error, fix running with `--check-all`, use some of the tricks that have been added to `tools/clang_tidy.py` over time, and more. It also: * Removes the jit paths from the whitelist (they are not formatted). * Updates the pre-commit hook to deal with the non-zero exit status. * Adds `tools/run-clang-format-in-ci.sh`. * Adds an Azure pipeline to run clang-format in CI.
|
Can we update the wiki with information about how to run clang-format locally? https://github.com/pytorch/pytorch/wiki |
| # Exits with non-zero status if clang-format generated changes. | ||
| time python tools/clang_format.py \ | ||
| --verbose \ | ||
| --diff "$BASE_BRANCH" \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's please run clang-format on all files in CI, not the diff only. Having it be diff-based in clang-tidy means that most of the time there is broken code in master and we don't know about it.
ezyang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI formatter should run on all files. If that means that we have to blacklist some files to start, let's do that.
suo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason we never did this is because different version of clang-format produce different results. Notably the default version on fb devservers is at some random point (not a release). So unless we can figure out a way to get everyone using the same clang-format in their editor, this will cause a lot of noise.
|
@suo Thanks for the insight. I figured it was somehow progressive in nature. I just checked and fb devservers have 8.0 installed, which is the same as run in the Azure pipeline. |
|
The version number that clang-format --version reports may be misleading, since fb does some weird stuff with their tooling versioning I think (this may have changed since I last looked at it though). But even if devserver and azure is the same, we would need to get external contributors, devfairs, peoples' laptops, etc. to be on the same version. Generally people have been resistant to adding another hoop for OSS people to jump though to contribute to PyTorch (as I'm sure you know). This is solvable-ish through tooling, for example Chrome stores canonical clang-format binaries for each platform and has a script to download+run the binary on the codebase. We could do the same. |
|
Can we just use Chrome's binaries? :) |
|
Haha, maybe—although they update once in a while atomically with running the new version on the whole codebase, so that would be annoying. If we wanted to go that route, I think the work items would be:
|
|
@ShahriarSS It looks like that script is also a clang-format runner. What makes it more suitable? |
| CLANG_FORMAT_WHITELIST = ["torch/csrc/jit/", "test/cpp/jit/"] | ||
| CLANG_FORMAT_WHITELIST = [ | ||
| "torch/csrc/distributed/", | ||
| "torch/lib/c10d/", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
q: is there a way to check which directories are fully compliant (i.e. maybe by running clang-format recursively on them)? If there is, should we do that and add them here?
| name_to_diff[filename] = diff | ||
|
|
||
| # If no files produced a clang-format diff, exit early. | ||
| if len(name_to_diff) == 0: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this appears to occur when the code is formatted correctly, should we add:
if VERBOSE: print('No formatting changes were found')
to distinguish this from the case where no files were detected? Might help users distinguish between errors caused by passing in files incorrectly versus not having any formatting issues.
|
Hi @pietern! Thank you for your pull request. We require contributors to sign our Contributor License Agreement, and yours needs attention. You currently have a record in our system, but we do not have a signature on file. In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
|
Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as |
Stack from ghstack:
This change updates the
tools/clang_format.pydriver script to exitwith non-zero status on error, fix running with
--check-all, usesome of the tricks that have been added to
tools/clang_tidy.pyovertime, and more.
It also:
tools/run-clang-format-in-ci.sh.