Skip to content

Commit

Permalink
Merge branch 'main' into st/buildifier-rule-load-location
Browse files Browse the repository at this point in the history
  • Loading branch information
styurin authored Jan 29, 2025
2 parents 283c8f9 + 1d76340 commit 3a2c86a
Show file tree
Hide file tree
Showing 15 changed files with 479 additions and 83 deletions.
84 changes: 74 additions & 10 deletions WARNINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,25 @@ Warning categories supported by buildifier's linter:
* [`name-conventions`](#name-conventions)
* [`native-android`](#native-android)
* [`native-build`](#native-build)
* [`native-cc`](#native-cc)
* [`native-cc-binary`](#native-cc-binary)
* [`native-cc-common`](#native-cc-common)
* [`native-cc-debug-package-info`](#native-cc-debug-package-info)
* [`native-cc-fdo-prefetch-hints`](#native-cc-fdo-prefetch-hints)
* [`native-cc-fdo-profile`](#native-cc-fdo-profile)
* [`native-cc-import`](#native-cc-import)
* [`native-cc-info`](#native-cc-info)
* [`native-cc-library`](#native-cc-library)
* [`native-cc-memprof-profile`](#native-cc-memprof-profile)
* [`native-cc-objc-import`](#native-cc-objc-import)
* [`native-cc-objc-library`](#native-cc-objc-library)
* [`native-cc-propeller-optimize`](#native-cc-propeller-optimize)
* [`native-cc-proto`](#native-cc-proto)
* [`native-cc-shared-library`](#native-cc-shared-library)
* [`native-cc-shared-library-hint-info`](#native-cc-shared-library-hint-info)
* [`native-cc-shared-library-info`](#native-cc-shared-library-info)
* [`native-cc-test`](#native-cc-test)
* [`native-cc-toolchain`](#native-cc-toolchain)
* [`native-cc-toolchain-suite`](#native-cc-toolchain-suite)
* [`native-java-binary`](#native-java-binary)
* [`native-java-common`](#native-java-common)
* [`native-java-import`](#native-java-import)
Expand All @@ -61,6 +78,9 @@ Warning categories supported by buildifier's linter:
* [`native-proto-lang-toolchain`](#native-proto-lang-toolchain)
* [`native-proto-lang-toolchain-info`](#native-proto-lang-toolchain-info)
* [`native-py`](#native-py)
* [`native-sh-binary`](#native-sh-binary)
* [`native-sh-library`](#native-sh-library)
* [`native-sh-test`](#native-sh-test)
* [`no-effect`](#no-effect)
* [`out-of-order-load`](#out-of-order-load)
* [`output-group`](#output-group)
Expand Down Expand Up @@ -696,18 +716,32 @@ as global symbols there.

--------------------------------------------------------------------------------

## <a name="native-cc"></a>All C++ build rules should be loaded from Starlark
## <a name="native-cc-binary"></a><a name="native-cc-common"></a><a name="native-cc-debug-package-info"></a><a name="native-cc-fdo-prefetch-hints"></a><a name="native-cc-fdo-profile"></a><a name="native-cc-import"></a><a name="native-cc-info"></a><a name="native-cc-library"></a><a name="native-cc-memprof-profile"></a><a name="native-cc-objc-import"></a><a name="native-cc-objc-library"></a><a name="native-cc-propeller-optimize"></a><a name="native-cc-shared-library"></a><a name="native-cc-shared-library-hint-info"></a><a name="native-cc-shared-library-info"></a><a name="native-cc-test"></a><a name="native-cc-toolchain"></a><a name="native-cc-toolchain-suite"></a>All C++ build rules should be loaded from Starlark

* Category name: `native-cc`
* Flag in Bazel: [`--incompatible_load_cc_rules_from_bzl`](https://github.com/bazelbuild/bazel/issues/8743)
* Category names:
* `native-cc-binary`
* `native-cc-common`
* `native-cc-debug-package-info`
* `native-cc-fdo-prefetch-hints`
* `native-cc-fdo-profile`
* `native-cc-import`
* `native-cc-info`
* `native-cc-library`
* `native-cc-memprof-profile`
* `native-cc-objc-import`
* `native-cc-objc-library`
* `native-cc-propeller-optimize`
* `native-cc-shared-library`
* `native-cc-shared-library-hint-info`
* `native-cc-shared-library-info`
* `native-cc-test`
* `native-cc-toolchain`
* `native-cc-toolchain-suite`
* Flag in Bazel: [`--incompatible_autoload_externally`](https://github.com/bazelbuild/bazel/issues/23043)
* Automatic fix: yes
* [Suppress the warning](#suppress): `# buildifier: disable=native-cc`

The CC build rules should be loaded from Starlark.
* [Suppress the warning](#suppress): `# buildifier: disable=native-cc-binary`, `# buildifier: disable=native-cc-common`, `# buildifier: disable=native-cc-debug-package-info`, `# buildifier: disable=native-cc-fdo-prefetch-hints`, `# buildifier: disable=native-cc-fdo-profile`, `# buildifier: disable=native-cc-import`, `# buildifier: disable=native-cc-info`, `# buildifier: disable=native-cc-library`, `# buildifier: disable=native-cc-memprof-profile`, `# buildifier: disable=native-cc-objc-import`, `# buildifier: disable=native-cc-objc-library`, `# buildifier: disable=native-cc-propeller-optimize`, `# buildifier: disable=native-cc-shared-library`, `# buildifier: disable=native-cc-shared-library-hint-info`, `# buildifier: disable=native-cc-shared-library-info`, `# buildifier: disable=native-cc-test`, `# buildifier: disable=native-cc-toolchain`, `# buildifier: disable=native-cc-toolchain-suite`

Update: the plans for disabling native rules
[have been postponed](https://groups.google.com/g/bazel-discuss/c/XNvpWcge4AE/m/aJ-aQzszAwAJ),
at the moment it's not required to load Starlark rules.
The C++ build rules should be loaded from @rules_cc.

--------------------------------------------------------------------------------

Expand Down Expand Up @@ -946,6 +980,36 @@ at the moment it's not required to load Starlark rules.

--------------------------------------------------------------------------------

## <a name="native-sh-binary"></a>sh_binary build rules should be loaded from Starlark

* Category name: `native-sh-binary`
* Automatic fix: yes
* [Suppress the warning](#suppress): `# buildifier: disable=native-sh-binary`

The sh_binary build rules should be loaded from Starlark.

--------------------------------------------------------------------------------

## <a name="native-sh-library"></a>sh_library build rules should be loaded from Starlark

* Category name: `native-sh-library`
* Automatic fix: yes
* [Suppress the warning](#suppress): `# buildifier: disable=native-sh-library`

The sh_library build rules should be loaded from Starlark.

--------------------------------------------------------------------------------

## <a name="native-sh-test"></a>sh_test build rules should be loaded from Starlark

* Category name: `native-sh-test`
* Automatic fix: yes
* [Suppress the warning](#suppress): `# buildifier: disable=native-sh-test`

The sh_test build rules should be loaded from Starlark.

--------------------------------------------------------------------------------

## <a name="no-effect"></a>Expression result is not used

* Category name: `no-effect`
Expand Down
4 changes: 4 additions & 0 deletions build/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ func (p *printer) useCompactMode(start *Position, list *[]Expr, end *End, mode s
if mode == modeSeq {
return true
}
// Use compact mode for empty call expressions if ForceMultiLine is not set
if mode == modeCall && len(*list) == 0 && !forceMultiLine {
return true
}

// In the Default and .bzl printing modes try to keep the original printing style.
// Non-top-level statements and lists of arguments of a function definition
Expand Down
10 changes: 10 additions & 0 deletions build/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ var rewrites = []struct {
{"reorderarguments", reorderArguments, scopeBoth},
{"editoctal", editOctals, scopeBoth},
{"editfloat", editFloats, scopeBoth},
{"collapseEmpty", collapseEmpty, scopeBoth},
}

// leaveAlone reports whether any of the nodes on the stack are marked
Expand Down Expand Up @@ -1459,3 +1460,12 @@ func removeParens(f *File, _ *Rewriter) {

Edit(f, simplify)
}

// collapseEmpty unsets ForceMultiLine for empty call expressions.
func collapseEmpty(f *File, _ *Rewriter) {
Walk(f, func(expr Expr, stack []Expr) {
if c, ok := expr.(*CallExpr); ok && len(c.List) == 0 { // No arguments
c.ForceMultiLine = false
}
})
}
18 changes: 18 additions & 0 deletions build/testdata/076.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Test collapse empty call expressions

func()

func()

# before comment
func()

func() # after comment

func(
# line comment
)

func(
name = "not_empty",
)
22 changes: 22 additions & 0 deletions build/testdata/076.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Test collapse empty call expressions

func(
)

func(
)

# before comment
func(
)

func(
) # after comment

func(
# line comment
)

func(
name = "not_empty",
)
90 changes: 85 additions & 5 deletions buildifier/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,25 @@ func ExampleExample() {
// "name-conventions",
// "native-android",
// "native-build",
// "native-cc",
// "native-cc-binary",
// "native-cc-common",
// "native-cc-debug-package-info",
// "native-cc-fdo-prefetch-hints",
// "native-cc-fdo-profile",
// "native-cc-import",
// "native-cc-info",
// "native-cc-library",
// "native-cc-memprof-profile",
// "native-cc-objc-import",
// "native-cc-objc-library",
// "native-cc-propeller-optimize",
// "native-cc-proto",
// "native-cc-shared-library",
// "native-cc-shared-library-hint-info",
// "native-cc-shared-library-info",
// "native-cc-test",
// "native-cc-toolchain",
// "native-cc-toolchain-suite",
// "native-java-binary",
// "native-java-common",
// "native-java-import",
Expand All @@ -100,6 +117,9 @@ func ExampleExample() {
// "native-proto-lang-toolchain",
// "native-proto-lang-toolchain-info",
// "native-py",
// "native-sh-binary",
// "native-sh-library",
// "native-sh-test",
// "no-effect",
// "output-group",
// "overly-nested-depset",
Expand Down Expand Up @@ -277,8 +297,25 @@ func TestValidate(t *testing.T) {
"name-conventions",
"native-android",
"native-build",
"native-cc",
"native-cc-binary",
"native-cc-common",
"native-cc-debug-package-info",
"native-cc-fdo-prefetch-hints",
"native-cc-fdo-profile",
"native-cc-import",
"native-cc-info",
"native-cc-library",
"native-cc-memprof-profile",
"native-cc-objc-import",
"native-cc-objc-library",
"native-cc-propeller-optimize",
"native-cc-proto",
"native-cc-shared-library",
"native-cc-shared-library-hint-info",
"native-cc-shared-library-info",
"native-cc-test",
"native-cc-toolchain",
"native-cc-toolchain-suite",
"native-java-binary",
"native-java-common",
"native-java-import",
Expand All @@ -299,6 +336,9 @@ func TestValidate(t *testing.T) {
"native-proto-lang-toolchain",
"native-proto-lang-toolchain-info",
"native-py",
"native-sh-binary",
"native-sh-library",
"native-sh-test",
"no-effect",
"output-group",
"overly-nested-depset",
Expand Down Expand Up @@ -357,8 +397,25 @@ func TestValidate(t *testing.T) {
"name-conventions",
"native-android",
"native-build",
"native-cc",
"native-cc-binary",
"native-cc-common",
"native-cc-debug-package-info",
"native-cc-fdo-prefetch-hints",
"native-cc-fdo-profile",
"native-cc-import",
"native-cc-info",
"native-cc-library",
"native-cc-memprof-profile",
"native-cc-objc-import",
"native-cc-objc-library",
"native-cc-propeller-optimize",
"native-cc-proto",
"native-cc-shared-library",
"native-cc-shared-library-hint-info",
"native-cc-shared-library-info",
"native-cc-test",
"native-cc-toolchain",
"native-cc-toolchain-suite",
"native-java-binary",
"native-java-common",
"native-java-import",
Expand All @@ -379,6 +436,9 @@ func TestValidate(t *testing.T) {
"native-proto-lang-toolchain",
"native-proto-lang-toolchain-info",
"native-py",
"native-sh-binary",
"native-sh-library",
"native-sh-test",
"no-effect",
"output-group",
"overly-nested-depset",
Expand Down Expand Up @@ -437,8 +497,25 @@ func TestValidate(t *testing.T) {
"name-conventions",
"native-android",
"native-build",
"native-cc",
"native-cc-binary",
"native-cc-common",
"native-cc-debug-package-info",
"native-cc-fdo-prefetch-hints",
"native-cc-fdo-profile",
"native-cc-import",
"native-cc-info",
"native-cc-library",
"native-cc-memprof-profile",
"native-cc-objc-import",
"native-cc-objc-library",
"native-cc-propeller-optimize",
"native-cc-proto",
"native-cc-shared-library",
"native-cc-shared-library-hint-info",
"native-cc-shared-library-info",
"native-cc-test",
"native-cc-toolchain",
"native-cc-toolchain-suite",
"native-java-binary",
"native-java-common",
"native-java-import",
Expand All @@ -459,6 +536,9 @@ func TestValidate(t *testing.T) {
"native-proto-lang-toolchain",
"native-proto-lang-toolchain-info",
"native-py",
"native-sh-binary",
"native-sh-library",
"native-sh-test",
"no-effect",
"output-group",
"overly-nested-depset",
Expand All @@ -481,7 +561,7 @@ func TestValidate(t *testing.T) {
"unused-variable",
"unsorted-dict-items",
}},
"warnings error": {options: "--warnings=native-cc,-print,-deprecated-function", wantErr: fmt.Errorf(`warning categories with modifiers ("+" or "-") can't be mixed with raw warning categories`)},
"warnings error": {options: "--warnings=native-py,-print,-deprecated-function", wantErr: fmt.Errorf(`warning categories with modifiers ("+" or "-") can't be mixed with raw warning categories`)},
} {
t.Run(name, func(t *testing.T) {
c := New()
Expand Down
22 changes: 21 additions & 1 deletion buildifier/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,25 @@ cat > golden/.buildifier.example.json <<EOF
"name-conventions",
"native-android",
"native-build",
"native-cc",
"native-cc-binary",
"native-cc-common",
"native-cc-debug-package-info",
"native-cc-fdo-prefetch-hints",
"native-cc-fdo-profile",
"native-cc-import",
"native-cc-info",
"native-cc-library",
"native-cc-memprof-profile",
"native-cc-objc-import",
"native-cc-objc-library",
"native-cc-propeller-optimize",
"native-cc-proto",
"native-cc-shared-library",
"native-cc-shared-library-hint-info",
"native-cc-shared-library-info",
"native-cc-test",
"native-cc-toolchain",
"native-cc-toolchain-suite",
"native-java-binary",
"native-java-common",
"native-java-import",
Expand All @@ -305,6 +322,9 @@ cat > golden/.buildifier.example.json <<EOF
"native-proto-lang-toolchain",
"native-proto-lang-toolchain-info",
"native-py",
"native-sh-binary",
"native-sh-library",
"native-sh-test",
"no-effect",
"output-group",
"overly-nested-depset",
Expand Down
1 change: 1 addition & 0 deletions buildozer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ the attribute, a warning is printed on stderr.
There are some special attributes in the `print` command:

* `kind`: displays the name of the function
* `attrs`: displays the attribute names of the rule
* `label`: the fully qualified label
* `rule`: the entire rule definition
* `startline`: the line number on which the rule begins in the BUILD file
Expand Down
Loading

0 comments on commit 3a2c86a

Please sign in to comment.