Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions gazelle/buf/buf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ func TestCrossResolve(t *testing.T) {
testRunGazelle(t, "v2/cross_resolve")
}

func TestCrossResolveOverride(t *testing.T) {
t.Parallel()
testRunGazelle(t, "cross_resolve_override")
}

func TestMerge(t *testing.T) {
t.Parallel()
testRunGazelle(t, "merge")
Expand Down
27 changes: 23 additions & 4 deletions gazelle/buf/cross_resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package buf

import (
"path"
"strings"

"github.com/bazelbuild/bazel-gazelle/config"
"github.com/bazelbuild/bazel-gazelle/label"
"github.com/bazelbuild/bazel-gazelle/language/proto"
"github.com/bazelbuild/bazel-gazelle/resolve"
)

Expand All @@ -33,11 +33,30 @@ func (*bufLang) CrossResolve(gazelleConfig *config.Config, ruleIndex *resolve.Ru
if langWithDep != "proto" || importSpec.Lang != "proto" {
return nil
}
config := GetConfigForGazelleConfig(gazelleConfig)
depRepo := getRepoNameForPath(config.BufConfigFile.Pkg)

// First, check for gazelle:resolve buf directives
// This allows users to override the default buf_deps resolution
// Example: # gazelle:resolve buf proto/foo/bar.proto @com_example//proto/foo:bar_proto
bufImportSpec := resolve.ImportSpec{
Lang: "buf",
Imp: importSpec.Imp,
}
if override, ok := resolve.FindRuleWithOverride(gazelleConfig, bufImportSpec, "buf"); ok {
return []resolve.FindResult{{Label: override}}
}

// Fall back to default buf_deps resolution
bufConfig := GetConfigForGazelleConfig(gazelleConfig)
depRepo := getRepoNameForPath(bufConfig.BufConfigFile.Pkg)

// Generate target name from the proto file name, not the directory name
// e.g., "nmts/v1/proto/ek/logical/interface.proto" -> "interface_proto"
protoFile := path.Base(importSpec.Imp)
targetName := strings.TrimSuffix(protoFile, ".proto") + "_proto"

return []resolve.FindResult{
{
Label: label.New(depRepo, path.Dir(importSpec.Imp), proto.RuleName(path.Dir(importSpec.Imp))),
Label: label.New(depRepo, path.Dir(importSpec.Imp), targetName),
},
}
}
4 changes: 4 additions & 0 deletions gazelle/buf/testdata/cross_resolve_override/buf.work.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: v1
directories:
- fooapis
- petapis
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# gazelle:buf_breaking_against //:against_file
10 changes: 10 additions & 0 deletions gazelle/buf/testdata/cross_resolve_override/fooapis/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
load("@rules_buf//buf:defs.bzl", "buf_breaking_test")

# gazelle:buf_breaking_against //:against_file

buf_breaking_test(
name = "buf_breaking",
against = "//:against_file",
config = "//fooapis:buf.yaml",
targets = ["//fooapis/foo/v1:foo_v1_proto"],
)
7 changes: 7 additions & 0 deletions gazelle/buf/testdata/cross_resolve_override/fooapis/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: v1
breaking:
use:
- FILE
lint:
use:
- DEFAULT
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_buf//buf:defs.bzl", "buf_lint_test")

proto_library(
name = "foo_v1_proto",
srcs = ["foo.proto"],
strip_import_prefix = "/fooapis",
visibility = ["//visibility:public"],
)

buf_lint_test(
name = "foo_v1_proto_lint",
config = "//fooapis:buf.yaml",
targets = [":foo_v1_proto"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

package foo.v1;

message Foo {}
2 changes: 2 additions & 0 deletions gazelle/buf/testdata/cross_resolve_override/petapis/BUILD.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gazelle:buf_breaking_against //:against_file
# gazelle:resolve buf validate/validate.proto @custom_validate//validate:validate_proto
11 changes: 11 additions & 0 deletions gazelle/buf/testdata/cross_resolve_override/petapis/BUILD.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_buf//buf:defs.bzl", "buf_breaking_test")

# gazelle:buf_breaking_against //:against_file
# gazelle:resolve buf validate/validate.proto @custom_validate//validate:validate_proto

buf_breaking_test(
name = "buf_breaking",
against = "//:against_file",
config = "//petapis:buf.yaml",
targets = ["//petapis/pets/v1:pets_v1_proto"],
)
7 changes: 7 additions & 0 deletions gazelle/buf/testdata/cross_resolve_override/petapis/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: v1
breaking:
use:
- FILE
lint:
use:
- DEFAULT
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_buf//buf:defs.bzl", "buf_lint_test")

proto_library(
name = "pets_v1_proto",
srcs = ["pets.proto"],
strip_import_prefix = "/petapis",
visibility = ["//visibility:public"],
deps = [
"//fooapis/foo/v1:foo_v1_proto",
"@custom_validate//validate:validate_proto",
],
)

buf_lint_test(
name = "pets_v1_proto_lint",
config = "//petapis:buf.yaml",
targets = [":pets_v1_proto"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";

package pets.v1;

import "foo/v1/foo.proto";
import "validate/validate.proto";

message Pet {
foo.v1.Foo foo = 1;
}