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

Enforce nixfmt on new files and changed files that were already formatted #326407

Merged
merged 3 commits into from
Jul 23, 2024

Conversation

infinisil
Copy link
Member

@infinisil infinisil commented Jul 12, 2024

Description of changes

This is a big step towards full implementation of NixOS/rfcs#166, see also NixOS/nixfmt#153. This is split off from #322537 (the first big treewide reformatting), because there's no need to have it be in the same PR. This PR should be reviewed/merged by the @NixOS/nix-formatting team.

This PR makes the Nix format workflow check all new files and changed files (that were already formatted) to be formatted.

Things done


This work is sponsored by Antithesis

Add a 👍 reaction to pull requests you find important.

This makes the Nix format workflow check new/changed files instead of
just an allowlist.

This enforces that all PRs updated after this is merged are required to
have fully standard formatted Nix files!
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux labels Jul 12, 2024
@Pandapip1
Copy link
Contributor

Pandapip1 commented Jul 12, 2024

This PR should only be merged after the other one is merged.

Shouldn't this be the other way round? First, nixfmt is enforced for new/changed files, then the treewide PR is merged?

@infinisil
Copy link
Member Author

@Pandapip1 If we first require all new/changed files to be formatted without doing the treewide format, anybody branching off master would have to format all of the files they're touching to make their PR valid.

The other way around is also not entirely great, because we shouldn't merge PRs that mess up the newly-formatted files either.

Really, these two PRs should be merged around the same time so that both of these problems can be avoided. Splitting it into two PRs is mainly done to make reviewing easier, because the other one is so big (and entirely automated), whereas this one is more tricky.

@infinisil infinisil added the significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc. label Jul 16, 2024
@infinisil
Copy link
Member Author

infinisil commented Jul 17, 2024

Oh, just noticing a problem: If we start doing this, we need to have all-packages.nix (and similarly often updated files) formatted, because otherwise somebody else will have to format it and cause merge conflicts for everybody else. #322537 is "smart" by avoiding any conflicts, but for the case of often updated files this backfires.

My proposed solution to this is to change this PR to enforce formatting of changed files only if they were previously formatted already, so that e.g. it wouldn't be enforced for all files that aren't part of the treewide reformat, including all-packages.nix. We can deal with these special files in the future at some point instead (e.g. see here)

Edit: Now implemented, this makes this PR way more independent of the original one, there's no more need to merge them together :)

The next commit will use this to have a simpler change
This prevents situations where contributors need to suddenly format a
huge file even if they only changed a small part of it (e.g.
all-packages.nix)
@infinisil infinisil force-pushed the enforce-nixfmt-changed-files-actual branch from 6ff7c81 to 8bbfb0b Compare July 17, 2024 05:17
@infinisil infinisil changed the title Enforce nixfmt on new/changed files Enforce nixfmt on new files and changed file that were already formatted Jul 17, 2024
@infinisil infinisil marked this pull request as ready for review July 17, 2024 05:27
@infinisil infinisil changed the title Enforce nixfmt on new files and changed file that were already formatted Enforce nixfmt on new files and changed files that were already formatted Jul 17, 2024
@Pandapip1
Copy link
Contributor

Pandapip1 commented Jul 17, 2024

@Pandapip1 If we first require all new/changed files to be formatted without doing the treewide format, anybody branching off master would have to format all of the files they're touching to make their PR valid.

How about this: CI checks if files that are already nixfmt-rfc-style-compliant have become non-compliant, or if a new non-compliant file has been added?

EDIT:

My proposed solution to this is to change this PR to enforce formatting of changed files only if they were previously formatted already, so that e.g. it wouldn't be enforced for all files that aren't part of the treewide reformat, including all-packages.nix. We can deal with these special files in the future at some point instead (e.g. see NixOS/nixfmt#223)

Great minds think alike, lol. +1 to this solution.

@MattSturgeon
Copy link
Contributor

I think NixOS/nixfmt#179 may block this and #322537, because the /* lang */ tagging pattern is apparently used a lot in the test suite.

e.g:

socketTest = pkgs.writeScript "socket-test.py" /* python */ ''
#!${pkgs.python3}/bin/python3

@infinisil infinisil mentioned this pull request Jul 23, 2024
7 tasks
@infinisil
Copy link
Member Author

@MattSturgeon This isn't relevant for this PR, since it wouldn't enforce formatting on files that have /* foo */ comments in them. Furthermore, I don't think it's a blocker for the treewide format PR either, because treesitter also supports # foo-style comments for syntax hints.

@tomberek tomberek merged commit 1f0b359 into NixOS:master Jul 23, 2024
29 checks passed
@infinisil infinisil deleted the enforce-nixfmt-changed-files-actual branch July 23, 2024 19:05
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2024-07-23/49510/1

@felbinger

This comment was marked as resolved.

@felbinger felbinger mentioned this pull request Jul 25, 2024
13 tasks
@MattSturgeon
Copy link
Contributor

Running the suggested command (see below) resolves in no changes

Are you doing this in the nix shell introduced in #322512? That should provide a nixpkgs on your PATH locked to the same version used by the CI.

Also, just to eliminate the obvious, are you running the command from the right location for those relative paths?

@felbinger
Copy link
Member

felbinger commented Jul 25, 2024

Running the suggested command (see below) resolves in no changes

Are you doing this in the nix shell introduced in #322512? That should provide a nixpkgs on your PATH locked to the same version used by the CI.

Also, just to eliminate the obvious, are you running the command from the right location for those relative paths?

I actually didn't use the shell.nix, because I wasn't aware of it. Thanks for pointing me in the right direction
Even though i don't really understand whats the difference between it and nix-shell -p nixfmt-rfc-style - Oh: I think it might be using an older version of nixfmt 🤔

@MattSturgeon
Copy link
Contributor

Even though i don't really understand whats the difference between it and nix-shell -p nixfmt-rfc-style

In most cases there should be no tangible difference, afaik. However in some cases the version used by the shell (and CI) may be different to the version in nixos-unstable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: policy discussion 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux significant Novel ideas, large API changes, notable refactorings, issues with RFC potential, etc.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants