Skip to content

Commit 1380c4b

Browse files
committed
Add buf linting workflow with Bazel integration via Azure Pipelines
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
1 parent ee656c7 commit 1380c4b

File tree

91 files changed

+1109
-355
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+1109
-355
lines changed

.bazelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ build:ci --announce_rc
33
# C++17 standard required for newer protobuf and grpc versions
44
build --cxxopt=-std=c++17
55
build --host_cxxopt=-std=c++17
6+
7+
# Enable source_code_info in proto descriptors for buf linting
8+
build --protocopt=--include_source_info

BUILD

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
load("@rules_buf//buf:defs.bzl", "buf_format", "buf_lint_test")
2+
3+
# Buf configuration files
4+
exports_files([
5+
"buf.yaml",
6+
"buf.lock",
7+
])
8+
9+
# Buf lint test for the entire repository
10+
buf_lint_test(
11+
name = "buf_lint_test",
12+
config = ":buf.yaml",
13+
targets = [
14+
"//udpa/annotations:pkg",
15+
"//udpa/data/orca/v1:pkg",
16+
"//udpa/service/orca/v1:pkg",
17+
"//udpa/type/v1:pkg",
18+
"//xds/annotations/v3:pkg",
19+
"//xds/core/v3:pkg",
20+
"//xds/data/orca/v3:pkg",
21+
"//xds/service/orca/v3:pkg",
22+
"//xds/type/v3:pkg",
23+
"//xds/type/matcher/v3:pkg",
24+
],
25+
)
26+
27+
# Buf format rule - run with: bazel run //:buf_format
28+
# This formats all proto files in the workspace
29+
buf_format(
30+
name = "buf_format",
31+
)

DEVELOPER.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,27 @@ Run the following command to update the generated files and commit them with you
88
bazel build //...
99
tools/generate_go_protobuf.py
1010
```
11+
12+
## Buf tools
13+
14+
### Format proto files
15+
16+
To format all proto files in the workspace:
17+
18+
```sh
19+
bazel run //:buf_format
20+
```
21+
22+
To check if proto files are formatted correctly (used in CI):
23+
24+
```sh
25+
bazel run //:buf_format -- -d
26+
```
27+
28+
### Linting
29+
30+
Buf linting is automatically run as part of the test suite:
31+
32+
```sh
33+
bazel test //...
34+
```

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ bazel_dep(name = "grpc", version = "1.68.0", repo_name = "com_github_grpc_grpc")
1111
bazel_dep(name = "protobuf", version = "29.3", repo_name = "com_google_protobuf")
1212
bazel_dep(name = "protoc-gen-validate", version = "1.2.1.bcr.1", repo_name = "com_envoyproxy_protoc_gen_validate")
1313
bazel_dep(name = "re2", version = "2024-07-02", repo_name = "com_googlesource_code_re2")
14+
bazel_dep(name = "rules_buf", version = "0.5.2")
1415
bazel_dep(name = "rules_cc", version = "0.0.17")
1516
bazel_dep(name = "rules_go", version = "0.53.0", repo_name = "io_bazel_rules_go")
1617
bazel_dep(name = "rules_python", version = "1.6.3")

bazel/api_build_system.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ load("@com_github_grpc_grpc//bazel:python_rules.bzl", _py_proto_library = "py_pr
44
load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
55
load("@io_bazel_rules_go//go:def.bzl", "go_test")
66
load("@io_bazel_rules_go//proto:def.bzl", "go_grpc_library", "go_proto_library")
7+
load("@rules_buf//buf:defs.bzl", "buf_lint_test")
78
load("@rules_cc//cc:cc_test.bzl", "cc_test")
89
load(
910
"//bazel:external_proto_deps.bzl",
@@ -101,6 +102,15 @@ def xds_proto_package(
101102
deps = [],
102103
has_services = False,
103104
visibility = ["//visibility:public"]):
105+
"""Builds proto targets and creates tests for linting.
106+
107+
Args:
108+
name: Name of the proto package (default: "pkg")
109+
srcs: List of proto source files
110+
deps: Dependencies on other proto_library targets
111+
has_services: Whether this package contains gRPC services
112+
visibility: Target visibility
113+
"""
104114
if srcs == []:
105115
srcs = native.glob(["*.proto"])
106116

@@ -136,6 +146,13 @@ def xds_proto_package(
136146
]).to_list(),
137147
)
138148

149+
# Add buf linting test for proto files
150+
buf_lint_test(
151+
name = name + "_buf_lint_test",
152+
config = "//:buf.yaml",
153+
targets = [":" + name],
154+
)
155+
139156
def xds_cc_test(name, **kwargs):
140157
cc_test(
141158
name = name,

bazel/dependency_imports.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ load("@com_envoyproxy_protoc_gen_validate//bazel:repositories.bzl", "pgv_depende
33
load("@com_google_googleapis//:repository_rules.bzl", "switched_rules_by_language")
44
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
55
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
6+
load("@rules_buf//buf:repositories.bzl", "rules_buf_dependencies", "rules_buf_toolchains")
67
load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")
78

89
# go version for rules_go
@@ -18,6 +19,10 @@ def xds_dependency_imports(go_version = GO_VERSION):
1819
gazelle_dependencies(go_sdk = "go_sdk")
1920
pgv_dependencies()
2021

22+
# Initialize rules_buf for WORKSPACE mode
23+
rules_buf_dependencies()
24+
rules_buf_toolchains(version = "v1.47.2")
25+
2126
# Initialize rules_python for WORKSPACE mode
2227
py_repositories()
2328
python_register_toolchains(

bazel/repositories.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def xds_api_dependencies():
4646
"rules_cc",
4747
locations = REPOSITORY_LOCATIONS,
4848
)
49+
xds_http_archive(
50+
"rules_buf",
51+
locations = REPOSITORY_LOCATIONS,
52+
)
4953

5054
# Old name for backward compatibility.
5155
# TODO(roth): Remove once all callers are updated to use the new name.

bazel/repository_locations.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ REPOSITORY_LOCATIONS = dict(
5858
strip_prefix = "rules_python-1.6.3",
5959
urls = ["https://github.com/bazelbuild/rules_python/archive/1.6.3.tar.gz"],
6060
),
61+
rules_buf = dict(
62+
sha256 = "19d845cedf32c0e74a01af8d0bd904872bddc7905f087318d00b332aa36d3929",
63+
strip_prefix = "rules_buf-0.5.2",
64+
urls = ["https://github.com/bufbuild/rules_buf/archive/refs/tags/v0.5.2.tar.gz"],
65+
),
6166
)

buf.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Generated by buf. DO NOT EDIT.
2+
version: v2
3+
deps:
4+
- name: buf.build/envoyproxy/protoc-gen-validate
5+
commit: daf171c6cdb54629b5f51e345a79e4dd
6+
digest: b5:c745e1521879f43740230b1df673d0729f55704efefdcfc489d4a0a2d40c92a26cacfeab62813403040a8b180142d53b398c7ca784a065e43823605ee49681de
7+
- name: buf.build/google/cel-spec
8+
commit: 96eff7bcf453468dbd44b80598bfe1bf
9+
digest: b5:660c244ec1e8a0f26c5431e1901f1d52d322c7ee3fd240cb6832f44d0694384a36ded008af457a9e5268c73adfa00170e972ac4fe44752a693424b3479f30b82
10+
- name: buf.build/googleapis/googleapis
11+
commit: 004180b77378443887d3b55cabc00384
12+
digest: b5:e8f475fe3330f31f5fd86ac689093bcd274e19611a09db91f41d637cb9197881ce89882b94d13a58738e53c91c6e4bae7dc1feba85f590164c975a89e25115dc

buf.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
version: v2
2+
modules:
3+
- path: .
4+
excludes:
5+
- bazel-bin
6+
- bazel-out
7+
- bazel-testlogs
8+
- bazel-xds
9+
deps:
10+
- buf.build/envoyproxy/protoc-gen-validate
11+
- buf.build/googleapis/googleapis
12+
- buf.build/google/cel-spec
13+
lint:
14+
use:
15+
- STANDARD
16+
ignore_only:
17+
# ENUM_VALUE_PREFIX: Legacy enums with established names that cannot be changed for compatibility
18+
ENUM_VALUE_PREFIX:
19+
- udpa/annotations/status.proto # PackageVersionStatus enum
20+
- xds/annotations/v3/status.proto # PackageVersionStatus enum
21+
- xds/core/v3/resource_locator.proto # Scheme enum
22+
# ENUM_ZERO_VALUE_SUFFIX: Legacy enum zero values that predate _UNSPECIFIED convention
23+
ENUM_ZERO_VALUE_SUFFIX:
24+
- udpa/annotations/status.proto # PackageVersionStatus.UNKNOWN
25+
- xds/annotations/v3/status.proto # PackageVersionStatus.UNKNOWN
26+
- xds/core/v3/resource_locator.proto # Scheme.XDSTP
27+
# PACKAGE_VERSION_SUFFIX: udpa.annotations package is legacy and predates versioning requirements
28+
PACKAGE_VERSION_SUFFIX:
29+
- udpa/annotations/migrate.proto
30+
- udpa/annotations/security.proto
31+
- udpa/annotations/sensitive.proto
32+
- udpa/annotations/status.proto
33+
- udpa/annotations/versioning.proto
34+
# RPC naming: ORCA service uses established naming convention that predates buf standards
35+
RPC_REQUEST_RESPONSE_UNIQUE:
36+
- udpa/service/orca/v1/orca.proto # StreamCoreMetrics RPC
37+
- xds/service/orca/v3/orca.proto # StreamCoreMetrics RPC
38+
RPC_REQUEST_STANDARD_NAME:
39+
- udpa/service/orca/v1/orca.proto # StreamCoreMetrics RPC
40+
- xds/service/orca/v3/orca.proto # StreamCoreMetrics RPC
41+
RPC_RESPONSE_STANDARD_NAME:
42+
- udpa/service/orca/v1/orca.proto # StreamCoreMetrics RPC
43+
- xds/service/orca/v3/orca.proto # StreamCoreMetrics RPC
44+
breaking:
45+
use:
46+
- FILE

0 commit comments

Comments
 (0)