@@ -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 , ok := namingConventionFromString (s ); ok {
183
+ gc .goNamingConvention = nc
184
+ return nil
185
+ } else {
186
+ return fmt .Errorf ("unknown go_naming_convention %q" , s )
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 goNamingConventionFlag func (string ) error
256
+
257
+ func (f goNamingConventionFlag ) Set (value string ) error {
258
+ return f (value )
259
+ }
260
+
261
+ func (f goNamingConventionFlag ) 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', 'go_default_test', and TODO(tomlu)
270
+ goDefaultLibraryNC = 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
+ importNC
277
+
278
+ // Same as importNC, but generate alias rules for libraries that have
279
+ // the legacy 'go_default_library' name.
280
+ importAliasNC
281
+ )
282
+
283
+ func (nc namingConvention ) String () string {
284
+ switch nc {
285
+ case goDefaultLibraryNC :
286
+ return "go_default_library"
287
+ case importNC :
288
+ return "import"
289
+ case importAliasNC :
290
+ return "import_alias"
291
+ }
292
+ return ""
293
+ }
294
+
295
+ func namingConventionFromString (s string ) (namingConvention , bool ) {
296
+ switch s {
297
+ case "go_default_library" :
298
+ return goDefaultLibraryNC , true
299
+ case "import" :
300
+ return importNC , true
301
+ case "import_alias" :
302
+ return importAliasNC , true
303
+ default :
304
+ return goDefaultLibraryNC , false
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
+ goNamingConventionFlag (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,15 @@ 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 , ok := namingConventionFromString (repo .AttrString ("build_naming_convention" )); ok {
453
+ repoNamingConvention [repo .Name ()] = nc
454
+ }
455
+ }
456
+ }
457
+ gc .repoNamingConvention = repoNamingConvention
375
458
}
376
459
377
460
if ! gc .moduleMode {
@@ -408,12 +491,15 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.`
408
491
gc .preprocessTags ()
409
492
gc .setBuildTags (d .Value )
410
493
case "go_generate_proto" :
411
-
412
494
if goGenerateProto , err := strconv .ParseBool (d .Value ); err == nil {
413
495
gc .goGenerateProto = goGenerateProto
414
496
} else {
415
497
log .Printf ("parsing go_generate_proto: %v" , err )
416
498
}
499
+ case "go_naming_convention" :
500
+ if err := gc .setNamingConvention (d .Value ); err != nil {
501
+ log .Print (err )
502
+ }
417
503
case "go_grpc_compilers" :
418
504
// Special syntax (empty value) to reset directive.
419
505
if d .Value == "" {
0 commit comments