Skip to content

Commit e44dee1

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. At this point, I have `bazel build //...` and `bazel test //...` up and running smoothly. The only thing I am aware of that doesn't work is `bazel run @hedron_compile_commands//:refresh_all`, but it seems that the issue I am running into is getting fixed [soon](hedronvision/bazel-compile-commands-extractor#262). 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. **Bumped version of most dependencies.** The main reason for doing so was to get on versions of dependencies that likely had much better support for Bzlmod. **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 e44dee1

File tree

58 files changed

+2498
-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

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