buildtools icon indicating copy to clipboard operation
buildtools copied to clipboard

buildifier formats square brackets inconsistently

Open obruns opened this issue 2 years ago • 2 comments

Maybe mistakenly I was under the impression that the undocumented (#1084) formatting style is something like this:

cc_library(
    name = "demo",
    srcs = ["one.cpp"],  # standard for single-element lists
    hdrs = [
        # standard for multi-element lists
        "one.hpp",
        "two.hpp",
    ],
    deps = [  # single-element lists like this are unchanged
        "//:foobar",
    ],
)

Yet I see the following behavior with buildifier v5.1.0:

Opening square brackets on the next line ...

cc_library(
    name = "demo",
    srcs = ["one.cpp"],
    hdrs =
[
        "one.hpp",
        "two.hpp",
    ],
    deps = [
        "//:foobar",
    ],
)

... do not get joined with attribute = on the previous line. Instead, all lines that are part of the list get indented:

diff --git a/BUILD b/BUILD
index ebbff44..af1440b 100644
--- a/BUILD
+++ b/BUILD
@@ -2,10 +2,10 @@ cc_library(
     name = "demo",
     srcs = ["one.cpp"],
     hdrs =
-[
-        "one.hpp",
-        "two.hpp",
-    ],
+        [
+            "one.hpp",
+            "two.hpp",
+        ],
     deps = [
         "//:foobar",
     ],

The behavior is the same for single-element lists:

cc_library(
    name = "demo",
    srcs = ["one.cpp"],
    hdrs =
    [ "one.hpp"],
    deps =
    [
        "//:foobar",
    ],
)
diff --git a/BUILD b/BUILD
index 1348355..1bfc0de 100644
--- a/BUILD
+++ b/BUILD
@@ -2,9 +2,9 @@ cc_library(
     name = "demo",
     srcs = ["one.cpp"],
     hdrs =
-    [ "one.hpp"],
+        ["one.hpp"],
     deps =
-    [
-        "//:foobar",
-    ],
+        [
+            "//:foobar",
+        ],
 )

If the formatting style is indeed supposed to look like in the first snippet above, I would start working towards a pull request.

obruns avatar Oct 26 '22 20:10 obruns

I do not consider this a duplicate of #1090 but closely related.

obruns avatar Oct 26 '22 20:10 obruns

At first I thought it was a bug, however it looks like a feature for preventing long lines in case both argument name and value are too long:

my_rule(
    some_long_attribute_name =
        "some long value",
    ...
)

This has nothing to do with square brackets, can be reproduced with any other syntax nodes.

Buildifier allows some flexibility in formatting (more in .bzl files, less in BUILD files), one-line lists mentioned in #1090 is just another example of that.

vladmos avatar Oct 27 '22 10:10 vladmos