Skip to content

Commit

Permalink
Enable Bzlmod
Browse files Browse the repository at this point in the history
Fulfills most of bazelbuild#1482. Compatible with Bazel 6.5.0 and 7.5.0, but not
Bazel 8.

New `MODULE.bazel` files mirroring existing `WORKSPACE` configurations
comprise the bulk of the commit. The empty `WORKSPACE.bzlmod` files
ensure that Bzlmod won't evaluate the existing `WORKSPACE` files.

These new files comprise the most significant part of the change:

- scala/extensions/config.bzl
- scala/extensions/deps.bzl
- scala/private/extensions/dev_deps.bzl
- scala/private/macros/bzlmod.bzl
- scala/private/macros/test/BUILD.bzlmod_test
- scala/private/macros/test/MODULE.bzlmod_test
- scala/private/macros/test/bzlmod_test_ext.bzl
- test/shell/test_bzlmod_macros.sh

The pattern employed throughout the new module extensions is explained
in the `scala/private/macros/bzlmod.bzl` docstring. Adding
`test/shell/test_bzlmod_macros.sh` also precipitated adding a new
`assert_matches` helper to `test/shell/test_helper.sh`.

"Publish to BCR" configuration will come in a future commit. After that,
we can publish a new major version and then close bazelbuild#1482.

Per bazelbuild#1647, `com_google_protobuf` remains at v21.7, and versions up to
v25.5 can work after bumping `com_google_absl` to 20240722.0 and setting
C++17 compiler flags in `.bazelrc`.

This change enables Bazel 6 and 7 users to migrate their `rules_scala`
dependency from `WORKSPACE` to `MODULE.bazel` whenever they're ready.

After the next major version release includes this change, we can update
`com_google_protobuf`, `rules_java`, and related dependencies as part of
enabling Bazel 8 compatibility (bazelbuild#1652). The `rules_jvm_external` update
in that change will effectively end Bzlmod support for Bazel 6.5.0,
requiring another major version release after that.
  • Loading branch information
mbland committed Feb 19, 2025
1 parent 17ef600 commit ac7471b
Show file tree
Hide file tree
Showing 51 changed files with 1,614 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Remove once the following is fixed:
# - bazelbuild/bazel: Loading top-level targets in local_path_override modules
# in child directory breaks the build #22208
# https://github.com/bazelbuild/bazel/issues/22208
third_party/test/example_external_workspace
third_party/test/proto
6 changes: 2 additions & 4 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
common --enable_bzlmod

build --enable_platform_specific_config

#Windows needs --worker_quit_after_build due to workers not being shut down when the compiler tools need to be rebuilt (resulting in 'file in use' errors). See Bazel Issue#10498.

build:windows --worker_quit_after_build --enable_runfiles

# Remove upon completing Bzlmod compatibility work.
# - https://github.com/bazelbuild/rules_scala/issues/1482
build --noenable_bzlmod
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@ test/semanticdb/tempsrc

# From scripts/create_repository.py
repository-artifacts.json

# Until it settles down
**/MODULE.bazel.lock

# Used by some tests, but can also be used for local experimentation.
tmp/
290 changes: 290 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
"""Bazel module definition for rules_scala"""

module(
name = "rules_scala",
version = "7.0.0",
compatibility_level = 7,
bazel_compatibility = [">=6.5.0", "<8.0.0"],
)

SCALA_VERSION = "2.12.20"

# These versions match those required by some tests, including
# test_thirdparty_version.sh.
SCALA_2_VERSIONS = [
"2.11.12",
"2.12.20",
"2.13.15",
]

SCALA_3_VERSIONS = [
"3.1.3",
"3.3.5",
"3.5.2",
"3.6.3",
]

SCALA_VERSIONS = SCALA_2_VERSIONS + SCALA_3_VERSIONS

bazel_dep(name = "bazel_skylib", version = "1.7.1")

# Bazel 6 breaks with any higher version of `rules_cc`, because:
# - 0.0.10 requires Bazel 7 to define `CcSharedLibraryHintInfo`
# - 0.0.13 and up don't support `protobuf` v21.7, requiring at least v27.0
# - 0.1.0 should work, but requires `stardoc` 0.7.0, which requires Bazel 7
# (though it's a `dev_dependency`, it still gets pulled in during analysis,
# breaking the build)
bazel_dep(name = "rules_cc", version = "0.0.9")
single_version_override(
module_name = "rules_cc",
version = "0.0.9",
)

bazel_dep(name = "rules_java", version = "7.12.4")
bazel_dep(name = "rules_proto", version = "6.0.2")

# For now, users are revlocked to protobuf-21.7 or protobuf-25.5 (which doesn't
# build under Bazel 6).
bazel_dep(
name = "protobuf",
version = "21.7",
repo_name = "com_google_protobuf",
)
single_version_override(
module_name = "protobuf",
version = "21.7",
)

scala_config = use_extension(
"//scala/extensions:config.bzl",
"scala_config",
)
use_repo(scala_config, "io_bazel_rules_scala_config")

dev_config = use_extension(
"//scala/extensions:config.bzl",
"scala_config",
dev_dependency = True,
)
dev_config.settings(
enable_compiler_dependency_tracking = True,
scala_version = SCALA_VERSION,
scala_versions = SCALA_VERSIONS,
)

scala_deps = use_extension("//scala/extensions:deps.bzl", "scala_deps")

# Register some of our testing toolchains first when building our repo.
register_toolchains(
"//scala:unused_dependency_checker_error_toolchain",
"//test/proto:scalapb_toolchain",
"//test/toolchains:java21_toolchain_definition",
dev_dependency = True,
)

use_repo(
scala_deps,
"rules_scala_toolchains",
"scala_compiler_sources",
)

register_toolchains("@rules_scala_toolchains//...:all")

# Dev dependencies

dev_deps = use_extension(
"//scala/extensions:deps.bzl",
"scala_deps",
dev_dependency = True,
)
dev_deps.toolchains(
jmh = True,
scala_proto = True,
#scala_proto_enable_all_options = True,
scalafmt = True,
testing = True,
#scalatest = True,
#junit = True,
#specs2 = True,
twitter_scrooge = True,
)

use_repo(
dev_deps,
"scala_proto_rules_scalapb_compilerplugin",
"scala_proto_rules_scalapb_protoc_bridge",
"scalafmt_default",
)

# Default versions of version specific repos needed by some of our tests. Tests
# that set `--repo_env=SCALA_VERSION=...` break without using the default here,
# because version specific repos for other versions won't be available.
use_repo(
dev_deps,
"io_bazel_rules_scala_guava",
"io_bazel_rules_scala_junit_junit",
"io_bazel_rules_scala_scala_compiler",
"io_bazel_rules_scala_scala_library",
)

[
[
use_repo(dev_deps, dep + "_" + scala_version.replace(".", "_"))
for dep in [
"io_bazel_rules_scala_junit_junit",
"io_bazel_rules_scala_scala_compiler",
"io_bazel_rules_scala_scala_library",
] + (
# We can remove this condition once we drop support for Scala 2.11.
["scala_proto_rules_scalapb_protoc_gen"]
if not scala_version.startswith("2.11.") else []
)
]
for scala_version in SCALA_VERSIONS
]

[
[
use_repo(dev_deps, dep + "_" + scala_version.replace(".", "_"))
for dep in [
"io_bazel_rules_scala_scala_reflect",
]
]
for scala_version in SCALA_2_VERSIONS
]

[
[
use_repo(dev_deps, dep + "_" + scala_version.replace(".", "_"))
for dep in [
"io_bazel_rules_scala_scala_compiler_2",
"io_bazel_rules_scala_scala_library_2",
"io_bazel_rules_scala_scala_reflect_2",
]
]
for scala_version in SCALA_3_VERSIONS
]

internal_dev_deps = use_extension(
"//scala/private/extensions:dev_deps.bzl",
"dev_deps",
dev_dependency = True,
)

# See //scala/private:extensions/dev_deps.bzl for notes on some of these repos.
use_repo(
internal_dev_deps,
"com_github_bazelbuild_buildtools",
"com_github_jnr_jffi_native",
"com_google_guava_guava_21_0",
"com_google_guava_guava_21_0_with_file",
"com_twitter__scalding_date",
"org_apache_commons_commons_lang_3_5",
"org_apache_commons_commons_lang_3_5_without_file",
"org_springframework_spring_core",
"org_springframework_spring_tx",
"org_typelevel__cats_core",
"org_typelevel_kind_projector",
)

java_toolchains = use_extension(
"@rules_java//java:extensions.bzl",
"toolchains",
dev_dependency = True,
)

use_repo(
java_toolchains,
# //test/toolchains:java21_toolchain
"remotejdk21_linux",
"remotejdk21_macos",
"remotejdk21_win",
# //test/jmh:test_jmh_jdk8
"remote_jdk8_linux",
"remote_jdk8_macos",
"remote_jdk8_windows",
)

[
(
bazel_dep(name = name, dev_dependency = True),
local_path_override(module_name = name, path = path)
)
for name, path in [
(
"proto_cross_repo_boundary",
"test/proto_cross_repo_boundary/repo",
),
(
"test_new_local_repo",
"third_party/test/new_local_repo",
),
(
"example_external_workspace",
"third_party/test/example_external_workspace",
),
]
]

bazel_dep(
name = "platforms",
version = "0.0.11",
dev_dependency = True,
)
bazel_dep(
name = "bazel_ci_rules",
version = "1.0.0",
dev_dependency = True,
repo_name = "bazelci_rules",
)
bazel_dep(
name = "rules_go",
version = "0.53.0",
dev_dependency = True,
repo_name = "io_bazel_rules_go", # for com_github_bazelbuild_buildtools
)
bazel_dep(name = "gazelle", version = "0.42.0", dev_dependency = True)

go_sdk = use_extension(
"@io_bazel_rules_go//go:extensions.bzl",
"go_sdk",
dev_dependency = True,
)
go_sdk.download(version = "1.24.0")

go_deps = use_extension(
"@gazelle//:extensions.bzl",
"go_deps",
dev_dependency = True,
)

# The go_deps.module calls are inspired by the following to get the
# com_github_bazelbuild_buildtools repo to work:
#
# - https://github.com/bazelbuild/bazel-central-registry/blob/main/modules/gazelle/0.39.1/MODULE.bazel#L31-L57
#
# To get the latest version and hashes for each per:
#
# - https://go.dev/ref/mod#go-list-m
# - https://go.dev/ref/mod#checksum-database
#
# go list -m golang.org/x/tools@latest
# curl https://sum.golang.org/lookup/golang.org/x/[email protected]
go_deps.module(
path = "golang.org/x/tools",
sum = "h1:BgcpHewrV5AUp2G9MebG4XPFI1E2W41zU1SaqVA9vJY=",
version = "v0.30.0",
)

go_deps.module(
path = "github.com/golang/protobuf",
sum = "h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=",
version = "v1.5.4",
)
use_repo(
go_deps,
"com_github_golang_protobuf",
"org_golang_x_tools",
)

bazel_dep(name = "rules_python", version = "0.38.0", dev_dependency = True)
Empty file added WORKSPACE.bzlmod
Empty file.
15 changes: 15 additions & 0 deletions dt_patches/compiler_sources/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Bazel module ./test/shell/test_examples.sh tests"""

module(name = "compiler_sources")

bazel_dep(name = "rules_scala")
local_path_override(
module_name = "rules_scala",
path = "../..",
)

scala_config = use_extension(
"@rules_scala//scala/extensions:config.bzl",
"scala_config",
)
use_repo(scala_config, "io_bazel_rules_scala_config")
Empty file.
7 changes: 7 additions & 0 deletions dt_patches/compiler_sources/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,10 @@ def import_compiler_source_repos():
licenses = ["notice"],
server_urls = default_maven_server_urls(),
)

def _compiler_source_repos_impl(_ctx):
import_compiler_source_repos()

compiler_source_repos = module_extension(
implementation = _compiler_source_repos_impl,
)
2 changes: 2 additions & 0 deletions dt_patches/test_dt_patches/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ SCALA_LIBS = ["@scala_library"] + select_for_scala_version(

setup_scala_toolchain(
name = "dt_scala_toolchain",
parser_combinators_deps = [],
scala_compile_classpath = ["@scala_compiler"] + SCALA_LIBS,
scala_library_classpath = SCALA_LIBS,
scala_macro_classpath = SCALA_LIBS,
scala_xml_deps = [],
)
Loading

0 comments on commit ac7471b

Please sign in to comment.