Skip to content

Commit 8792ded

Browse files
Scott Moellerclaude
andcommitted
feat(gazelle): respect gazelle:resolve buf directives for cross resolution
Addresses #117. Add support for `gazelle:resolve buf` directives in the buf gazelle plugin's `CrossResolve` function. This allows users to override the default `@buf_deps//` resolution for proto imports, enabling proper integration with `local_path_override` modules in bzlmod monorepos. The `CrossResolve` function now first checks for `gazelle:resolve buf` directives using the standard `resolve.FindRuleWithOverride` function. If a matching directive is found, return that label. Otherwise, fall back to the default `@buf_deps//` resolution. Example Usage ```python This directive goes in a BUILD file (typically root or parent directory) and tells gazelle to resolve imports of proto/foo/bar.proto to the label @com_example//proto/foo:bar_proto instead of the default @buf_deps//... resolution. ``` Test Coverage ``` bazel test //... ``` Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent a3128f1 commit 8792ded

15 files changed

Lines changed: 111 additions & 2 deletions

File tree

gazelle/buf/buf_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ func TestCrossResolve(t *testing.T) {
6565
testRunGazelle(t, "v2/cross_resolve")
6666
}
6767

68+
func TestCrossResolveOverride(t *testing.T) {
69+
t.Parallel()
70+
testRunGazelle(t, "cross_resolve_override")
71+
}
72+
6873
func TestMerge(t *testing.T) {
6974
t.Parallel()
7075
testRunGazelle(t, "merge")

gazelle/buf/cross_resolve.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,21 @@ func (*bufLang) CrossResolve(gazelleConfig *config.Config, ruleIndex *resolve.Ru
3333
if langWithDep != "proto" || importSpec.Lang != "proto" {
3434
return nil
3535
}
36-
config := GetConfigForGazelleConfig(gazelleConfig)
37-
depRepo := getRepoNameForPath(config.BufConfigFile.Pkg)
36+
37+
// First, check for gazelle:resolve buf directives
38+
// This allows users to override the default buf_deps resolution
39+
// Example: # gazelle:resolve buf proto/foo/bar.proto @com_example//proto/foo:bar_proto
40+
bufImportSpec := resolve.ImportSpec{
41+
Lang: "buf",
42+
Imp: importSpec.Imp,
43+
}
44+
if override, ok := resolve.FindRuleWithOverride(gazelleConfig, bufImportSpec, "buf"); ok {
45+
return []resolve.FindResult{{Label: override}}
46+
}
47+
48+
// Fall back to default buf_deps resolution
49+
bufConfig := GetConfigForGazelleConfig(gazelleConfig)
50+
depRepo := getRepoNameForPath(bufConfig.BufConfigFile.Pkg)
3851
return []resolve.FindResult{
3952
{
4053
Label: label.New(depRepo, path.Dir(importSpec.Imp), proto.RuleName(path.Dir(importSpec.Imp))),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version: v1
2+
directories:
3+
- fooapis
4+
- petapis
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# gazelle:buf_breaking_against //:against_file
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("@rules_buf//buf:defs.bzl", "buf_breaking_test")
2+
3+
# gazelle:buf_breaking_against //:against_file
4+
5+
buf_breaking_test(
6+
name = "buf_breaking",
7+
against = "//:against_file",
8+
config = "//fooapis:buf.yaml",
9+
targets = ["//fooapis/foo/v1:foo_v1_proto"],
10+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: v1
2+
breaking:
3+
use:
4+
- FILE
5+
lint:
6+
use:
7+
- DEFAULT

gazelle/buf/testdata/cross_resolve_override/fooapis/foo/v1/BUILD.in

Whitespace-only changes.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
load("@rules_proto//proto:defs.bzl", "proto_library")
2+
load("@rules_buf//buf:defs.bzl", "buf_lint_test")
3+
4+
proto_library(
5+
name = "foo_v1_proto",
6+
srcs = ["foo.proto"],
7+
strip_import_prefix = "/fooapis",
8+
visibility = ["//visibility:public"],
9+
)
10+
11+
buf_lint_test(
12+
name = "foo_v1_proto_lint",
13+
config = "//fooapis:buf.yaml",
14+
targets = [":foo_v1_proto"],
15+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
syntax = "proto3";
2+
3+
package foo.v1;
4+
5+
message Foo {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# gazelle:buf_breaking_against //:against_file
2+
# gazelle:resolve buf validate/validate.proto @custom_validate//validate:validate_proto

0 commit comments

Comments
 (0)