bazelbuild / buildtools

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

Fix false positive warnings for package on top #1214

Closed vladmos closed 11 months ago

vladmos commented 11 months ago

A package declaration may be not at the top of the file if it depends on some variables defined earlier:

VISIBILITY = [...]

package(
    ...
    default_visibility = VISIBILITY,
)

This dependance may also be indirect and it may be hard to determine whether the package declaration can safely be moved on top, and, if so, together with which assignment expressions. To avoid false positive warnings, with this buildifier will just suppress package-on-top warnings in case there's any assignment expression above package declarations. If the visibility scope in the example above has been loaded from another file it wouldn't have cause a similar issue because load statements come before package declarations by design (which is also enforced by other warnings) and loaded symbols are frozen and can't be mutated in the current file by other types of statements (non-assignment statements, e.g. VISIBILITY.append("bar") to modify the visibility before using it in the package declaration is not valid anyway).