-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: correct rule's kind & stmt Created rule with `NewRule()` is different to the rule created with `ruleFromExpr()` (when parsing input data). In the first case (`NewRule()`) kind is set to `build.Ident{Name: string}` while it's parsed expression in the second case (`ruleFromExpr()`). For rules packed in a struct like `selects.config_setting_group` there is no way to create/generate the same rule as the one parsed from the file as both will have different kinds. This change unifies kinds for two ways of creating rules and correctly sets the identifier while fixing loads (`FixLoads()`). * chore: fix load tests Change adds test to verify fixing load statements while generating packed rules in struct e.g. `selects.config_setting_group`. * chore: update MODULE.bazel.lock
- Loading branch information
Showing
14 changed files
with
252 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "test_load_for_packed_rules", | ||
srcs = ["lang.go"], | ||
importpath = "github.com/bazelbuild/bazel-gazelle/internal/language/test_load_for_packed_rules", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//config", | ||
"//label", | ||
"//language", | ||
"//repo", | ||
"//resolve", | ||
"//rule", | ||
], | ||
) | ||
|
||
filegroup( | ||
name = "all_files", | ||
testonly = True, | ||
srcs = [ | ||
"BUILD.bazel", | ||
"lang.go", | ||
], | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
alias( | ||
name = "go_default_library", | ||
actual = ":test_load_for_packed_rules", | ||
visibility = ["//:__subpackages__"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* Copyright 2023 The Bazel Authors. All rights reserved. | ||
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 | ||
http://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 `test_load_for_packed_rules` generates packed | ||
// rule of `selects.config_setting_group`. | ||
// | ||
// This extension is experimental and subject to change. It is not included | ||
// in the default Gazelle binary. | ||
package test_load_for_packed_rules | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/bazelbuild/bazel-gazelle/config" | ||
"github.com/bazelbuild/bazel-gazelle/label" | ||
"github.com/bazelbuild/bazel-gazelle/language" | ||
"github.com/bazelbuild/bazel-gazelle/repo" | ||
"github.com/bazelbuild/bazel-gazelle/resolve" | ||
"github.com/bazelbuild/bazel-gazelle/rule" | ||
) | ||
|
||
const testLoadForPackedRulesName = "test_load_for_packed_rules" | ||
|
||
type testLoadForPackedRulesLang struct { | ||
language.BaseLang | ||
|
||
Initialized, RulesGenerated, DepsResolved bool | ||
} | ||
|
||
var ( | ||
_ language.Language = (*testLoadForPackedRulesLang)(nil) | ||
_ language.LifecycleManager = (*testLoadForPackedRulesLang)(nil) | ||
) | ||
|
||
func NewLanguage() language.Language { | ||
return &testLoadForPackedRulesLang{} | ||
} | ||
|
||
var kinds = map[string]rule.KindInfo{ | ||
"selects.config_setting_group": { | ||
NonEmptyAttrs: map[string]bool{"name": true}, | ||
MergeableAttrs: map[string]bool{ | ||
"match_all": true, | ||
"match_any": true, | ||
}, | ||
}, | ||
} | ||
|
||
var loads = []rule.LoadInfo{ | ||
{ | ||
Name: "@bazel_skylib//lib:selects.bzl", | ||
Symbols: []string{ | ||
"selects", | ||
}, | ||
}, | ||
} | ||
|
||
func (*testLoadForPackedRulesLang) Name() string { | ||
return testLoadForPackedRulesName | ||
} | ||
|
||
func (*testLoadForPackedRulesLang) Kinds() map[string]rule.KindInfo { | ||
return kinds | ||
} | ||
|
||
func (*testLoadForPackedRulesLang) Loads() []rule.LoadInfo { | ||
return loads | ||
} | ||
|
||
func (l *testLoadForPackedRulesLang) Before(ctx context.Context) { | ||
l.Initialized = true | ||
} | ||
|
||
func (l *testLoadForPackedRulesLang) GenerateRules(args language.GenerateArgs) language.GenerateResult { | ||
if !l.Initialized { | ||
panic("GenerateRules must not be called before Before") | ||
} | ||
if l.RulesGenerated { | ||
panic("GenerateRules must not be called after DoneGeneratingRules") | ||
} | ||
|
||
r := rule.NewRule("selects.config_setting_group", "all_configs_group") | ||
|
||
match := []string{ | ||
"//:config_a", | ||
"//:config_b", | ||
} | ||
|
||
r.SetAttr("match_all", match) | ||
|
||
return language.GenerateResult{ | ||
Gen: []*rule.Rule{r}, | ||
Imports: []interface{}{nil}, | ||
} | ||
} | ||
|
||
func (l *testLoadForPackedRulesLang) DoneGeneratingRules() { | ||
l.RulesGenerated = true | ||
} | ||
|
||
func (l *testLoadForPackedRulesLang) Resolve(c *config.Config, ix *resolve.RuleIndex, rc *repo.RemoteCache, r *rule.Rule, imports interface{}, from label.Label) { | ||
if !l.RulesGenerated { | ||
panic("Expected a call to DoneGeneratingRules before Resolve") | ||
} | ||
if l.DepsResolved { | ||
panic("Resolve must be called before calling AfterResolvingDeps") | ||
} | ||
} | ||
|
||
func (l *testLoadForPackedRulesLang) AfterResolvingDeps(ctx context.Context) { | ||
l.DepsResolved = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
load("@bazel_skylib//lib:selects.bzl", "selects") | ||
|
||
selects.config_setting_group( | ||
name = "all_configs_group", | ||
match_all = [ | ||
"//:config_a", | ||
"//:config_b", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Fix load statements for packed rules in struct | ||
|
||
Fixes load statement for newly generated `select.config_setting_group` rule. |
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright 2023 The Bazel Authors. All rights reserved. | ||
# | ||
# 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 | ||
# | ||
# http://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. | ||
|
||
def get_binary(target): | ||
"""Get proper binary for test.""" | ||
if "fix_load_for_packed_rules" in target: | ||
return ":gazelle_with_language_load_for_packed_rules" | ||
|
||
if "loads_from_flag" in target: | ||
return ":gazelle_with_language_loads_from_flag" | ||
|
||
return ":gazelle" |