Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start using bzlmod #165

Merged
merged 5 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Always tell why tests fail
test --test_output=errors

# We use Bazel's modern dependency management system. This works for us only with Bazel >= 6.2.0
# This is the deprecated version. "--enable_bzlmod" is the forward path. However, as Bazel < 6.0.0 does not support the
# new flag. We keep using the deprecated one until we no longer support Bazel 5.
build --experimental_enable_bzlmod=true

# Mypy integration
build:mypy --aspects=@mypy_integration//:mypy.bzl%mypy_aspect
build:mypy --output_groups=mypy
Expand Down
2 changes: 1 addition & 1 deletion .bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.2.0
6.4.0
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ repos:
additional_dependencies:
- mdformat-gfm
- repo: https://github.com/keith/pre-commit-buildifier
rev: 6.3.3.1
rev: 6.4.0
hooks:
- id: buildifier
args: [ "--warnings=+out-of-order-load,+unsorted-dict-items,+native-py" ]
Expand Down
117 changes: 117 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
module(
name = "depend_on_what_you_use",
bazel_compatibility = [">=5.4.0"],
compatibility_level = 0,
)

bazel_dep(
name = "bazel_skylib",
# Keep in sync with //third_party/dependencies.bzl
version = "1.5.0",
)
bazel_dep(
name = "rules_python",
# Keep in sync with //third_party/dependencies.bzl
version = "0.27.0",
)

pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
hub_name = "dwyu_py_deps",
python_version = "3.10",
requirements_lock = "//third_party:requirements.txt",
)
use_repo(pip, "dwyu_py_deps")

###
### Development Dependencies
###

# Update with each rules_python update for easy lookup until specifying the patch version for toolchain is possible again
# Keep in syc with test/aspect/execute_tests.py
# PATCH_MAPPING:
# "3.10": "3.10.13"
# "3.11": "3.11.6"
# "3.12": "3.12.0"
# "3.8": "3.8.18"
# "3.9": "3.9.18"
# Choose different version via: --@rules_python//python/config_settings:python_version=Major.Minor.Patch mathing one of
# the registered toolchains below.
python = use_extension(
"@rules_python//python/extensions:python.bzl",
"python",
dev_dependency = True,
)
python.toolchain(
python_version = "3.8",
)
python.toolchain(
python_version = "3.9",
)
python.toolchain(
is_default = True,
python_version = "3.10",
)
python.toolchain(
python_version = "3.11",
)
python.toolchain(
python_version = "3.12",
)

pip_dev = use_extension(
"@rules_python//python/extensions:pip.bzl",
"pip",
dev_dependency = True,
)
pip_dev.parse(
hub_name = "dwyu_mypy_deps",
python_version = "3.10",
requirements_lock = "//third_party:mypy_requirements.txt",
)
use_repo(pip_dev, "dwyu_mypy_deps")

non_module_dependencies = use_extension(
"//third_party:extensions.bzl",
"non_module_dependencies",
dev_dependency = True,
)
use_repo(non_module_dependencies, "mypy_integration")

bazel_dep(
name = "external_test_repo",
version = "0.0.0",
dev_dependency = True,
)
local_path_override(
module_name = "external_test_repo",
path = "test/aspect/external_repo/repo",
)

bazel_dep(
name = "complex_includes_test_repo",
version = "0.0.0",
dev_dependency = True,
)
local_path_override(
module_name = "complex_includes_test_repo",
path = "test/aspect/complex_includes/ext_repo",
)

###
### The Migration phase using WORKSPACE.bzlmod and MODULE.bazel together does not support properly loading the implicit
### Bazel dependencies. Thus, we need to load some basic things directly. This should become superfluous when we are
### only using bzlmod eventually
### Reference https://github.com/bazelbuild/bazel/blob/master/src/MODULE.tools
###

bazel_dep(
name = "platforms",
version = "0.0.7",
dev_dependency = True,
)
bazel_dep(
name = "rules_cc",
version = "0.0.1",
dev_dependency = True,
)
Empty file added WORKSPACE.bzlmod
Empty file.
8 changes: 4 additions & 4 deletions dev_setup_step_2.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def dev_setup_step_2():
# Choose different version via: --@rules_python//python/config_settings:python_version=X
python_register_multi_toolchains(
name = "python",
default_version = "3.10.9",
default_version = "3.10",
python_versions = [
"3.8.15",
"3.9.16",
"3.11.1",
"3.8",
"3.9",
"3.11",
],
)
2 changes: 1 addition & 1 deletion test/aspect/complex_includes/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ cc_library(
cc_library(
name = "use_complex_includes_from_extern",
srcs = ["use_complex_includes.cpp"],
deps = ["@complex_includes_repo//:complex_includes"],
deps = ["@complex_includes_test_repo//:complex_includes"],
)
2 changes: 1 addition & 1 deletion test/aspect/complex_includes/ext_repo.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# buildifier: disable=unnamed-macro
def load_complex_includes_repo():
native.local_repository(
name = "complex_includes_repo",
name = "complex_includes_test_repo",
path = "test/aspect/complex_includes/ext_repo",
)
1 change: 1 addition & 0 deletions test/aspect/complex_includes/ext_repo/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module(name = "complex_includes_test_repo")
27 changes: 20 additions & 7 deletions test/aspect/execute_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,32 @@

# Test matrix. We don't combine each Bazel version with each Python version as there is no significant benefit. We
# manually define pairs which make sure each Bazel and Python version we care about is used at least once.
# Keep in sync with MODULE.bazel
TESTED_VERSIONS = [
TestedVersions(bazel="5.4.1", python="3.8.15"),
TestedVersions(bazel="6.0.0", python="3.9.16"),
TestedVersions(bazel="6.4.0", python="3.10.9"),
TestedVersions(bazel="7.0.0rc5", python="3.11.1"),
TestedVersions(bazel="8.0.0-pre.20231030.2", python="3.11.1"),
TestedVersions(bazel="5.4.1", python="3.8.18"),
TestedVersions(bazel="6.0.0", python="3.9.18"),
TestedVersions(bazel="6.4.0", python="3.10.13"),
TestedVersions(bazel="7.0.0", python="3.11.6"),
TestedVersions(bazel="8.0.0-pre.20231030.2", python="3.12.0"),
]

# When Bazel 7.0.0 releases we have to look again at the flags and check if more flags are available
VERSION_SPECIFIC_ARGS = {
# We support Bazel's modern dependency management system, but it works only as desired with a recent Bazel version
"--experimental_enable_bzlmod=false": CompatibleVersions(max="6.1.99"),
# We are not yet sure if we really want to lock the bzlmod resolution down given we test with various Bazel versions
# and configurations. It seems the main benefits of the lock file are not having to reanalyze the central registry
# when working without a cached workspace and being safeguarded against changed or yanked modules in the central
# registry. Both don't matter much to us right now.
"--lockfile_mode=off": CompatibleVersions(min="6.2.0"),
# Incompatible changes
"--incompatible_legacy_local_fallback=false": CompatibleVersions(min="5.0.0"), # false is the forward path behavior
"--incompatible_enforce_config_setting_visibility": CompatibleVersions(min="5.0.0"),
"--incompatible_config_setting_private_default_visibility": CompatibleVersions(min="5.0.0"),
"--incompatible_disable_target_provider_fields": CompatibleVersions(min="5.0.0"),
"--incompatible_struct_has_no_methods": CompatibleVersions(min="5.0.0"),
"--incompatible_use_platforms_repo_for_constraints": CompatibleVersions(min="5.0.0", max="6.99.99"),
"--incompatible_disallow_empty_glob": CompatibleVersions(min="5.0.0"),
"--incompatible_existing_rules_immutable_view": CompatibleVersions(min="5.0.0"),
"--incompatible_no_implicit_file_export": CompatibleVersions(min="5.0.0"),
"--incompatible_use_cc_configure_from_rules_cc": CompatibleVersions(min="5.0.0"),
"--incompatible_default_to_explicit_init_py": CompatibleVersions(min="5.0.0"),
Expand All @@ -40,8 +48,13 @@
"--incompatible_sandbox_hermetic_tmp": CompatibleVersions(min="6.0.0"),
"--incompatible_check_testonly_for_output_files": CompatibleVersions(min="6.0.0"),
"--incompatible_check_visibility_for_toolchains": CompatibleVersions(min="7.0.0"),
"--incompatible_auto_exec_groups": CompatibleVersions(min="7.0.0"),
"--incompatible_disable_non_executable_java_binary": CompatibleVersions(min="7.0.0"),
# TODO for us to work on those:
# "--incompatible_stop_exporting_language_modules": CompatibleVersions(min="5.0.0"),
# Theoretically interesting for our project, but Bazel itself does not adhere to it
# "--incompatible_python_disallow_native_rules": CompatibleVersions(min="7.0.0"),
# Theoretically of interest for us, but rules_python does not comply to this.
# "--incompatible_disallow_struct_provider_syntax": CompatibleVersions(min="7.0.0"),
}

Expand Down Expand Up @@ -239,7 +252,7 @@
),
TestCase(
name="complex_includes_in_ext_repo",
cmd=TestCmd(target="@complex_includes_repo//...", aspect=DEFAULT_ASPECT),
cmd=TestCmd(target="@complex_includes_test_repo//...", aspect=DEFAULT_ASPECT),
expected=ExpectedResult(success=True),
),
TestCase(
Expand Down
4 changes: 2 additions & 2 deletions test/aspect/external_repo/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cc_library(
name = "use_external_libs",
hdrs = ["use_external_libs.h"],
deps = [
"@ext_repo//:ext_bar",
"@ext_repo//:ext_foo",
"@external_test_repo//:ext_bar",
"@external_test_repo//:ext_foo",
],
)
2 changes: 1 addition & 1 deletion test/aspect/external_repo/repo.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# buildifier: disable=unnamed-macro
def load_external_repo():
native.local_repository(
name = "ext_repo",
name = "external_test_repo",
path = "test/aspect/external_repo/repo",
)
1 change: 1 addition & 0 deletions test/aspect/external_repo/repo/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module(name = "external_test_repo")
2 changes: 2 additions & 0 deletions third_party/dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

def dependencies():
# Keep in sync with MODULE.bazel
rules_python_version = "0.27.0"
maybe(
http_archive,
Expand All @@ -11,6 +12,7 @@ def dependencies():
urls = ["https://github.com/bazelbuild/rules_python/releases/download/{v}/rules_python-{v}.tar.gz".format(v = rules_python_version)],
)

# Keep in sync with MODULE.bazel
skylib_version = "1.5.0"
http_archive(
name = "bazel_skylib",
Expand Down
8 changes: 8 additions & 0 deletions third_party/extensions.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
load("//third_party:dev_dependencies.bzl", "dev_dependencies")

def _non_module_dependencies_impl(_ctx):
dev_dependencies()

non_module_dependencies = module_extension(
implementation = _non_module_dependencies_impl,
)