buildtools icon indicating copy to clipboard operation
buildtools copied to clipboard

Add option to sort lists by default

Open davidhao3300 opened this issue 5 years ago • 8 comments

We work in a repo that does not care about dependency order, and we keep our lists in variables. A simple example of a typical file in our repo looks like:

DEPS = [
    ":a"',
    ":b",
    ":c",
]
function_call(deps=DEPS)

We'd like to keep lists sorted by default, and if we ever have a case of wanting to preserve order, we can choose to use the "do not sort" comment.

This PR adds an option to sort by lists by default, and it respects the existing "do not sort" comment.

davidhao3300 avatar May 15 '20 17:05 davidhao3300

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

:memo: Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

googlebot avatar May 15 '20 17:05 googlebot

@googlebot I signed it!

davidhao3300 avatar May 15 '20 17:05 davidhao3300

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

googlebot avatar May 15 '20 17:05 googlebot

what problem does this solve that some combination of: tables.go and jsontables doesn't already solve in combo with # buildifier: do not sort

?

pmbethe09 avatar May 15 '20 17:05 pmbethe09

my understanding is that the code block

DEPS = [
    ":a"',
    ":b",
    ":c",
]

is handled by some subset of the following lines: https://github.com/bazelbuild/buildtools/blob/master/build/rewrite.go#L411-L434 These branches will only sort the list only if the list is prepended with a comment saying "keep sorted". In the current state, we would have to prepend every list with that comment, which is not ideal for us.

davidhao3300 avatar May 15 '20 17:05 davidhao3300

I'm not familiar with what jsontables is, and google doesn't seem to be returning relevant results. What is that?

davidhao3300 avatar May 15 '20 17:05 davidhao3300

It's correct, this is currently not supported.

However, I feel like this is a very niche need and it doesn't match the style recommendations in Bazel. I would encourage you to build your own program (using Buildifier as a library) to perform this transformation; you'll just need to write a few lines of Go. I don't think this feature should belong to Buildifier.

laurentlb avatar May 15 '20 17:05 laurentlb

Based on https://docs.bazel.build/versions/master/skylark/build-style.html, it's true that we should Prefer to list dependencies directly, as a single list. Putting the “common” dependencies of several targets into a variable reduces maintainability, makes it impossible for tools to change the dependencies of a target and can lead to unused dependencies. I'd push back and say that the style guide doesn't forbid the general idea of putting lists in variables, so I think there would be use for others who may keep lists in variables for whatever reason?

davidhao3300 avatar May 15 '20 18:05 davidhao3300