Skip to content

Commit

Permalink
go_deps: Support default naming convention override for Bazel modules (
Browse files Browse the repository at this point in the history
…#1967)

**What type of PR is this?**

Bug fix

**What package or component does this PR mostly affect?**

go_deps

**What does this PR do? Why is it needed?**

This unblocks using the `build_stackb_rules_proto` module with Gazelle
and default settings, as it doesn't generate `go_default_library`
targets.

Note that a manually applied `gazelle_override` still results in the Go
module being preferred over the Bazel module, so this currently only
works with default overrides.


**Which issues(s) does this PR fix?**

Fixes #

**Other notes for review**
  • Loading branch information
fmeum authored Oct 28, 2024
1 parent 84320ee commit da0ad18
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions internal/bzlmod/default_gazelle_overrides.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ DEFAULT_DIRECTIVES_BY_PATH = {
# in go files are generated by gogo proto. Resolving to the gogo proto target preserves the behavior of Go modules.
"gazelle:resolve go github.com/mwitkow/go-proto-validators @com_github_mwitkow_go_proto_validators//:validators_gogo",
],
"github.com/stackb/rules_proto": [
"gazelle:go_naming_convention import",
],
"google.golang.org/grpc": [
"gazelle:proto disable",
],
Expand Down
12 changes: 9 additions & 3 deletions language/go/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,23 @@ Update io_bazel_rules_go to a newer version in your WORKSPACE file.`
repoNamingConvention := map[string]namingConvention{}
for _, repo := range c.Repos {
if repo.Kind() == "go_repository" {
var name string
if apparentName := c.ModuleToApparentName(repo.AttrString("module_name")); apparentName != "" {
name = apparentName
} else {
name = repo.Name()
}
if attr := repo.AttrString("build_naming_convention"); attr == "" {
// No naming convention specified.
// go_repsitory uses importAliasNamingConvention by default, so we
// could use whichever name.
// resolveExternal should take that as a signal to follow the current
// naming convention to avoid churn.
repoNamingConvention[repo.Name()] = importAliasNamingConvention
repoNamingConvention[name] = importAliasNamingConvention
} else if nc, err := namingConventionFromString(attr); err != nil {
log.Printf("in go_repository named %q: %v", repo.Name(), err)
log.Printf("in go_repository named %q: %v", name, err)
} else {
repoNamingConvention[repo.Name()] = nc
repoNamingConvention[name] = nc
}
}
}
Expand Down

0 comments on commit da0ad18

Please sign in to comment.