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

[FR]: Should keep-sorted by a formatter? #480

Open
antspy opened this issue Feb 13, 2025 · 4 comments
Open

[FR]: Should keep-sorted by a formatter? #480

antspy opened this issue Feb 13, 2025 · 4 comments
Labels
enhancement New feature or request

Comments

@antspy
Copy link

antspy commented Feb 13, 2025

What is the current behavior?

Keep-sorted is currently integrated as a linter, not as a formatter.

Describe the feature

Should keep-sorted by a formatter instead of a linter? My motivations are:

  1. If I have a // keep-sorted start section, I obviously want this to be sorted. So auto-formatting it to be sorted seems only natural.
  2. bazel lint works on bazel targets, I cannot lint a random file. But often I would need // keep-sorted on files which are not in any bazel targets, e.g. devcontainer.json to sort the list of extensions, or pyproject.toml to sort the list of linting tools, etc.

Let me know if you think this makes sense!

@antspy antspy added the enhancement New feature or request label Feb 13, 2025
@alexeagle
Copy link
Member

Take a look at the root README where I lay out the principles of format vs. lint. In particular formatters are never allowed to change the program's behavior, so that we can hook them into a DX where developers have no choice, and it's safe to apply them blindly across a whole repo including other teams' legacy code.
Changing the order of lines can definitely break things, so I don't think it's safe enough to do blindly.

@alexeagle
Copy link
Member

I should add, yes it's frustrating under my formulation that you need your devcontainer.json to be in a filegroup target simply so an aspect can visit it. I could imagine a third category of DX which is "run outside Bazel but require manual review". It feels to me like that probably exceeds the complexity budget - we gain a lot from the simple mental model.

@antspy
Copy link
Author

antspy commented Feb 14, 2025

Ok, I see - yes I understand that keep-sorted is not safe enough to be used as a formatter.

Perhaps the easiest way would be to instead integrate it into the editor (outside of bazel) so that every file can be "linted" as such.

Unfortunately there doesn't seem to be any editor extensions doing that at the moment (at least for vscode). Will look into it!

@alexeagle
Copy link
Member

Maybe you can simply bazel run @com_github_google_keep_sorted//:keep-sorted - using the binary that's provided here but not hooking with either the linter or formatter. You could put it in pre-commit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants