From 82a64aab0bba28d6921a4c08176ec60804fa4a98 Mon Sep 17 00:00:00 2001 From: Sergey Tyurin Date: Mon, 16 Dec 2024 17:13:20 -0800 Subject: [PATCH 1/2] Add a buildifier warning to enforce rule load location --- WARNINGS.md | 21 ++++++++++++++++ buildifier/config/config_test.go | 5 +++- buildifier/integration_test.sh | 1 + tables/jsonparser.go | 5 ++-- tables/jsonparser_test.go | 1 + tables/tables.go | 11 +++++++-- tables/testdata/simple_tables.json | 5 +++- warn/BUILD.bazel | 2 ++ warn/docs/warnings.textproto | 15 ++++++++++++ warn/warn.go | 1 + warn/warn_load.go | 32 ++++++++++++++++++++++++ warn/warn_load_test.go | 39 ++++++++++++++++++++++++++++++ 12 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 warn/warn_load.go create mode 100644 warn/warn_load_test.go diff --git a/WARNINGS.md b/WARNINGS.md index 9eed6aad5..7c1b11ecc 100644 --- a/WARNINGS.md +++ b/WARNINGS.md @@ -74,6 +74,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) @@ -1207,6 +1208,26 @@ consider using [providers](https://docs.bazel.build/versions/main/skylark/rules.html#providers) or lists of providers instead. +-------------------------------------------------------------------------------- + +## 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" + } +} +``` + + -------------------------------------------------------------------------------- ## Same label is used for multiple loads diff --git a/buildifier/config/config_test.go b/buildifier/config/config_test.go index dd3344e87..32b9f4b06 100644 --- a/buildifier/config/config_test.go +++ b/buildifier/config/config_test.go @@ -112,6 +112,7 @@ func ExampleExample() { // "repository-name", // "return-value", // "rule-impl-return", + // "rule-load-location", // "skylark-comment", // "skylark-docstring", // "string-iteration", @@ -310,6 +311,7 @@ func TestValidate(t *testing.T) { "repository-name", "return-value", "rule-impl-return", + "rule-load-location", "skylark-comment", "skylark-docstring", "string-iteration", @@ -389,6 +391,7 @@ func TestValidate(t *testing.T) { "repository-name", "return-value", "rule-impl-return", + "rule-load-location", "skylark-comment", "skylark-docstring", "string-iteration", @@ -468,7 +471,7 @@ func TestValidate(t *testing.T) { "repository-name", "return-value", "rule-impl-return", - + "rule-load-location", "skylark-comment", "skylark-docstring", "string-iteration", diff --git a/buildifier/integration_test.sh b/buildifier/integration_test.sh index f5457ea44..b3511edf1 100755 --- a/buildifier/integration_test.sh +++ b/buildifier/integration_test.sh @@ -317,6 +317,7 @@ cat > golden/.buildifier.example.json < Date: Mon, 16 Dec 2024 18:57:00 -0800 Subject: [PATCH 2/2] Minor refactoring --- warn/warn_load.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/warn/warn_load.go b/warn/warn_load.go index fe2d6977f..a16473343 100644 --- a/warn/warn_load.go +++ b/warn/warn_load.go @@ -15,7 +15,7 @@ func ruleLoadLocationWarning(f *build.File) []*LinterFinding { continue } - for i := 0; i < len(load.To); i++ { + for i := 0; i < len(load.From); i++ { from := load.From[i] expectedLocation, ok := tables.RuleLoadLocation[from.Name] @@ -23,7 +23,7 @@ func ruleLoadLocationWarning(f *build.File) []*LinterFinding { continue } - f := makeLinterFinding(load.From[i], fmt.Sprintf("Rule %q must be loaded from %v.", from.Name, expectedLocation)) + f := makeLinterFinding(from, fmt.Sprintf("Rule %q must be loaded from %v.", from.Name, expectedLocation)) findings = append(findings, f) }