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

WIP: create an interface to allow for convention checking in gazelle #1900

Open
wants to merge 1 commit into
base: master
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
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ filegroup(
"repository.rst",
"//cmd:all_files",
"//config:all_files",
"//convention:all_files",
"//flag:all_files",
"//internal:all_files",
"//label:all_files",
Expand Down
8 changes: 8 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,14 @@ The following flags are accepted:
| should use the index to resolve dependencies. If this is switched off, Gazelle would rely on |
| ``# gazelle:prefix`` directive or ``-go_prefix`` flag to resolve dependencies. |
+-------------------------------------------------------------------+----------------------------------------+
| :flag:`-use_conventions true|false` | :value:`false` |
+-------------------------------------------------------------------+----------------------------------------+
| Usually used in combindation with `-index=false`, when enabled, this flag will check gazelle languages |
| for compliance with the `Convention` interface. Enabling the convention checker will add |
| `# gazelle:resolve` directives to the root `BUILD.bazel` that do not follow the convention. |
| `CheckConvention(c *config.Config, kind, imp, name, rel string) bool` should be written to return false |
| for the a target and import path pair that do not follow the convention. |
+-------------------------------------------------------------------+----------------------------------------+
| :flag:`-go_grpc_compiler` | ``@io_bazel_rules_go//proto:go_grpc`` |
+-------------------------------------------------------------------+----------------------------------------+
| The protocol buffers compiler to use for building go bindings for gRPC. May be repeated. |
Expand Down
1 change: 1 addition & 0 deletions cmd/gazelle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//config",
"//convention",
"//flag",
"//internal/wspace",
"//label",
Expand Down
26 changes: 21 additions & 5 deletions cmd/gazelle/fix-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/bazelbuild/bazel-gazelle/internal/wspace"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/language"
"github.com/bazelbuild/bazel-gazelle/convention"
"github.com/bazelbuild/bazel-gazelle/merger"
"github.com/bazelbuild/bazel-gazelle/repo"
"github.com/bazelbuild/bazel-gazelle/resolve"
Expand All @@ -55,6 +56,7 @@ type updateConfig struct {
patchBuffer bytes.Buffer
print0 bool
profile profiler
useConventions bool
}

type emitFunc func(c *config.Config, f *rule.File) error
Expand Down Expand Up @@ -90,6 +92,7 @@ func (ucr *updateConfigurer) RegisterFlags(fs *flag.FlagSet, cmd string, c *conf

fs.StringVar(&ucr.mode, "mode", "fix", "print: prints all of the updated BUILD files\n\tfix: rewrites all of the BUILD files in place\n\tdiff: computes the rewrite but then just does a diff")
fs.BoolVar(&ucr.recursive, "r", true, "when true, gazelle will update subdirectories recursively")
fs.BoolVar(&uc.useConventions, "use_conventions", false, "when true, gazelle will enable convention checking in fix-update.")
fs.StringVar(&uc.patchPath, "patch", "", "when set with -mode=diff, gazelle will write to a file instead of stdout")
fs.BoolVar(&uc.print0, "print0", false, "when set with -mode=fix, gazelle will print the names of rewritten files separated with \\0 (NULL)")
fs.StringVar(&ucr.cpuProfile, "cpuprofile", "", "write cpu profile to `file`")
Expand Down Expand Up @@ -263,7 +266,8 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
&config.CommonConfigurer{},
&updateConfigurer{},
&walk.Configurer{},
&resolve.Configurer{})
&resolve.Configurer{},
&convention.Configurer{})

for _, lang := range languages {
cexts = append(cexts, lang)
Expand Down Expand Up @@ -313,6 +317,7 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
}
}()

conventionChecker := convention.NewChecker(c, uc.dirs, mrslv.Resolver, exts...)
var errorsFromWalk []error
walk.Walk(c, cexts, uc.dirs, uc.walkMode, func(dir, rel string, c *config.Config, update bool, f *rule.File, subdirs, regularFiles, genFiles []string) {
// If this file is ignored or if Gazelle was not asked to update this
Expand All @@ -321,9 +326,14 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
for _, repl := range c.KindMap {
mrslv.MappedKind(rel, repl)
}
if c.IndexLibraries && f != nil {
if f != nil {
for _, r := range f.Rules {
ruleIndex.AddRule(c, r, f)
if c.IndexLibraries {
ruleIndex.AddRule(c, r, f)
}
if uc.useConventions {
conventionChecker.AddRule(c, r, f)
}
}
}
return
Expand Down Expand Up @@ -448,10 +458,13 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
})

// Add library rules to the dependency resolution table.
if c.IndexLibraries {
for _, r := range f.Rules {
for _, r := range f.Rules {
if c.IndexLibraries {
ruleIndex.AddRule(c, r, f)
}
if uc.useConventions {
conventionChecker.AddRule(c, r, f)
}
}
})

Expand All @@ -475,6 +488,9 @@ func runFixUpdate(wd string, cmd command, args []string) (err error) {
}

// Finish building the index for dependency resolution.
if uc.useConventions {
conventionChecker.Finish(c, ruleIndex)
}
ruleIndex.Finish()

// Resolve dependencies.
Expand Down
55 changes: 55 additions & 0 deletions convention/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "convention",
srcs = [
"check.go",
"config.go",
"dir_set.go",
],
importpath = "github.com/bazelbuild/bazel-gazelle/convention",
visibility = ["//visibility:public"],
deps = [
"//config",
"//label",
"//resolve",
"//rule",
],
)

alias(
name = "go_default_library",
actual = ":convention",
visibility = ["//visibility:public"],
)

go_test(
name = "convention_test",
srcs = [
"check_test.go",
"config_test.go",
],
embed = [":convention"],
deps = [
"//config",
"//label",
"//language/go",
"//language/proto",
"//resolve",
"//rule",
],
)

filegroup(
name = "all_files",
testonly = True,
srcs = [
"BUILD.bazel",
"check.go",
"check_test.go",
"config.go",
"config_test.go",
"dir_set.go",
],
visibility = ["//visibility:public"],
)
Loading