Skip to content

Commit ed3e499

Browse files
committed
Rename to AllowedSymbolLoadLocations
1 parent 85ba582 commit ed3e499

File tree

11 files changed

+197
-124
lines changed

11 files changed

+197
-124
lines changed

WARNINGS.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Warning categories supported by buildifier's linter:
44

5+
* [`allowed-symbol-load-locations`](#allowed-symbol-load-locations)
56
* [`attr-applicable_licenses`](#attr-applicable_licenses)
67
* [`attr-cfg`](#attr-cfg)
78
* [`attr-license`](#attr-license)
@@ -96,7 +97,6 @@ Warning categories supported by buildifier's linter:
9697
* [`repository-name`](#repository-name)
9798
* [`return-value`](#return-value)
9899
* [`rule-impl-return`](#rule-impl-return)
99-
* [`rule-load-location`](#rule-load-location)
100100
* [`same-origin-load`](#same-origin-load)
101101
* [`skylark-comment`](#skylark-comment)
102102
* [`skylark-docstring`](#skylark-docstring)
@@ -129,6 +129,27 @@ if debug:
129129

130130
--------------------------------------------------------------------------------
131131

132+
## <a name="allowed-symbol-load-locations"></a>Symbol must be loaded from a specific location
133+
134+
* Category name: `allowed-symbol-load-locations`
135+
* Automatic fix: no
136+
* [Suppress the warning](#suppress): `# buildifier: disable=allowed-symbol-load-locations`
137+
138+
Warns when a symbol is loaded from a location other than the expected ones.
139+
Expected locations are specified in the tables file:
140+
141+
```json
142+
{
143+
"AllowedSymbolLoadLocations": {
144+
"genrule": [
145+
"//tools/bazel:genrule.bzl"
146+
]
147+
}
148+
}
149+
```
150+
151+
--------------------------------------------------------------------------------
152+
132153
## <a name="attr-applicable_licenses"></a>Do not use `applicable_licenses` as an attribute name.
133154

134155
* Category name: `attr-applicable_licenses`
@@ -1341,26 +1362,6 @@ consider using
13411362
[providers](https://docs.bazel.build/versions/main/skylark/rules.html#providers)
13421363
or lists of providers instead.
13431364

1344-
--------------------------------------------------------------------------------
1345-
1346-
## <a name="rule-load-location"></a>Rule must be loaded from a specific location
1347-
1348-
* Category name: `rule-load-location`
1349-
* Automatic fix: no
1350-
* [Suppress the warning](#suppress): `# buildifier: disable=rule-load-location`
1351-
1352-
Warns when a rule is loaded from a location other than the expected one.
1353-
Expected locations are specified in the tables file:
1354-
1355-
```json
1356-
{
1357-
"RuleLoadLocation": {
1358-
"genrule": "//tools/bazel:genrule.bzl"
1359-
}
1360-
}
1361-
```
1362-
1363-
13641365
--------------------------------------------------------------------------------
13651366

13661367
## <a name="same-origin-load"></a>Same label is used for multiple loads

buildifier/config/config_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func ExampleExample() {
4343
// "mode": "fix",
4444
// "lint": "fix",
4545
// "warningsList": [
46+
// "allowed-symbol-load-locations",
4647
// "attr-applicable_licenses",
4748
// "attr-cfg",
4849
// "attr-license",
@@ -134,7 +135,6 @@ func ExampleExample() {
134135
// "repository-name",
135136
// "return-value",
136137
// "rule-impl-return",
137-
// "rule-load-location",
138138
// "skylark-comment",
139139
// "skylark-docstring",
140140
// "string-iteration",
@@ -264,6 +264,7 @@ func TestValidate(t *testing.T) {
264264
"type auto": {options: "--type=auto"},
265265
"type error": {options: "--type=foo", wantErr: fmt.Errorf("unrecognized input type foo; valid types are build, bzl, workspace, default, module, auto")},
266266
"warnings all": {options: "--warnings=all", wantWarnings: []string{
267+
"allowed-symbol-load-locations",
267268
"attr-applicable_licenses",
268269
"attr-cfg",
269270
"attr-license",
@@ -355,7 +356,6 @@ func TestValidate(t *testing.T) {
355356
"repository-name",
356357
"return-value",
357358
"rule-impl-return",
358-
"rule-load-location",
359359
"skylark-comment",
360360
"skylark-docstring",
361361
"string-iteration",
@@ -366,6 +366,7 @@ func TestValidate(t *testing.T) {
366366
"unused-variable",
367367
}},
368368
"warnings default": {options: "--warnings=default", wantWarnings: []string{
369+
"allowed-symbol-load-locations",
369370
"attr-applicable_licenses",
370371
"attr-cfg",
371372
"attr-license",
@@ -457,7 +458,6 @@ func TestValidate(t *testing.T) {
457458
"repository-name",
458459
"return-value",
459460
"rule-impl-return",
460-
"rule-load-location",
461461
"skylark-comment",
462462
"skylark-docstring",
463463
"string-iteration",
@@ -468,6 +468,7 @@ func TestValidate(t *testing.T) {
468468
"unused-variable",
469469
}},
470470
"warnings plus/minus": {options: "--warnings=+unsorted-dict-items,-print,-deprecated-function", wantWarnings: []string{
471+
"allowed-symbol-load-locations",
471472
"attr-applicable_licenses",
472473
"attr-cfg",
473474
"attr-license",
@@ -559,7 +560,6 @@ func TestValidate(t *testing.T) {
559560
"repository-name",
560561
"return-value",
561562
"rule-impl-return",
562-
"rule-load-location",
563563
"skylark-comment",
564564
"skylark-docstring",
565565
"string-iteration",
@@ -570,6 +570,7 @@ func TestValidate(t *testing.T) {
570570
"unused-variable",
571571
}},
572572
"warnings no duplicates": {options: "--warnings=+unused-variable", wantWarnings: []string{
573+
"allowed-symbol-load-locations",
573574
"attr-applicable_licenses",
574575
"attr-cfg",
575576
"attr-license",
@@ -661,7 +662,6 @@ func TestValidate(t *testing.T) {
661662
"repository-name",
662663
"return-value",
663664
"rule-impl-return",
664-
665665
"skylark-comment",
666666
"skylark-docstring",
667667
"string-iteration",

buildifier/integration_test.sh

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ cat > golden/.buildifier.example.json <<EOF
258258
"mode": "fix",
259259
"lint": "fix",
260260
"warningsList": [
261+
"allowed-symbol-load-locations",
261262
"attr-applicable_licenses",
262263
"attr-cfg",
263264
"attr-license",
@@ -349,7 +350,6 @@ cat > golden/.buildifier.example.json <<EOF
349350
"repository-name",
350351
"return-value",
351352
"rule-impl-return",
352-
"rule-load-location",
353353
"skylark-comment",
354354
"skylark-docstring",
355355
"string-iteration",
@@ -692,3 +692,36 @@ EOF
692692

693693
$buildifier --lint=warn --warnings=deprecated-function BUILD 2> report || ret=$?
694694
diff -u report_golden report || die "$1: wrong console output for multifile warnings (WORKSPACE exists)"
695+
696+
cd ..
697+
698+
# Test allowed symbol load locations
699+
700+
mkdir test_dir/allowed_locations
701+
cd test_dir/allowed_locations
702+
703+
cat > BUILD <<EOF
704+
load(":f.bzl", "s1", "s2")
705+
load(":a.bzl", "s3")
706+
load(":a.bzl", "s4")
707+
EOF
708+
709+
cat > buildifier.tables <<EOF
710+
{
711+
"AllowedSymbolLoadLocations": {
712+
"s1": [":z.bzl"],
713+
"s3": [":y.bzl", ":x.bzl"],
714+
"s4": [":a.bzl"]
715+
}
716+
}
717+
EOF
718+
719+
cat > report_golden <<EOF
720+
BUILD:1: allowed-symbol-load-locations: Symbol "s1" must be loaded from :z.bzl. (https://github.com/bazelbuild/buildtools/blob/main/WARNINGS.md#allowed-symbol-load-locations)
721+
BUILD:2: allowed-symbol-load-locations: Symbol "s3" must be loaded from one of the allowed locations: :x.bzl, :y.bzl. (https://github.com/bazelbuild/buildtools/blob/main/WARNINGS.md#allowed-symbol-load-locations)
722+
EOF
723+
724+
$buildifier --lint=warn --warnings=allowed-symbol-load-locations -tables=buildifier.tables BUILD 2> report || true
725+
diff -u report_golden report || die "$1: wrong console output for allowed symbol load locations"
726+
727+
cd ../..

tables/jsonparser.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ type Definitions struct {
3131
NamePriority map[string]int
3232
StripLabelLeadingSlashes bool
3333
ShortenAbsoluteLabelsToRelative bool
34-
RuleLoadLocation map[string]string
34+
AllowedSymbolLoadLocations map[string][]string
3535
}
3636

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

5858
if merge {
59-
MergeTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative, definitions.RuleLoadLocation)
59+
MergeTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative, definitions.AllowedSymbolLoadLocations)
6060
} else {
61-
OverrideTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative, definitions.RuleLoadLocation)
61+
OverrideTables(definitions.IsLabelArg, definitions.LabelDenylist, definitions.IsListArg, definitions.IsSortableListArg, definitions.SortableDenylist, definitions.SortableAllowlist, definitions.NamePriority, definitions.StripLabelLeadingSlashes, definitions.ShortenAbsoluteLabelsToRelative, definitions.AllowedSymbolLoadLocations)
6262
}
6363
return nil
6464
}

tables/jsonparser_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ func TestParseJSONDefinitions(t *testing.T) {
3030
}
3131

3232
expected := Definitions{
33-
IsLabelArg: map[string]bool{"srcs": true},
34-
LabelDenylist: map[string]bool{},
35-
IsSortableListArg: map[string]bool{"srcs": true, "visibility": true},
36-
SortableDenylist: map[string]bool{"genrule.srcs": true},
37-
SortableAllowlist: map[string]bool{},
38-
NamePriority: map[string]int{"name": -1},
39-
StripLabelLeadingSlashes: true,
40-
RuleLoadLocation: map[string]string{"genrule": "//tools/bazel:genrule.bzl"},
33+
IsLabelArg: map[string]bool{"srcs": true},
34+
LabelDenylist: map[string]bool{},
35+
IsSortableListArg: map[string]bool{"srcs": true, "visibility": true},
36+
SortableDenylist: map[string]bool{"genrule.srcs": true},
37+
SortableAllowlist: map[string]bool{},
38+
NamePriority: map[string]int{"name": -1},
39+
StripLabelLeadingSlashes: true,
40+
AllowedSymbolLoadLocations: map[string][]string{"genrule": {"//tools/bazel:genrule.bzl"}},
4141
}
4242
if !reflect.DeepEqual(expected, definitions) {
4343
t.Errorf("ParseJSONDefinitions(simple_tables.json) = %v; want %v", definitions, expected)

tables/tables.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,11 @@ var IsModuleOverride = map[string]bool{
265265
"single_version_override": true,
266266
}
267267

268-
// RuleLoadLocation contains custom locations for loading rules.
269-
var RuleLoadLocation = map[string]string{}
268+
// AllowedSymbolLoadLocations contains locations for loading rules that are allowed to be used.
269+
var AllowedSymbolLoadLocations = map[string]map[string]bool{}
270270

271271
// OverrideTables allows a user of the build package to override the special-case rules. The user-provided tables replace the built-in tables.
272-
func OverrideTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sortAllowlist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool, ruleLoadLocation map[string]string) {
272+
func OverrideTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sortAllowlist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool, symbolLoadLocation map[string][]string) {
273273
IsLabelArg = labelArg
274274
LabelDenylist = denylist
275275
IsListArg = listArg
@@ -279,11 +279,19 @@ func OverrideTables(labelArg, denylist, listArg, sortableListArg, sortDenylist,
279279
NamePriority = namePriority
280280
StripLabelLeadingSlashes = stripLabelLeadingSlashes
281281
ShortenAbsoluteLabelsToRelative = shortenAbsoluteLabelsToRelative
282-
RuleLoadLocation = ruleLoadLocation
282+
283+
AllowedSymbolLoadLocations = map[string]map[string]bool{}
284+
for k, v := range symbolLoadLocation {
285+
locations := map[string]bool{}
286+
for _, l := range v {
287+
locations[l] = true
288+
}
289+
AllowedSymbolLoadLocations[k] = locations
290+
}
283291
}
284292

285293
// 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.
286-
func MergeTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sortAllowlist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool, ruleLoadLocation map[string]string) {
294+
func MergeTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sortAllowlist map[string]bool, namePriority map[string]int, stripLabelLeadingSlashes, shortenAbsoluteLabelsToRelative bool, symbolLoadLocation map[string][]string) {
287295
for k, v := range labelArg {
288296
IsLabelArg[k] = v
289297
}
@@ -307,7 +315,12 @@ func MergeTables(labelArg, denylist, listArg, sortableListArg, sortDenylist, sor
307315
}
308316
StripLabelLeadingSlashes = stripLabelLeadingSlashes || StripLabelLeadingSlashes
309317
ShortenAbsoluteLabelsToRelative = shortenAbsoluteLabelsToRelative || ShortenAbsoluteLabelsToRelative
310-
for k, v := range ruleLoadLocation {
311-
RuleLoadLocation[k] = v
318+
319+
for k, v := range symbolLoadLocation {
320+
locations := map[string]bool{}
321+
for _, l := range v {
322+
locations[l] = true
323+
}
324+
AllowedSymbolLoadLocations[k] = locations
312325
}
313326
}

tables/testdata/simple_tables.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
"name": -1
2121
},
2222
"StripLabelLeadingSlashes": true,
23-
"RuleLoadLocation": {
24-
"genrule": "//tools/bazel:genrule.bzl"
23+
"AllowedSymbolLoadLocations": {
24+
"genrule": [
25+
"//tools/bazel:genrule.bzl"
26+
]
2527
}
2628
}

warn/docs/warnings.textproto

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@
22
# proto-message: Warnings
33
# After modifying this file, run `bazel build //warn/docs:warnings_docs && cp bazel-bin/warn/docs/WARNINGS.md .`
44

5+
warnings: {
6+
name: "allowed-symbol-load-locations"
7+
header: "Symbol must be loaded from a specific location"
8+
description:
9+
"Warns when a symbol is loaded from a location other than the expected ones.\n"
10+
"Expected locations are specified in the tables file:\n\n"
11+
"```json\n"
12+
"{\n"
13+
" \"AllowedSymbolLoadLocations\": {\n"
14+
" \"genrule\": [\n"
15+
" \"//tools/bazel:genrule.bzl\"\n"
16+
" ]\n"
17+
" }\n"
18+
"}\n"
19+
"```"
20+
}
21+
522
warnings: {
623
name: "attr-cfg"
724
header: "`cfg = \"data\"` for attr definitions has no effect"
@@ -983,21 +1000,6 @@ warnings: {
9831000
"or lists of providers instead."
9841001
}
9851002

986-
warnings: {
987-
name: "rule-load-location"
988-
header: "Rule must be loaded from a specific location"
989-
description:
990-
"Warns when a rule is loaded from a location other than the expected one.\n"
991-
"Expected locations are specified in the tables file:\n\n"
992-
"```json\n"
993-
"{\n"
994-
" \"RuleLoadLocation\": {\n"
995-
" \"genrule\": \"//tools/bazel:genrule.bzl\"\n"
996-
" }\n"
997-
"}\n"
998-
"```\n"
999-
}
1000-
10011003
warnings: {
10021004
name: "same-origin-load"
10031005
header: "Same label is used for multiple loads"

0 commit comments

Comments
 (0)