@@ -77,6 +77,9 @@ type goConfig struct {
77
77
// goGenerateProto indicates whether to generate go_proto_library
78
78
goGenerateProto bool
79
79
80
+ // goNamingConvention controls the name of generated targets
81
+ goNamingConvention namingConvention
82
+
80
83
// goProtoCompilers is the protocol buffers compiler(s) to use for go code.
81
84
goProtoCompilers []string
82
85
@@ -104,6 +107,10 @@ type goConfig struct {
104
107
// resolved differently (also depending on goRepositoryMode).
105
108
moduleMode bool
106
109
110
+ // map between external repo names and their `build_naming_convention`
111
+ // attribute.
112
+ repoNamingConvention map [string ]namingConvention
113
+
107
114
// submodules is a list of modules which have the current module's path
108
115
// as a prefix of their own path. This affects visibility attributes
109
116
// in internal packages.
@@ -171,6 +178,15 @@ func (gc *goConfig) setBuildTags(tags string) error {
171
178
return nil
172
179
}
173
180
181
+ func (gc * goConfig ) setNamingConvention (s string ) error {
182
+ if nc , err := namingConventionFromString (s ); err == nil {
183
+ gc .goNamingConvention = nc
184
+ return nil
185
+ } else {
186
+ return err
187
+ }
188
+ }
189
+
174
190
func getProtoMode (c * config.Config ) proto.Mode {
175
191
if gc := getGoConfig (c ); ! gc .goGenerateProto {
176
192
return proto .DisableMode
@@ -236,6 +252,59 @@ func (f tagsFlag) String() string {
236
252
return ""
237
253
}
238
254
255
+ type namingConventionFlag func (string ) error
256
+
257
+ func (f namingConventionFlag ) Set (value string ) error {
258
+ return f (value )
259
+ }
260
+
261
+ func (f namingConventionFlag ) String () string {
262
+ return ""
263
+ }
264
+
265
+ // namingConvention determines how go targets are named.
266
+ type namingConvention int
267
+
268
+ const (
269
+ // 'go_default_library' and 'go_default_test'
270
+ goDefaultLibraryNamingConvention = iota
271
+
272
+ // For an import path that ends with foo, the go_library rules target is
273
+ // named 'foo', the go_test is named 'foo_test'.
274
+ // For a main package, the go_binary takes the 'foo' name, the library
275
+ // is named 'foo_lib', and the go_test is named 'foo_test'.
276
+ importNamingConvention
277
+
278
+ // Same as importNamingConvention, but generate alias rules for libraries that have
279
+ // the legacy 'go_default_library' name.
280
+ importAliasNamingConvention
281
+ )
282
+
283
+ func (nc namingConvention ) String () string {
284
+ switch nc {
285
+ case goDefaultLibraryNamingConvention :
286
+ return "go_default_library"
287
+ case importNamingConvention :
288
+ return "import"
289
+ case importAliasNamingConvention :
290
+ return "import_alias"
291
+ }
292
+ return ""
293
+ }
294
+
295
+ func namingConventionFromString (s string ) (namingConvention , error ) {
296
+ switch s {
297
+ case "go_default_library" :
298
+ return goDefaultLibraryNamingConvention , nil
299
+ case "import" :
300
+ return importNamingConvention , nil
301
+ case "import_alias" :
302
+ return importAliasNamingConvention , nil
303
+ default :
304
+ return goDefaultLibraryNamingConvention , fmt .Errorf ("unknown naming convention %q" , s )
305
+ }
306
+ }
307
+
239
308
type moduleRepo struct {
240
309
repoName , modulePath string
241
310
}
@@ -249,6 +318,7 @@ func (*goLang) KnownDirectives() []string {
249
318
"build_tags" ,
250
319
"go_generate_proto" ,
251
320
"go_grpc_compilers" ,
321
+ "go_naming_convention" ,
252
322
"go_proto_compilers" ,
253
323
"go_visibility" ,
254
324
"importmap_prefix" ,
@@ -290,6 +360,10 @@ func (*goLang) RegisterFlags(fs *flag.FlagSet, cmd string, c *config.Config) {
290
360
"go_repository_module_mode" ,
291
361
false ,
292
362
"set when gazelle is invoked by go_repository in module mode" )
363
+ fs .Var (
364
+ namingConventionFlag (gc .setNamingConvention ),
365
+ "go_naming_convention" ,
366
+ "controls generated library names. One of (go_default_library, import, import_alias)" )
293
367
294
368
case "update-repos" :
295
369
fs .Var (& gzflag.AllowedStringFlag {Value : & gc .buildExternalAttr , Allowed : validBuildExternalAttr },
@@ -372,6 +446,17 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.`
372
446
log .Printf ("Found RULES_GO_VERSION %s. Minimum compatible version is %s.\n %s" , gc .rulesGoVersion , minimumRulesGoVersion , message )
373
447
}
374
448
}
449
+ repoNamingConvention := map [string ]namingConvention {}
450
+ for _ , repo := range c .Repos {
451
+ if repo .Kind () == "go_repository" {
452
+ if nc , err := namingConventionFromString (repo .AttrString ("build_naming_convention" )); err == nil {
453
+ repoNamingConvention [repo .Name ()] = nc
454
+ } else {
455
+ log .Printf ("%v\n " , err )
456
+ }
457
+ }
458
+ }
459
+ gc .repoNamingConvention = repoNamingConvention
375
460
}
376
461
377
462
if ! gc .moduleMode {
@@ -408,12 +493,15 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.`
408
493
gc .preprocessTags ()
409
494
gc .setBuildTags (d .Value )
410
495
case "go_generate_proto" :
411
-
412
496
if goGenerateProto , err := strconv .ParseBool (d .Value ); err == nil {
413
497
gc .goGenerateProto = goGenerateProto
414
498
} else {
415
499
log .Printf ("parsing go_generate_proto: %v" , err )
416
500
}
501
+ case "go_naming_convention" :
502
+ if err := gc .setNamingConvention (d .Value ); err != nil {
503
+ log .Print (err )
504
+ }
417
505
case "go_grpc_compilers" :
418
506
// Special syntax (empty value) to reset directive.
419
507
if d .Value == "" {
0 commit comments