Skip to content

Format the codebase with clang-format #12957

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

grahamc
Copy link
Member

@grahamc grahamc commented Apr 7, 2025

Motivation

  • It is tough to contribute to a project that doesn't use a formatter,
  • It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files
  • Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose,

Let's rip the bandaid off?

Note that PRs currently in flight should be able to be merged relatively easily by applying clang-format to their tip prior to merge.
I would also expect it to be not too hard to apply patch backports if the backport targets are similarly formatted with clang-format.

Context

Implementation strategy requires a bit of effort on each branch in active maintenance:

  1. Make sure each maintained branch has the same .clang-format file,
  2. Delete the excluded list of unformatted files as the first commit,
  3. Run clang-format (via ./maintainers/format.sh) until it hits steady state (it took two runs) and commit that change
  4. Add the autoformat change to .git-blame-ignore-revs

Once that is done, backports should be easy to apply.

Maintainers can focus on only the change in the first commit, and optionally completely regenerate the second commit to ensure it wasn't tampered with.

This has been done somewhat: new files must be formatted. However, that makes it challenging to contribute with editor integration for code formatting.


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions bot added documentation new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority store Issues and pull requests concerning the Nix store repl The Read Eval Print Loop, "nix repl" command and debugger fetching Networking with the outside (non-Nix) world, input locking c api Nix as a C library with a stable interface labels Apr 7, 2025
@grahamc grahamc force-pushed the check-format branch 2 times, most recently from 577f0b8 to 2bb5def Compare April 7, 2025 20:52
Comment on lines -66 to +68
.handler = {[&](std::string name, std::string expr) { autoArgs.insert_or_assign(name, AutoArg{AutoArgExpr{expr}}); }},
.handler = {[&](std::string name, std::string expr) {
autoArgs.insert_or_assign(name, AutoArg{AutoArgExpr{expr}});
}},
Copy link
Member

Choose a reason for hiding this comment

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

Much better now! :)

Copy link
Member Author

Choose a reason for hiding this comment

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

🎉

Copy link
Member

@roberth roberth left a comment

Choose a reason for hiding this comment

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

Not meaning to delay the bandaid ripping, but it does seem like an opportunity to rip off a proverbial speck of dirt along with it :)

@grahamc grahamc force-pushed the check-format branch 2 times, most recently from a4c85cf to d3d1c7c Compare April 8, 2025 16:23
@edolstra edolstra added this to Nix team Apr 8, 2025
@github-project-automation github-project-automation bot moved this to To triage in Nix team Apr 8, 2025
@edolstra edolstra moved this from To triage to ⚖ To discuss in Nix team Apr 8, 2025
Copy link
Member

@edolstra edolstra left a comment

Choose a reason for hiding this comment

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

Some issues to discuss in the Nix team meeting:

  • What does this do to open PRs?
  • Should this be backported to the stable branches? Generally we want to avoid giant diffs for point releases, but if we don't, it will make backporting much harder.
  • Will this make git history largely useless?

@Mic92
Copy link
Member

Mic92 commented Apr 9, 2025

Some issues to discuss in the Nix team meeting:

* What does this do to open PRs?

In nixpkgs we have some tooling to fix existing pull requests after we reformatted everything with nixfmt. Maybe we can adapt the approach. I can also recommend to install https://mergiraf.org/ into your git locally as it is much more robust against whitespace changes than the git merge algorithm.

* Should this be backported to the stable branches? Generally we want to avoid giant diffs for point releases, but if we don't, it will make backporting much harder.

Probably. Otherwise our backport action becomes useless.

* Will this make git history largely useless?

I believe .git-blame-ignore-revs should prevent this?

@grahamc grahamc force-pushed the check-format branch 5 times, most recently from d0aaa87 to b242452 Compare April 10, 2025 12:35
@Ericson2314
Copy link
Member

Ericson2314 commented Apr 10, 2025

+1 on the things @Mic92 said. I want to try out those tools either way!

What does this do to open PRs?

In addition to what @Mic92 said:

Still, it might make sense to do a dedicated push trying to get the open PR count down first. Yes, that blocks this, but it is a good chore to do in its own right! So I don't mind the delay so much for that reason.

@Mic92
Copy link
Member

Mic92 commented Apr 11, 2025

The result of the last meeting that we all are for this change and we just need to figure out how we migrate old pull requests.

@lf-
Copy link
Member

lf- commented Apr 11, 2025

FYI lix has a .clang-format file here, which you probably want to diff with and check if there's anything you want to change: https://git.lix.systems/lix-project/lix/src/branch/main/.clang-format

We already went through the bikeshedding how to get the formatter to match existing formatting reasonably.

@grahamc
Copy link
Member Author

grahamc commented Apr 12, 2025

This PR is using the existing .clang-format in Nix's repo fwiw

@Ericson2314
Copy link
Member

Having our formatting be in sync does help cherry-pick things back and forth.

@grahamc grahamc force-pushed the check-format branch 3 times, most recently from 2d73c29 to d557902 Compare April 15, 2025 12:57
grahamc added 3 commits April 15, 2025 13:36
* It is tough to contribute to a project that doesn't use a formatter,
* It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files
* Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose,

Let's rip the bandaid off?

Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c api Nix as a C library with a stable interface documentation fetching Networking with the outside (non-Nix) world, input locking new-cli Relating to the "nix" command repl The Read Eval Print Loop, "nix repl" command and debugger store Issues and pull requests concerning the Nix store with-tests Issues related to testing. PRs with tests have some priority
Projects
Status: ⚖ To discuss
Development

Successfully merging this pull request may close these issues.

6 participants