Skip to content

Commit 7b3b1f0

Browse files
electronjoeclaudesrikrsna-buf
authored
Add support for gazelle:resolve directives (#127)
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 # gazelle:resolve buf proto/foo/bar.proto @com_example//proto/foo:bar_proto ``` 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 //... ``` Additionally, in the repository cited in #117, this patch addresses one (of two) issues uncovered. --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: Sri Krishna <93153132+srikrsna-buf@users.noreply.github.com>
1 parent a3128f1 commit 7b3b1f0

15 files changed

Lines changed: 107 additions & 0 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ func (*bufLang) CrossResolve(gazelleConfig *config.Config, ruleIndex *resolve.Ru
3333
if langWithDep != "proto" || importSpec.Lang != "proto" {
3434
return nil
3535
}
36+
// First, check for gazelle:resolve buf directives
37+
// This allows users to override the default buf_deps resolution
38+
// Example: # gazelle:resolve buf proto/foo/bar.proto @com_example//proto/foo:bar_proto
39+
bufImportSpec := resolve.ImportSpec{
40+
Lang: "buf",
41+
Imp: importSpec.Imp,
42+
}
43+
if override, ok := resolve.FindRuleWithOverride(gazelleConfig, bufImportSpec, "buf"); ok {
44+
return []resolve.FindResult{{Label: override}}
45+
}
46+
// Fall back to default buf_deps resolution
3647
config := GetConfigForGazelleConfig(gazelleConfig)
3748
depRepo := getRepoNameForPath(config.BufConfigFile.Pkg)
3849
return []resolve.FindResult{
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)