nixfmt icon indicating copy to clipboard operation
nixfmt copied to clipboard

Consider removing empty lines from empty attrset

Open jfly opened this issue 8 months ago • 4 comments

Description

Originally requested by @mightyiam in https://github.com/NixOS/nixfmt/pull/297

Should we ensure that empty attrsets are always formatted the same way?

Small example input

{
  a = {
  };
  b = {

  };
  c = {


  };
}

Expected output

{
  a = { };
  b = { };
  c = { };
}

Actual output

{
  a = {
  };
  b = {

  };
  c = {

  };
}

jfly avatar Apr 30 '25 17:04 jfly

I believe the intention of being somewhat permissive here is to minimise patch differences where entries are frequently added/removed from a set or a list:

E.g.:

 {
   example = {
-    hello = "world";
   };
 }

vs

 {
-  example = {
-    hello = "world";
-  };
+  example = { };
 }

That said, I don't have a strong opinion on whether this is more important than producing a consistent output style.

MattSturgeon avatar Apr 30 '25 18:04 MattSturgeon

Negative. Preserving line breaks and preserving newlines are two different things in Nixfmt.

The original issue for which things got implemented was #188, however I don't remember if the wording in the specification was added before that or due to that.

CC @infinisil you implemented this, I'd like to hear your position.

piegamesde avatar May 01 '25 07:05 piegamesde

Discussed at today's team meeting. This behavior occurs for the same underlying reason as https://github.com/NixOS/nixfmt/issues/298.

We want to continue to preserve empty lines, but we're willing to debate a:

a = {
};

There's no empty line here, so we could reformat this to:

a = { };

Nobody at the meeting has a strong opinion. We'll leave this on our backlog, and if someone wants to make a PR with a strong case, we'll look at it!

jfly avatar May 13 '25 19:05 jfly

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

https://discourse.nixos.org/t/formatting-team-meeting-2025-05-13/64230/1

nixos-discourse avatar May 13 '25 20:05 nixos-discourse