bazelbuild / buildtools

A bazel BUILD file formatter and editor
Apache License 2.0
1.01k stars 415 forks source link

Buildifier doesn't recognise `MODULE.bazel` #1254

Open wingsofovnia opened 6 months ago

wingsofovnia commented 6 months ago

I am using the latest buildifier to check Bazel files in a project with bzlmod enabled.

> ls -1
BUILD.bazel
MODULE.bazel
MODULE.bazel.lock
src

It appears to me that buildifier's linter incorrectly detects the type of MODULE.bazel files. There is a module-docstring violations in this file but buildifier reports nothing:

> buildifier --mode=check --lint=warn -r . && echo "$?"
0
> buildifier --mode=check --lint=warn MODULE.bazel && echo "$?"
0

However, if the type is set to default explicitly, it works fine:

> buildifier --mode=check --lint=warn --type=default MODULE.bazel
MODULE.bazel:1: module-docstring: The file has no module docstring.
A module docstring is a string literal (not a comment) which should be the first statement of a file (it may follow comment lines). (https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#module-docstring)

At the same time, formatting is still detected without setting --type=default:

> buildifier --mode=diff --lint=warn MODULE.bazel
--- MODULE.bazel    2024-03-19 12:34:36
+++ /var/folders/pj/jqnlngf95nlg3qd7l5js_wb40000gr/T/buildifier-tmp-2791490739  2024-03-19 12:34:41
@@ -1,4 +1,5 @@
 bazel_dep(name = "buildifier_prebuilt", version = "6.4.0", dev_dependency = True)
+
 bazel_dep(name = "gazelle", version = "0.35.0")
 bazel_dep(name = "rules_go", version = "0.46.0")
exit status 1

Am I missing something or there is an issue with buildifier?

The version of buildifier used:

> brew info buildifier | head -1
==> buildifier: stable 6.4.0 (bottled), HEAD