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

Add a buildifier warning to enforce rule load location #1318

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions WARNINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ Warning categories supported by buildifier's linter:
* [`repository-name`](#repository-name)
* [`return-value`](#return-value)
* [`rule-impl-return`](#rule-impl-return)
* [`rule-load-location`](#rule-load-location)
* [`same-origin-load`](#same-origin-load)
* [`skylark-comment`](#skylark-comment)
* [`skylark-docstring`](#skylark-docstring)
Expand Down Expand Up @@ -1271,6 +1272,26 @@ consider using
[providers](https://docs.bazel.build/versions/main/skylark/rules.html#providers)
or lists of providers instead.

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

## <a name="rule-load-location"></a>Rule must be loaded from a specific location

* Category name: `rule-load-location`
* Automatic fix: no
* [Suppress the warning](#suppress): `# buildifier: disable=rule-load-location`

Warns when a rule is loaded from a location other than the expected one.
Expected locations are specified in the tables file:

```json
{
"RuleLoadLocation": {
"genrule": "//tools/bazel:genrule.bzl"
}
}
```


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

## <a name="same-origin-load"></a>Same label is used for multiple loads
Expand Down
5 changes: 4 additions & 1 deletion buildifier/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func ExampleExample() {
// "repository-name",
// "return-value",
// "rule-impl-return",
// "rule-load-location",
// "skylark-comment",
// "skylark-docstring",
// "string-iteration",
Expand Down Expand Up @@ -350,6 +351,7 @@ func TestValidate(t *testing.T) {
"repository-name",
"return-value",
"rule-impl-return",
"rule-load-location",
"skylark-comment",
"skylark-docstring",
"string-iteration",
Expand Down Expand Up @@ -449,6 +451,7 @@ func TestValidate(t *testing.T) {
"repository-name",
"return-value",
"rule-impl-return",
"rule-load-location",
"skylark-comment",
"skylark-docstring",
"string-iteration",
Expand Down Expand Up @@ -548,7 +551,7 @@ func TestValidate(t *testing.T) {
"repository-name",
"return-value",
"rule-impl-return",

"rule-load-location",
"skylark-comment",
"skylark-docstring",
"string-iteration",
Expand Down
1 change: 1 addition & 0 deletions buildifier/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ cat > golden/.buildifier.example.json <<EOF
"repository-name",
"return-value",
"rule-impl-return",
"rule-load-location",
"skylark-comment",
"skylark-docstring",
"string-iteration",
Expand Down
5 changes: 3 additions & 2 deletions tables/jsonparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Definitions struct {
NamePriority map[string]int
StripLabelLeadingSlashes bool
ShortenAbsoluteLabelsToRelative bool
RuleLoadLocation map[string]string
}

// ParseJSONDefinitions reads and parses JSON table definitions from file.
Expand All @@ -55,9 +56,9 @@ func ParseAndUpdateJSONDefinitions(file string, merge bool) error {
}

if merge {
MergeTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
MergeTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative, definitions.RuleLoadLocation)
} else {
OverrideTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative)
OverrideTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative, definitions.RuleLoadLocation)
}
return nil
}
1 change: 1 addition & 0 deletions tables/jsonparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestParseJSONDefinitions(t *testing.T) {
SortableAllowlist: map[string]bool{},
NamePriority: map[string]int{"name": -1},
StripLabelLeadingSlashes: true,
RuleLoadLocation: map[string]string{"genrule": "//tools/bazel:genrule.bzl"},
}
if !reflect.DeepEqual(expected, definitions) {
t.Errorf("ParseJSONDefinitions(simple_tables.json) = %v; want %v", definitions, expected)
Expand Down
11 changes: 9 additions & 2 deletions tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,11 @@ var IsModuleOverride = map[string]bool{
"single_version_override": true,
}

// RuleLoadLocation contains custom locations for loading rules.
var RuleLoadLocation = map[string]string{}

// OverrideTables allows a user of the build package to override the special-case rules. The user-provided tables replace the built-in tables.
func OverrideTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sortAllowlist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool) {
func OverrideTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sortAllowlist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool, ruleLoadLocation map[string]string) {
IsLabelArg = labelArg
LabelDenylist = denylist
IsListArg = listArg
Expand All @@ -277,10 +280,11 @@ func OverrideTables(labelArg, denylist, listArg, sortableListArg, sortDenylist,
NamePriority = namePriority
StripLabelLeadingSlashes = stripLabelLeadingSlashes
ShortenAbsoluteLabelsToRelative = shortenAbsoluteLabelsToRelative
RuleLoadLocation = ruleLoadLocation
}

// MergeTables allows a user of the build package to override the special-case rules. The user-provided tables are merged into the built-in tables.
func MergeTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sortAllowlist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool) {
func MergeTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sortAllowlist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool, ruleLoadLocation map[string]string) {
for k, v := range labelArg {
IsLabelArg[k] = v
}
Expand All @@ -304,4 +308,7 @@ func MergeTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sor
}
StripLabelLeadingSlashes = stripLabelLeadingSlashes || StripLabelLeadingSlashes
ShortenAbsoluteLabelsToRelative = shortenAbsoluteLabelsToRelative || ShortenAbsoluteLabelsToRelative
for k, v := range ruleLoadLocation {
RuleLoadLocation[k] = v
}
}
5 changes: 4 additions & 1 deletion tables/testdata/simple_tables.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"NamePriority": {
"name": -1
},
"StripLabelLeadingSlashes": true
"StripLabelLeadingSlashes": true,
"RuleLoadLocation": {
"genrule": "//tools/bazel:genrule.bzl"
}
}
2 changes: 2 additions & 0 deletions warn/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"warn_cosmetic.go",
"warn_deprecated.go",
"warn_docstring.go",
"warn_load.go",
"warn_macro.go",
"warn_naming.go",
"warn_operation.go",
Expand Down Expand Up @@ -42,6 +43,7 @@ go_test(
"warn_cosmetic_test.go",
"warn_deprecated_test.go",
"warn_docstring_test.go",
"warn_load_test.go",
"warn_macro_test.go",
"warn_naming_test.go",
"warn_operation_test.go",
Expand Down
15 changes: 15 additions & 0 deletions warn/docs/warnings.textproto
Original file line number Diff line number Diff line change
Expand Up @@ -931,6 +931,21 @@ warnings: {
"or lists of providers instead."
}

warnings: {
name: "rule-load-location"
header: "Rule must be loaded from a specific location"
description:
"Warns when a rule is loaded from a location other than the expected one.\n"
"Expected locations are specified in the tables file:\n\n"
"```json\n"
"{\n"
" \"RuleLoadLocation\": {\n"
" \"genrule\": \"//tools/bazel:genrule.bzl\"\n"
" }\n"
"}\n"
"```\n"
}

warnings: {
name: "same-origin-load"
header: "Same label is used for multiple loads"
Expand Down
1 change: 1 addition & 0 deletions warn/warn.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ var FileWarningMap = map[string]func(f *build.File) []*LinterFinding{
"redefined-variable": redefinedVariableWarning,
"repository-name": repositoryNameWarning,
"rule-impl-return": ruleImplReturnWarning,
"rule-load-location": ruleLoadLocationWarning,
"return-value": missingReturnValueWarning,
"skylark-comment": skylarkCommentWarning,
"skylark-docstring": skylarkDocstringWarning,
Expand Down
32 changes: 32 additions & 0 deletions warn/warn_load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package warn

import (
"fmt"
"github.com/bazelbuild/buildtools/build"
"github.com/bazelbuild/buildtools/tables"
)

func ruleLoadLocationWarning(f *build.File) []*LinterFinding {
var findings []*LinterFinding

for stmtIndex := 0; stmtIndex < len(f.Stmt); stmtIndex++ {
load, ok := f.Stmt[stmtIndex].(*build.LoadStmt)
if !ok {
continue
}

for i := 0; i < len(load.From); i++ {
from := load.From[i]

expectedLocation, ok := tables.RuleLoadLocation[from.Name]
if !ok || expectedLocation == load.Module.Value {
continue
}

f := makeLinterFinding(from, fmt.Sprintf("Rule %q must be loaded from %v.", from.Name, expectedLocation))
findings = append(findings, f)
}

}
return findings
}
39 changes: 39 additions & 0 deletions warn/warn_load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2020 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package warn

import (
"github.com/bazelbuild/buildtools/tables"
"testing"
)

func TestWarnLoadLocation(t *testing.T) {
tables.RuleLoadLocation["s1"] = ":z.bzl"
tables.RuleLoadLocation["s3"] = ":y.bzl"
checkFindingsAndFix(t, "rule-load-location", `
load(":f.bzl", "s1", "s2")
load(":a.bzl", "s3")
`, `
load(":f.bzl", "s1", "s2")
load(":a.bzl", "s3")
`,
[]string{
":1: Rule \"s1\" must be loaded from :z.bzl.",
":2: Rule \"s3\" must be loaded from :y.bzl.",
},
scopeEverywhere)
}