Skip to content

Commit 5a8ceac

Browse files
committed
Migrate from WORKSPACE to MODULE.bazel.
This represents a pretty significant amount of work to move away from the old and deprecated `WORKSPACE` way of managing Bazel external dependencies, to using `MODULE.bazel` aka Bzlmod. In the process of doing this I did a few things to get the migration over the line. **Migrated from `rules_docker` to `rules_oci`.** The former is more or less deprecated and there's no Bazel module for it in BCR. There is a module for `rules_oci` though, which seems to be the spiritual successor to `rules_docker`. They do mostly the same things, although in different ways. **Struggled a lot to get the dependencies on the Docker Go libraries working**, and ended up refactoring my old version of `dockertest` in favor of one based on `github.com/ory/dockertest/v3` which I know from professional experience works perfectly fine and doesn't require me to do some of the lower-level Docker stuff myself. **Removed a bunch of the existing Docker-related stuff**, since I couldn't be bothered to fix it and will likely not be using it in the near future anyway. **Removed the `./bazel` script**, and chose to just rely on Bazelisk being installed on the system instead. Probably more things I'm forgetting, but they're probably not that important if I can't think of them right now.
1 parent c005469 commit 5a8ceac

File tree

58 files changed

+2517
-2857
lines changed

Some content is hidden

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

58 files changed

+2517
-2857
lines changed

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.2.0
1+
8.3.1

BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@bazel_gazelle//:def.bzl", "gazelle")
1+
load("@gazelle//:def.bzl", "gazelle")
22

33
# gazelle:exclude _tools
44
# gazelle:exclude external

HOWTO.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ Requirements:
88
* `make`.
99
* `tar`.
1010
* `unzip`.
11+
* `bazel` (ideally `bazelisk` installed as `bazel`).
1112

12-
This repository uses [`bazelisk`](https://github.com/bazelbuild/bazelisk) to install and run Bazel. In other words, you don't need to install it yourself. `bazelisk` is invoked via a script called `bazel` in the root of the repository.
13-
14-
To run Bazel for building and testing
13+
To run Bazel for building and testing:
1514

1615
```shell
1716
# Assuming the root of the repository is the working directory.
18-
$ ./bazel build //...
19-
$ ./bazel test //...
17+
$ bazel build //...
18+
$ bazel test //...
2019
```
2120

2221
## Updating Go dependencies
@@ -28,7 +27,7 @@ $ go get -u -t ./... # updates direct dependencies
2827
$ go get -u -t -tags=tools ./... # updates direct tool dependencies, see tools.go in the root of the repository
2928
$ go mod tidy # clean up go.mod and go.sum
3029
$ make generate # regenerate code
31-
$ make fix # update repositories.bzl, BUILD files, etc
30+
$ make fix # update MODULE.bazel, BUILD files, etc
3231
$ make fix # possibly needed due to non-hermetic code formatters
3332
```
3433

MODULE.bazel

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
module(
2+
name = "code",
3+
version = "0.1.0",
4+
)
5+
6+
# -- bazel_dep definitions -- #
7+
8+
# TODO: mark some of these as dev dependencies? Many of them are only needed for
9+
# e.g. tests and setting up dev tooling
10+
11+
bazel_dep(name = "gazelle", version = "0.45.0")
12+
bazel_dep(name = "rules_cc", version = "0.2.3")
13+
bazel_dep(name = "rules_go", version = "0.57.0")
14+
bazel_dep(name = "toolchains_llvm", version = "1.5.0")
15+
bazel_dep(name = "rules_oci", version = "2.2.6")
16+
17+
# NOTE from the Bzlmod migration:
18+
# For the dependencies below, a higher version was used compared to what was in
19+
# WORKSPACE. The reason is that the WORKSPACE versions were often arbitrary
20+
# commit hashes and I couldn't be arsed to do overrides, and figured that these
21+
# dependencies had stable enough APIs that I can probably live with a later
22+
# version too.
23+
bazel_dep(name = "abseil-cpp", version = "20250814.0")
24+
bazel_dep(name = "google_benchmark", version = "1.9.4")
25+
bazel_dep(name = "googletest", version = "1.17.0")
26+
bazel_dep(name = "re2", version = "2025-08-12")
27+
28+
bazel_dep(name = "hedron_compile_commands", dev_dependency = True)
29+
git_override(
30+
module_name = "hedron_compile_commands",
31+
commit = "abb61a688167623088f8768cc9264798df6a9d10",
32+
remote = "https://github.com/hedronvision/bazel-compile-commands-extractor.git",
33+
)
34+
35+
# For some completely incomprehensible reason, there appears to be a dependency version problem in the combination of
36+
# - the `github.com/docker/docker` Go module
37+
# - the `re2` Bazel module
38+
# - the `rules_cc` Bazel module
39+
# - the `abseil-cpp` Bazel module
40+
# that causes build errors for me. They look like this:
41+
#
42+
# $ bazel build //...
43+
# ERROR: /home/saser/.cache/bazel/_bazel_saser/06ad534583e887506ca6aa175f8ed7b5/external/abseil-cpp+/absl/utility/BUILD.bazel: no such target '@@abseil-cpp+//absl/utility:if_constexpr': target 'if_constexpr' not declared in package 'absl/utility' defined by /home/saser/.cache/bazel/_bazel_saser/06ad534583e887506ca6aa175f8ed7b5/external/abseil-cpp+/absl/utility/BUILD.bazel
44+
# ERROR: /home/saser/.cache/bazel/_bazel_saser/06ad534583e887506ca6aa175f8ed7b5/external/protobuf+/src/google/protobuf/BUILD.bazel:483:11: no such target '@@abseil-cpp+//absl/utility:if_constexpr': target 'if_constexpr' not declared in package 'absl/utility' defined by /home/saser/.cache/bazel/_bazel_saser/06ad534583e887506ca6aa175f8ed7b5/external/abseil-cpp+/absl/utility/BUILD.bazel and referenced by '@@protobuf+//src/google/protobuf:protobuf_lite'
45+
# ERROR: Analysis of target '//docker/dockertest:dockertest' failed; build aborted: Analysis failed
46+
# INFO: Elapsed time: 0.734s, Critical Path: 0.00s
47+
# INFO: 1 process: 1 internal.
48+
# ERROR: Build did NOT complete successfully
49+
#
50+
# And somehow, bumping the version of Protobuf (it resolves to 29.0 otherwise)
51+
# fixes this problem. I don't know why, but I'm keeping it here until further
52+
# notice. I might start relying on it anyway to run `protoc`, who knows.
53+
bazel_dep(name = "protobuf", version = "32.0")
54+
55+
# NOTE: there was a dependency on bazel_skylib but I dropped it because I
56+
# couldn't find any references to it in this repo... can't remember why I added
57+
# it either, maybe to fix some transitive dependency?
58+
59+
# -- use_repo_rule statements -- #
60+
61+
# -- repo definitions -- #
62+
63+
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
64+
llvm.toolchain(
65+
llvm_version = "15.0.6",
66+
)
67+
use_repo(llvm, "llvm_toolchain")
68+
69+
register_toolchains("@llvm_toolchain//:all")
70+
71+
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
72+
go_sdk.from_file(go_mod = "//:go.mod")
73+
74+
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
75+
go_deps.from_file(go_mod = "//:go.mod")
76+
77+
# See the README file located next to the patch file for an explanation of what
78+
# it does.
79+
go_deps.module_override(
80+
patch_strip = 1,
81+
patches = ["//patches/com_github_docker_cli:01-fix-broken-embeds.patch"],
82+
path = "github.com/docker/cli",
83+
)
84+
85+
# All *direct* Go dependencies of the module have to be listed explicitly.
86+
use_repo(go_deps, "cc_mvdan_gofumpt", "com_github_bazelbuild_buildtools", "com_github_cenkalti_backoff_v4", "com_github_google_go_cmp", "com_github_google_uuid", "com_github_jackc_pgerrcode", "com_github_jackc_pgx_v5", "com_github_jonboulle_clockwork", "com_github_masterminds_squirrel", "com_github_ory_dockertest_v3", "com_github_stretchr_testify", "io_k8s_klog_v2", "org_golang_google_grpc", "org_golang_google_grpc_cmd_protoc_gen_go_grpc", "org_golang_google_protobuf")
87+
88+
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
89+
oci.pull(
90+
name = "distroless_base",
91+
digest = "sha256:d605e138bb398428779e5ab490a6bbeeabfd2551bd919578b1044718e5c30798",
92+
image = "gcr.io/distroless/base",
93+
platforms = ["linux/amd64"],
94+
)
95+
oci.pull(
96+
name = "hello_world",
97+
digest = "sha256:a0dfb02aac212703bfcb339d77d47ec32c8706ff250850ecc0e19c8737b18567",
98+
image = "hello-world",
99+
platforms = ["linux/amd64"],
100+
)
101+
oci.pull(
102+
name = "nginx",
103+
# 'latest' as of 2025-09-13.
104+
digest = "sha256:d5f28ef21aabddd098f3dbc21fe5b7a7d7a184720bc07da0b6c9b9820e97f25e",
105+
image = "nginx",
106+
platforms = ["linux/amd64"],
107+
)
108+
oci.pull(
109+
name = "postgres",
110+
# 'latest' as of 2025-09-13.
111+
digest = "sha256:feff5b24fedd610975a1f5e743c51a4b360437f4dc3a11acf740dcd708f413f6",
112+
image = "postgres",
113+
platforms = ["linux/amd64"],
114+
)
115+
116+
# For each oci.pull call, repeat the "name" here to expose them as dependencies.
117+
use_repo(oci, "distroless_base", "distroless_base_linux_amd64", "hello_world", "hello_world_linux_amd64", "nginx", "nginx_linux_amd64", "postgres", "postgres_linux_amd64")

0 commit comments

Comments
 (0)