From da0ad1847d05e62366a83bb51d965da413687858 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 28 Oct 2024 10:43:05 +0100 Subject: [PATCH] go_deps: Support default naming convention override for Bazel modules (#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** --- internal/bzlmod/default_gazelle_overrides.bzl | 3 +++ language/go/config.go | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/bzlmod/default_gazelle_overrides.bzl b/internal/bzlmod/default_gazelle_overrides.bzl index 2283f5d33..3492a4f31 100644 --- a/internal/bzlmod/default_gazelle_overrides.bzl +++ b/internal/bzlmod/default_gazelle_overrides.bzl @@ -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", ], diff --git a/language/go/config.go b/language/go/config.go index e23ed5437..d0a4007f6 100644 --- a/language/go/config.go +++ b/language/go/config.go @@ -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 } } }