Skip to content
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

Move clang-format checks to pre-commit #787

Merged
merged 1 commit into from
Nov 28, 2024

Conversation

stephenswat
Copy link
Member

This will make them significantly easier for users to use, as well as easier to migrate to future versions.

@stephenswat stephenswat added the cicd Changes related to the CI system label Nov 27, 2024
Copy link
Member

@paulgessinger paulgessinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Are you considering using this for other linters?

@stephenswat
Copy link
Member Author

Yes, in time!

@StewMH
Copy link
Contributor

StewMH commented Nov 27, 2024

Is it worth keeping the CI check in case it fails locally in some way and something badly formatted creeps in?

@stephenswat stephenswat disabled auto-merge November 27, 2024 14:56
@stephenswat
Copy link
Member Author

stephenswat commented Nov 27, 2024

Is it worth keeping the CI check in case it fails locally in some way and something badly formatted creeps in?

You mean to keep the local check_format.sh file? I would suggest we toss that out as it is no longer necessary. pre-commit can be used locally very simply, e.g.:

$ pip install pre-commit
$ pre-commit run --all-files

Let me know what you think.

@StewMH
Copy link
Contributor

StewMH commented Nov 27, 2024

Right I mean if the pre-commit hook fails in some way. But maybe this is obvious/unlikely

@stephenswat
Copy link
Member Author

Right I mean if the pre-commit hook fails in some way. But maybe this is obvious/unlikely

Ah, I think I understand what you mean. Consider, though, that pre-commit will automatically update the files for you so they conform to the clang-format requirements; so in principle if you run pre-commit locally (which doesn't have to be in a pre-commit hook, by the way!) the code should always pass the CI job.

@krasznaa
Copy link
Member

I'm very unfamiliar with this... 🤔 How does this work? Does one need to install some extra piece of software? Is it really that a git commit will now forcefully update the code, not allowing formatting at all that clang-format doesn't agree with? 😕

I personally really don't view the current setup we have as so much of a problem. But if this is easy enough to use, we can give it a try, sure...

@krasznaa
Copy link
Member

Is it worth keeping the CI check in case it fails locally in some way and something badly formatted creeps in?

You mean to keep the local check_format.sh file? I would suggest we toss that out as it is no longer necessary. pre-commit can be used locally very simply, e.g.:

$ pip install pre-commit
$ pre-commit run --all-files

Let me know what you think.

So you do need to install something? Why is it way better to install this, instead of Docker? 😕

@stephenswat
Copy link
Member Author

I'm very unfamiliar with this... 🤔 How does this work? Does one need to install some extra piece of software? Is it really that a git commit will now forcefully update the code, not allowing formatting at all that clang-format doesn't agree with? 😕

I personally really don't view the current setup we have as so much of a problem. But if this is easy enough to use, we can give it a try, sure...

Even though the tool is called pre-commit, it's not actually required to be in a pre-commit hook at all; it also works as a stand-alone tool and - as shown in the PR - as a CI job. So it's still perfectly possible to commit and push code that is improperly formatted. It will just be rejected by the CI, as it is right now.

The problem with the current setup is that it is a bit of a pain in the rear for users to find the right versions of clang-format; you can download some pre-build binaries from a slightly sketchy git repository or you can use your package manager, but the latter often only supports the latest few versions of clang-format.

So you do need to install something? Why is it way better to install this, instead of Docker? 😕

Well, pre-format makes it much easier for users to use the exact same setup as the CI to reduce version conflicts. Also, spinning up a Docker container every time you want to format the code seems like a lot of unnecessary work. Still, this change doesn't prohibit that at all; users who prefer to use Docker are completely free to do that if they so please.

@stephenswat
Copy link
Member Author

This PR came to be because we talked about updating the clang-format versions on Mattermost. Without pre-commit, this would require users to find a way to install the new version of clang-format; with pre-commit, zero user action would be required.

@krasznaa
Copy link
Member

My main point is that you have to write instructions as part of this PR on how to use this pre-commit setup. I myself have no idea for instance.

This will make them significantly easier for users to use, as well as
easier to migrate to future versions.
@stephenswat
Copy link
Member Author

Fair point, I added some documentation to the README. 👍


```console
$ pre-commit run --all-files
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to mention that pre-commit can made to run as a pre-commit hook but does not by default (despite the name). And that you can enable that for your local git checkout with

$ pre-commit install

@StewMH
Copy link
Contributor

StewMH commented Nov 27, 2024

Getting the diff from a failed format run is nice - I tested this out here:
https://github.com/StewMH/traccc/actions/runs/12054648976/job/33613171270

clang-format.............................................................Failed
- hook id: clang-format
- files were modified by this hook
pre-commit hook(s) made changes.
If you are seeing this message in CI, reproduce locally with: `pre-commit run --all-files`.
To run `pre-commit` as part of git workflow, use `pre-commit install`.
All changes made by hooks:
diff --git a/tests/alpaka/alpaka_basic.cpp b/tests/alpaka/alpaka_basic.cpp
index a2dbbc6..33dbd6f 100644
--- a/tests/alpaka/alpaka_basic.cpp
+++ b/tests/alpaka/alpaka_basic.cpp
@@ -33,145 +33,145 @@
 
 template <typename Acc>
 ALPAKA_FN_ACC float process(Acc const& acc, uint32_t idx) {
-  return static_cast<float>(alpaka::math::sin(acc, idx));
+    return static_cast<float>(alpaka::math::sin(acc, idx));
 }
...

so from that point of view it is an improvement on our current system: you don't actually have to set up clang-format locally if you're willing to just apply the diff.

@stephenswat stephenswat merged commit 69a4714 into acts-project:main Nov 28, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cicd Changes related to the CI system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants