Skip to content

Commit be86e42

Browse files
committed
Add 'go_naming_convention' support.
Add directive 'go_naming_convention' to control the name of generated Go rules. go_default_library: Legacy behaviour. import: Name targets after their import path. import_alias: Same as import, but generate alias targets to maintain backwards compatibility with go_default_library. We also add a `build_naming_convention` attribute to `go_repository` rules, allowing per-external control over the naming convention. gazelle fix is augmented to migrate between the different styles, both forwards and backwards. FIXES=bazel-contrib#5
1 parent be9c2a8 commit be86e42

37 files changed

+1362
-38
lines changed

README.rst

+17
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,10 @@ The following flags are accepted:
336336
| See `Predefined plugins`_ for available options; commonly used options include |
337337
| ``@io_bazel_rules_go//proto:gofast_grpc`` and ``@io_bazel_rules_go//proto:gogofaster_grpc``. |
338338
+--------------------------------------------------------------+----------------------------------------+
339+
| :flag:`-go_naming_convention` | |
340+
+--------------------------------------------------------------+----------------------------------------+
341+
| Command line flag for the ``go_naming_convention`` directive. |
342+
+--------------------------------------------------------------+----------------------------------------+
339343
| :flag:`-go_prefix example.com/repo` | |
340344
+--------------------------------------------------------------+----------------------------------------+
341345
| A prefix of import paths for libraries in the repository that corresponds to |
@@ -575,6 +579,19 @@ The following directives are recognized:
575579
| ``@io_bazel_rules_go//proto:gofast_grpc`` and |
576580
| ``@io_bazel_rules_go//proto:gogofaster_grpc``. |
577581
+---------------------------------------------------+----------------------------------------+
582+
| :direc:`# gazelle:go_naming_convention` | n/a |
583+
+---------------------------------------------------+----------------------------------------+
584+
| Controls the names of generated Go targets. Valid values are: |
585+
| ``go_default_library``: Library targets are named ``go_default_library``, test targets |
586+
| are named ``go_default_test``. |
587+
| ``import``: Library and test targets are named after the last segment of their import path.|
588+
| Eg. ``example.repo/foo`` is named ``foo``, and the test target is ``foo_test``. |
589+
| Major version suffixes (eg. ``v1``) are dropped. |
590+
| For a main package with a binary ``foobin``, the names are instead ``foobin_lib`` and |
591+
| ``foobin_test``. |
592+
| ``import_alias``: Same as ``import``, but an ``alias`` target is generated named |
593+
| ``go_default_library`` to ensure backwards compatibility. |
594+
+---------------------------------------------------+----------------------------------------+
578595
| :direc:`# gazelle:go_proto_compilers` | ``@io_bazel_rules_go//proto:go_proto`` |
579596
+---------------------------------------------------+----------------------------------------+
580597
| The protocol buffers compiler(s) to use for building go bindings. |

cmd/gazelle/fix-update.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,19 @@ func (ucr *updateConfigurer) CheckFlags(fs *flag.FlagSet, c *config.Config) erro
147147
GoPrefix: imp,
148148
})
149149
}
150+
goRepos := make(map[string]repo.Repo)
150151
for _, r := range c.Repos {
151152
if r.Kind() == "go_repository" {
152-
uc.repos = append(uc.repos, repo.Repo{
153-
Name: r.Name(),
154-
GoPrefix: r.AttrString("importpath"),
155-
})
153+
gr := repo.Repo{
154+
Name: r.Name(),
155+
GoPrefix: r.AttrString("importpath"),
156+
BuildNamingConvention: r.AttrString("build_naming_convention"),
157+
}
158+
uc.repos = append(uc.repos, gr)
159+
goRepos[r.Name()] = gr
156160
}
157161
}
162+
c.GoRepos = goRepos
158163

159164
// If the repo configuration file is not WORKSPACE, also load WORKSPACE
160165
// and any declared macro files so we can apply fixes.

config/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ go_library(
1010
visibility = ["//visibility:public"],
1111
deps = [
1212
"//internal/wspace:go_default_library",
13+
"//repo:go_default_library",
1314
"//rule:go_default_library",
1415
],
1516
)

config/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"strings"
3535

3636
"github.com/bazelbuild/bazel-gazelle/internal/wspace"
37+
"github.com/bazelbuild/bazel-gazelle/repo"
3738
"github.com/bazelbuild/bazel-gazelle/rule"
3839
)
3940

@@ -85,6 +86,9 @@ type Config struct {
8586
// generation and dependency resolution.
8687
Repos []*rule.Rule
8788

89+
// go_repository rules, mapped by name.
90+
GoRepos map[string]repo.Repo
91+
8892
// Langs is a list of language names which Gazelle should process.
8993
// An empty list means "all languages".
9094
Langs []string

internal/go_repository.bzl

+8
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ go_repository = repository_rule(
191191
attrs = {
192192
# Fundamental attributes of a go repository
193193
"importpath": attr.string(mandatory = True),
194+
"build_naming_convention": attr.string(
195+
values = [
196+
"",
197+
"go_default_library",
198+
"import",
199+
"import_alias",
200+
],
201+
),
194202

195203
# Attributes for a repository that should be checked out from VCS
196204
"commit": attr.string(),

language/go/config.go

+74-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ type goConfig struct {
7777
// goGenerateProto indicates whether to generate go_proto_library
7878
goGenerateProto bool
7979

80+
// goNamingConvention controls the name of generated targets
81+
goNamingConvention namingConvention
82+
8083
// goProtoCompilers is the protocol buffers compiler(s) to use for go code.
8184
goProtoCompilers []string
8285

@@ -171,6 +174,15 @@ func (gc *goConfig) setBuildTags(tags string) error {
171174
return nil
172175
}
173176

177+
func (gc *goConfig) setNamingConvention(s string) error {
178+
if nc, ok := namingConventionFromString(s); ok {
179+
gc.goNamingConvention = nc
180+
return nil
181+
} else {
182+
return fmt.Errorf("unknown go_naming_convention %q", s)
183+
}
184+
}
185+
174186
func getProtoMode(c *config.Config) proto.Mode {
175187
if gc := getGoConfig(c); !gc.goGenerateProto {
176188
return proto.DisableMode
@@ -236,6 +248,59 @@ func (f tagsFlag) String() string {
236248
return ""
237249
}
238250

251+
type goNamingConventionFlag func(string) error
252+
253+
func (f goNamingConventionFlag) Set(value string) error {
254+
return f(value)
255+
}
256+
257+
func (f goNamingConventionFlag) String() string {
258+
return ""
259+
}
260+
261+
// namingConvention determines how go targets are named.
262+
type namingConvention int
263+
264+
const (
265+
// 'go_default_library', 'go_default_test', and TODO(tomlu)
266+
goDefaultLibraryNC = iota
267+
268+
// For an import path that ends with foo, the go_library rules target is
269+
// named 'foo', the go_test is named 'foo_test'.
270+
// For a main package, the go_binary takes the 'foo' name, the library
271+
// is named 'foo_lib', and the go_test is named 'foo_test'.
272+
importNC
273+
274+
// Same as importNC, but generate alias rules for libraries that have
275+
// the legacy 'go_default_library' name.
276+
importAliasNC
277+
)
278+
279+
func (nc namingConvention) String() string {
280+
switch nc {
281+
case goDefaultLibraryNC:
282+
return "go_default_library"
283+
case importNC:
284+
return "import"
285+
case importAliasNC:
286+
return "import_alias"
287+
}
288+
return ""
289+
}
290+
291+
func namingConventionFromString(s string) (namingConvention, bool) {
292+
switch s {
293+
case "go_default_library":
294+
return goDefaultLibraryNC, true
295+
case "import":
296+
return importNC, true
297+
case "import_alias":
298+
return importAliasNC, true
299+
default:
300+
return goDefaultLibraryNC, false
301+
}
302+
}
303+
239304
type moduleRepo struct {
240305
repoName, modulePath string
241306
}
@@ -249,6 +314,7 @@ func (*goLang) KnownDirectives() []string {
249314
"build_tags",
250315
"go_generate_proto",
251316
"go_grpc_compilers",
317+
"go_naming_convention",
252318
"go_proto_compilers",
253319
"go_visibility",
254320
"importmap_prefix",
@@ -290,6 +356,10 @@ func (*goLang) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
290356
"go_repository_module_mode",
291357
false,
292358
"set when gazelle is invoked by go_repository in module mode")
359+
fs.Var(
360+
goNamingConventionFlag(gc.setNamingConvention),
361+
"go_naming_convention",
362+
"controls generated library names. One of (go_default_library, import, import_alias)")
293363

294364
case "update-repos":
295365
fs.Var(&gzflag.AllowedStringFlag{Value: &gc.buildExternalAttr, Allowed: validBuildExternalAttr},
@@ -408,12 +478,15 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.`
408478
gc.preprocessTags()
409479
gc.setBuildTags(d.Value)
410480
case "go_generate_proto":
411-
412481
if goGenerateProto, err := strconv.ParseBool(d.Value); err == nil {
413482
gc.goGenerateProto = goGenerateProto
414483
} else {
415484
log.Printf("parsing go_generate_proto: %v", err)
416485
}
486+
case "go_naming_convention":
487+
if err := gc.setNamingConvention(d.Value); err != nil {
488+
log.Print(err)
489+
}
417490
case "go_grpc_compilers":
418491
// Special syntax (empty value) to reset directive.
419492
if d.Value == "" {

language/go/config_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ func TestCommandLine(t *testing.T) {
6464
t,
6565
"-build_tags=foo,bar",
6666
"-go_prefix=example.com/repo",
67+
"-go_naming_convention=import_alias",
6768
"-external=vendored",
6869
"-repo_root=.")
6970
gc := getGoConfig(c)
@@ -78,6 +79,9 @@ func TestCommandLine(t *testing.T) {
7879
if gc.depMode != vendorMode {
7980
t.Errorf("got dep mode %v; want %v", gc.depMode, vendorMode)
8081
}
82+
if gc.goNamingConvention != importAliasNC {
83+
t.Errorf("got naming convention %v; want %v", gc.goNamingConvention, importAliasNC)
84+
}
8185
}
8286

8387
func TestDirectives(t *testing.T) {

0 commit comments

Comments
 (0)