Skip to content

Commit 056a873

Browse files
author
jchadwick-buf
authored
Add support for bzlmod + Bazel 8 (#289)
This is a full-rewrite of the `WORKSPACE.bazel` rules + a bzlmod `MODULE.bazel` implementation. For both Bazel 7.x and Bazel 8.x, all of the following combinations will work: ``` # Legacy WORKSPACE mode bazel build --noenable_bzlmod --enable_workspace //... && bazel test --noenable_bzlmod --enable_workspace //... # Bzlmod-only mode bazel build --enable_bzlmod --noenable_workspace //... && bazel test --enable_bzlmod --noenable_workspace //... # Hybrid incremental migration mode bazel build --enable_bzlmod --enable_workspace //... && bazel test --enable_bzlmod --enable_workspace //... ``` Some modules needed to be updated for Bazel 8.x compatibility. Also, some `WORKSPACE` code need to be retained for Bazel 7.x compatibility that could otherwise be omitted. Gazelle is set up to function in bzlmod mode and can be invoked using `bazel mod tidy`. I don't think we should need to substantially change CI except to test all of the possible configuration modes to ensure they do not break on accident.
1 parent cfee84e commit 056a873

File tree

15 files changed

+938
-84
lines changed

15 files changed

+938
-84
lines changed

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.4.1
1+
8.0.0

.github/workflows/bazel.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Bazel CI
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
workflow_dispatch: {} # support manual runs
8+
permissions:
9+
contents: read
10+
pull-requests: write
11+
jobs:
12+
ci:
13+
name: CI
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 2
20+
- name: Install bazelisk
21+
uses: bazelbuild/setup-bazelisk@v3
22+
- name: Mount bazel cache
23+
uses: actions/cache@v4
24+
with:
25+
path: "~/.cache/bazel"
26+
key: bazel
27+
- name: Test Bazel targets with legacy WORKSPACE mode
28+
run: bazel build --noenable_bzlmod --enable_workspace //... && bazel test --noenable_bzlmod --enable_workspace //...
29+
- name: Test Bazel targets with bzlmod-only mode
30+
run: bazel build --enable_bzlmod --noenable_workspace //... && bazel test --enable_bzlmod --noenable_workspace //...
31+
- name: Test Bazel targets with hybrid incremental-migration mode
32+
run: bazel build --enable_bzlmod --enable_workspace //... && bazel test --enable_bzlmod --enable_workspace //...

MODULE.bazel

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,28 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
###############################################################################
16-
# Bazel now uses Bzlmod by default to manage external dependencies.
17-
# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel.
18-
#
19-
# For more details, please check https://github.com/bazelbuild/bazel/issues/18958
20-
###############################################################################
15+
module(
16+
name = "protovalidate",
17+
version = "0.10.0",
18+
repo_name = "com_github_bufbuild_protovalidate",
19+
)
20+
21+
bazel_dep(name = "rules_go", version = "0.51.0", repo_name = "io_bazel_rules_go")
22+
bazel_dep(name = "gazelle", version = "0.40.0", repo_name = "bazel_gazelle")
23+
bazel_dep(name = "rules_buf", version = "0.3.0")
24+
bazel_dep(name = "protobuf", version = "29.2", repo_name = "com_google_protobuf")
25+
bazel_dep(name = "rules_proto", version = "7.1.0")
26+
bazel_dep(name = "googleapis", version = "0.0.0-20240819-fe8ba054a")
27+
28+
go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")
29+
go_sdk.download(version = "1.20.4")
30+
31+
go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
32+
go_deps.from_file(go_mod = "//tools:go.mod")
33+
use_repo(go_deps, "com_github_bufbuild_protocompile", "com_github_spf13_pflag", "com_github_stretchr_testify", "in_gopkg_yaml_v3", "org_golang_google_protobuf", "org_golang_x_sync")
34+
35+
buf = use_extension("@rules_buf//buf:extensions.bzl", "buf")
36+
buf.toolchains(
37+
sha256 = "bb9d7b4a1fc9ddebc285faa09b9c3e4ce176b94585a1d7988371d73b208aa0f5",
38+
version = "v1.35.0",
39+
)

MODULE.bazel.lock

Lines changed: 789 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WORKSPACE

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,67 @@ workspace(name = "com_github_bufbuild_protovalidate")
22

33
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
44

5-
# This is needed due to an unresolved issue with protobuf v27+.
6-
# https://github.com/protocolbuffers/protobuf/issues/17200
5+
http_archive(
6+
name = "rules_java",
7+
integrity = "sha256-XCFXV7mmw91TEqPNxIls7z8MWzHbMbqo2g2YhoXUKuQ=",
8+
url = "https://github.com/bazelbuild/rules_java/releases/download/8.5.0/rules_java-8.5.0.tar.gz",
9+
)
10+
711
http_archive(
812
name = "rules_python",
9-
sha256 = "0a8003b044294d7840ac7d9d73eef05d6ceb682d7516781a4ec62eeb34702578",
10-
strip_prefix = "rules_python-0.24.0",
11-
url = "https://github.com/bazelbuild/rules_python/releases/download/0.24.0/rules_python-0.24.0.tar.gz",
13+
sha256 = "4f7e2aa1eb9aa722d96498f5ef514f426c1f55161c3c9ae628c857a7128ceb07",
14+
strip_prefix = "rules_python-1.0.0",
15+
url = "https://github.com/bazelbuild/rules_python/releases/download/1.0.0/rules_python-1.0.0.tar.gz",
1216
)
1317

14-
# Use a newer protobuf toolchain for editions support.
1518
http_archive(
16-
name = "com_google_protobuf",
17-
sha256 = "e4ff2aeb767da6f4f52485c2e72468960ddfe5262483879ef6ad552e52757a77",
18-
strip_prefix = "protobuf-27.2",
19-
urls = ["https://github.com/protocolbuffers/protobuf/archive/v27.2.tar.gz"],
19+
name = "bazel_skylib",
20+
sha256 = "bc283cdfcd526a52c3201279cda4bc298652efa898b10b4db0837dc51652756f",
21+
urls = [
22+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
23+
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.7.1/bazel-skylib-1.7.1.tar.gz",
24+
],
2025
)
2126

22-
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
27+
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
2328

24-
protobuf_deps()
29+
bazel_skylib_workspace()
30+
31+
# rules_go v0.51.0 + gazelle v0.37.0
32+
http_archive(
33+
name = "io_bazel_rules_go",
34+
integrity = "sha256-CTbJvDxDIe43LLj2bdly02jLlA7QGpup/X3rzwCT8Js=",
35+
urls = [
36+
"https://mirror.bazel.build/github.com/bazel-contrib/rules_go/releases/download/v0.51.0/rules_go-v0.51.0.zip",
37+
"https://github.com/bazel-contrib/rules_go/releases/download/v0.51.0/rules_go-v0.51.0.zip",
38+
],
39+
)
40+
41+
http_archive(
42+
name = "bazel_gazelle",
43+
integrity = "sha256-12v3pg/YsFBEQJDfooN6Tq+YKeEWVhjuNdzspcvfWNU=",
44+
urls = [
45+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
46+
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.37.0/bazel-gazelle-v0.37.0.tar.gz",
47+
],
48+
)
49+
50+
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
51+
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")
52+
53+
go_rules_dependencies()
54+
55+
go_register_toolchains(version = "1.20.4")
56+
57+
gazelle_dependencies()
2558

59+
# rules_buf v0.3.0
2660
http_archive(
2761
name = "rules_buf",
28-
sha256 = "523a4e06f0746661e092d083757263a249fedca535bd6dd819a8c50de074731a",
29-
strip_prefix = "rules_buf-0.1.1",
62+
integrity = "sha256-Hr64Q/CaYr0E3ptAjEOgdZd1yc+cBjp7OG1wzuf3DIs=",
63+
strip_prefix = "rules_buf-0.3.0",
3064
urls = [
31-
"https://github.com/bufbuild/rules_buf/archive/refs/tags/v0.1.1.zip",
65+
"https://github.com/bufbuild/rules_buf/archive/refs/tags/v0.3.0.zip",
3266
],
3367
)
3468

@@ -38,41 +72,51 @@ rules_buf_dependencies()
3872

3973
rules_buf_toolchains(version = "v1.35.0")
4074

41-
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
42-
43-
rules_proto_dependencies()
44-
45-
rules_proto_toolchains()
75+
load("@rules_buf//gazelle/buf:repositories.bzl", "gazelle_buf_dependencies")
4676

47-
# Gazelle Setup
77+
gazelle_buf_dependencies()
4878

79+
# protobuf v29.2
4980
http_archive(
50-
name = "io_bazel_rules_go",
51-
sha256 = "6b65cb7917b4d1709f9410ffe00ecf3e160edf674b78c54a894471320862184f",
81+
name = "com_google_protobuf",
82+
integrity = "sha256-YxUKuiP3qQ/X2HvfUU5FndX+cCP93gG1asUzNd9k1L0=",
83+
strip_prefix = "protobuf-29.2",
5284
urls = [
53-
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.0/rules_go-v0.39.0.zip",
54-
"https://github.com/bazelbuild/rules_go/releases/download/v0.39.0/rules_go-v0.39.0.zip",
85+
"https://github.com/protocolbuffers/protobuf/releases/download/v29.2/protobuf-29.2.tar.gz",
5586
],
5687
)
5788

89+
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
90+
91+
protobuf_deps()
92+
93+
load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")
94+
95+
rules_proto_dependencies()
96+
97+
rules_proto_toolchains()
98+
99+
# googleapis
58100
http_archive(
59-
name = "bazel_gazelle",
60-
sha256 = "ecba0f04f96b4960a5b250c8e8eeec42281035970aa8852dda73098274d14a1d",
101+
name = "googleapis",
102+
strip_prefix = "googleapis-fe8ba054ad4f7eca946c2d14a63c3f07c0b586a0",
61103
urls = [
62-
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz",
63-
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.29.0/bazel-gazelle-v0.29.0.tar.gz",
104+
"https://github.com/googleapis/googleapis/archive/fe8ba054ad4f7eca946c2d14a63c3f07c0b586a0.zip",
64105
],
65106
)
66107

67-
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
68-
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
108+
load("@googleapis//:repository_rules.bzl", "switched_rules_by_language")
69109

70-
go_rules_dependencies()
110+
switched_rules_by_language(
111+
name = "com_google_googleapis_imports",
112+
)
71113

72-
go_register_toolchains(version = "1.20.4")
114+
# Dependencies that need to be loaded later
73115

74-
gazelle_dependencies()
116+
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
75117

76-
load("@rules_buf//gazelle/buf:repositories.bzl", "gazelle_buf_dependencies")
118+
rules_java_dependencies()
77119

78-
gazelle_buf_dependencies()
120+
load("@rules_python//python:repositories.bzl", "py_repositories")
121+
122+
py_repositories()

WORKSPACE.bzlmod

Whitespace-only changes.

examples/BUILD.bazel

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@rules_proto//proto:defs.bzl", "proto_library")
1615
load("@rules_buf//buf:defs.bzl", "buf_lint_test")
16+
load("@rules_proto//proto:defs.bzl", "proto_library")
17+
18+
# gazelle:resolve proto proto google/type/postal_address.proto @googleapis//google/type:postal_address_proto
1719

1820
proto_library(
1921
name = "examples_proto",
@@ -107,7 +109,7 @@ proto_library(
107109
"@com_google_protobuf//:struct_proto",
108110
"@com_google_protobuf//:timestamp_proto",
109111
"@com_google_protobuf//:wrappers_proto",
110-
"@go_googleapis//google/type:postaladdress_proto",
112+
"@googleapis//google/type:postal_address_proto",
111113
],
112114
)
113115

proto/protovalidate-testing/buf/validate/conformance/cases/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@rules_proto//proto:defs.bzl", "proto_library")
1615
load("@rules_buf//buf:defs.bzl", "buf_lint_test")
16+
load("@rules_proto//proto:defs.bzl", "proto_library")
1717

1818
proto_library(
1919
name = "buf_validate_conformance_cases_proto",

proto/protovalidate-testing/buf/validate/conformance/cases/custom_constraints/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@rules_proto//proto:defs.bzl", "proto_library")
1615
load("@rules_buf//buf:defs.bzl", "buf_lint_test")
16+
load("@rules_proto//proto:defs.bzl", "proto_library")
1717

1818
proto_library(
1919
name = "buf_validate_conformance_cases_custom_constraints_proto",

proto/protovalidate-testing/buf/validate/conformance/cases/other_package/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
load("@rules_proto//proto:defs.bzl", "proto_library")
1615
load("@rules_buf//buf:defs.bzl", "buf_lint_test")
16+
load("@rules_proto//proto:defs.bzl", "proto_library")
1717

1818
proto_library(
1919
name = "buf_validate_conformance_cases_other_package_proto",

0 commit comments

Comments
 (0)