Skip to content

Commit e5754bf

Browse files
committed
Modifying examples for gazelle and bzlmod
This commit removes bzlmod support from the build_file_generation example and adds bzlmod support to the bzlmod example.
1 parent 52e14b7 commit e5754bf

File tree

9 files changed

+729
-101
lines changed

9 files changed

+729
-101
lines changed

.bazelci/presubmit.yml

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ buildifier:
4747
build_targets: ["..."]
4848
test_targets: ["..."]
4949
.coverage_targets_example_bzlmod: &coverage_targets_example_bzlmod
50-
coverage_targets: ["//:test"]
50+
coverage_targets: ["//:bzlmod_test"]
5151
.coverage_targets_example_multi_python: &coverage_targets_example_multi_python
5252
coverage_targets:
5353
- //tests:my_lib_3_10_test
@@ -147,32 +147,6 @@ tasks:
147147
working_directory: examples/build_file_generation
148148
platform: windows
149149

150-
integration_test_build_file_generation_bzlmod_ubuntu:
151-
<<: *minimum_supported_bzlmod_version
152-
<<: *common_bzlmod_flags
153-
<<: *reusable_build_test_all
154-
name: build_file_generation_bzlmod integration tests on Ubuntu
155-
working_directory: examples/build_file_generation
156-
platform: ubuntu2004
157-
integration_test_build_file_generation_bzlmod_debian:
158-
<<: *common_bzlmod_flags
159-
<<: *reusable_build_test_all
160-
name: build_file_generation_bzlmod integration tests on Debian
161-
working_directory: examples/build_file_generation
162-
platform: debian11
163-
integration_test_build_file_generation_bzlmod_macos:
164-
<<: *common_bzlmod_flags
165-
<<: *reusable_build_test_all
166-
name: build_file_generation_bzlmod integration tests on macOS
167-
working_directory: examples/build_file_generation
168-
platform: macos
169-
integration_test_build_file_generation_bzlmod_windows:
170-
<<: *common_bzlmod_flags
171-
<<: *reusable_build_test_all
172-
name: build_file_generation_bzlmod integration tests on Windows
173-
working_directory: examples/build_file_generation
174-
platform: windows
175-
176150
integration_test_bzlmod_ubuntu_min:
177151
<<: *minimum_supported_bzlmod_version
178152
<<: *reusable_build_test_all

examples/build_file_generation/MODULE.bazel

Lines changed: 0 additions & 43 deletions
This file was deleted.

examples/build_file_generation/WORKSPACE

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,24 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
1616
######################################################################
1717

1818
# Define an http_archive rule that will download the below ruleset,
19-
# test the sha, and extract the ruleset to you local bazel cache.
19+
# test the sha256, and extract the ruleset to you local bazel cache.
20+
2021
http_archive(
2122
name = "io_bazel_rules_go",
22-
sha256 = "099a9fb96a376ccbbb7d291ed4ecbdfd42f6bc822ab77ae6f1b5cb9e914e94fa",
23+
sha256 = "6b65cb7917b4d1709f9410ffe00ecf3e160edf674b78c54a894471320862184f",
2324
urls = [
24-
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
25-
"https://github.com/bazelbuild/rules_go/releases/download/v0.35.0/rules_go-v0.35.0.zip",
25+
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.39.0/rules_go-v0.39.0.zip",
26+
"https://github.com/bazelbuild/rules_go/releases/download/v0.39.0/rules_go-v0.39.0.zip",
2627
],
2728
)
2829

2930
# Download the bazel_gazelle ruleset.
3031
http_archive(
3132
name = "bazel_gazelle",
32-
sha256 = "448e37e0dbf61d6fa8f00aaa12d191745e14f07c31cabfa731f0c8e8a4f41b97",
33+
sha256 = "727f3e4edd96ea20c29e8c2ca9e8d2af724d8c7778e7923a854b2c80952bc405",
3334
urls = [
34-
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.tar.gz",
35-
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.28.0/bazel-gazelle-v0.28.0.tar.gz",
35+
"https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
36+
"https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.30.0/bazel-gazelle-v0.30.0.tar.gz",
3637
],
3738
)
3839

@@ -48,7 +49,7 @@ go_rules_dependencies()
4849
# go_rules_dependencies is a function that registers external dependencies
4950
# needed by the Go rules.
5051
# See: https://github.com/bazelbuild/rules_go/blob/master/go/dependencies.rst#go_rules_dependencies
51-
go_register_toolchains(version = "1.19.4")
52+
go_register_toolchains(version = "1.19.5")
5253

5354
# The following call configured the gazelle dependencies, Go environment and Go SDK.
5455
gazelle_dependencies()

examples/build_file_generation/WORKSPACE.bzlmod

Lines changed: 0 additions & 2 deletions
This file was deleted.

examples/bzlmod/BUILD.bazel

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
1-
load("@pip//:requirements.bzl", "requirement")
1+
# Load various rules so that we can have bazel download
2+
# various rulesets and dependencies.
3+
# The `load` statement imports the symbol for the rule, in the defined
4+
# ruleset. When the symbol is loaded you can use the rule.
5+
6+
# The following code loads the base python requirements and gazelle
7+
# requirements.
8+
load("@bazel_gazelle//:def.bzl", "gazelle")
9+
load("@pip//:requirements.bzl", "all_whl_requirements")
210
load("@python3_9//:defs.bzl", py_test_with_transition = "py_test")
311
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
412
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
13+
load("@rules_python_gazelle_plugin//:def.bzl", "GAZELLE_PYTHON_RUNTIME_DEPS")
14+
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
15+
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")
516

17+
# This stanza calls a rule that generates targets for managing pip dependencies
18+
# with pip-compile.
619
compile_pip_requirements(
720
name = "requirements",
821
extra_args = ["--allow-unsafe"],
@@ -11,35 +24,76 @@ compile_pip_requirements(
1124
requirements_windows = "requirements_windows.txt",
1225
)
1326

27+
# This repository rule fetches the metadata for python packages we
28+
# depend on. That data is required for the gazelle_python_manifest
29+
# rule to update our manifest file.
30+
modules_mapping(
31+
name = "modules_map",
32+
exclude_patterns = [
33+
"^_|(\\._)+", # This is the default.
34+
"(\\.tests)+", # Add a custom one to get rid of the psutil tests.
35+
],
36+
wheels = all_whl_requirements,
37+
)
38+
39+
# Gazelle python extension needs a manifest file mapping from
40+
# an import to the installed package that provides it.
41+
# This macro produces two targets:
42+
# - //:gazelle_python_manifest.update can be used with `bazel run`
43+
# to recalculate the manifest
44+
# - //:gazelle_python_manifest.test is a test target ensuring that
45+
# the manifest doesn't need to be updated
46+
gazelle_python_manifest(
47+
name = "gazelle_python_manifest",
48+
modules_mapping = ":modules_map",
49+
pip_repository_name = "pip",
50+
requirements = "//:requirements_lock.txt",
51+
# NOTE: we can use this flag in order to make our setup compatible with
52+
# bzlmod.
53+
use_pip_repository_aliases = True,
54+
)
55+
56+
# Our gazelle target points to the python gazelle binary.
57+
# This is the simple case where we only need one language supported.
58+
# If you also had proto, go, or other gazelle-supported languages,
59+
# you would also need a gazelle_binary rule.
60+
# See https://github.com/bazelbuild/bazel-gazelle/blob/master/extend.rst#example
61+
gazelle(
62+
name = "gazelle",
63+
data = GAZELLE_PYTHON_RUNTIME_DEPS,
64+
gazelle = "@rules_python_gazelle_plugin//python:gazelle_binary",
65+
)
66+
67+
# gazelle does not support transition tests yet
68+
# so we add the following directive for gazelle to ignore
69+
# the following rule.
70+
# gazelle:ignore
71+
py_test_with_transition(
72+
name = "test_with_transition",
73+
srcs = ["__test__.py"],
74+
main = "__test__.py",
75+
deps = [":bzlmod"],
76+
)
77+
78+
# The following targets are created by gazelle
1479
py_library(
15-
name = "lib",
80+
name = "bzlmod",
1681
srcs = ["lib.py"],
17-
deps = [
18-
requirement("pylint"),
19-
requirement("tabulate"),
20-
requirement("python-dateutil"),
21-
],
82+
visibility = ["//:__subpackages__"],
83+
deps = ["@pip//tabulate"],
2284
)
2385

2486
py_binary(
25-
name = "bzlmod",
87+
name = "bzlmod_bin",
2688
srcs = ["__main__.py"],
2789
main = "__main__.py",
2890
visibility = ["//:__subpackages__"],
29-
deps = [
30-
":lib",
31-
],
91+
deps = [":bzlmod"],
3292
)
3393

3494
py_test(
35-
name = "test",
36-
srcs = ["test.py"],
37-
deps = [":lib"],
38-
)
39-
40-
py_test_with_transition(
41-
name = "test_with_transition",
42-
srcs = ["test.py"],
43-
main = "test.py",
44-
deps = [":lib"],
95+
name = "bzlmod_test",
96+
srcs = ["__test__.py"],
97+
main = "__test__.py",
98+
deps = [":bzlmod"],
4599
)

examples/bzlmod/MODULE.bazel

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,89 @@
1+
# This file replaces the WORKSPACE file when using bzlmod.
2+
3+
# Declares certain properties of the Bazel module represented by the current Bazel repo.
4+
# These properties are either essential metadata of the module (such as the name and version),
5+
# or affect behavior of the current module and its dependents.
16
module(
27
name = "example_bzlmod",
38
version = "0.0.0",
49
compatibility_level = 1,
510
)
611

12+
# The following stanza defines the dependency rules_python.
13+
# For typical setups you set the version.
14+
# See the releases page for available versions.
15+
# https://github.com/bazelbuild/rules_python/releases
716
bazel_dep(name = "rules_python", version = "0.0.0")
17+
18+
# The following loads rules_python from the file system.
19+
# For usual setups you should remove this local_path_override block.
820
local_path_override(
921
module_name = "rules_python",
1022
path = "../..",
1123
)
1224

25+
# The following stanza defines the dependency rules_python_gazelle_plugin.
26+
# For typical setups you set the version.
27+
# See the releases page for available versions.
28+
# https://github.com/bazelbuild/rules_python/releases
29+
bazel_dep(name = "rules_python_gazelle_plugin", version = "0.0.0")
30+
31+
# The following starlark loads the gazelle plugin from the file system.
32+
# For usual setups you should remove this local_path_override block.
33+
local_path_override(
34+
module_name = "rules_python_gazelle_plugin",
35+
path = "../../gazelle",
36+
)
37+
38+
# The following stanza defines the dependency for gazelle
39+
# See here https://github.com/bazelbuild/bazel-gazelle/releases/ for the
40+
# latest version.
41+
bazel_dep(name = "gazelle", version = "0.30.0", repo_name = "bazel_gazelle")
42+
43+
# The following stanze returns a proxy object representing a module extension;
44+
# its methods can be invoked to create module extension tags.
1345
python = use_extension("@rules_python//python:extensions.bzl", "python")
46+
47+
# Intialize the python toolchain.
48+
# You can set different Python versions in this block.
1449
python.toolchain(
1550
name = "python3_9",
1651
configure_coverage_tool = True,
1752
python_version = "3.9",
1853
)
54+
55+
# Import the python repositories generated by the given module extension into the scope of the current module.
1956
use_repo(python, "python3_9")
2057
use_repo(python, "python3_9_toolchains")
2158

59+
# Register an already-defined toolchain so that Bazel can use it during toolchain resolution.
2260
register_toolchains(
2361
"@python3_9_toolchains//:all",
2462
)
2563

64+
# Use the pip extension
2665
pip = use_extension("@rules_python//python:extensions.bzl", "pip")
66+
67+
# Use the extension to call the `pip_repository` rule that invokes `pip`, with `incremental` set.
68+
# Accepts a locked/compiled requirements file and installs the dependencies listed within.
69+
# Those dependencies become available in a generated `requirements.bzl` file.
70+
# You can instead check this `requirements.bzl` file into your repo.
71+
# Because this project has different requirements for windows vs other
72+
# operating systems, we have requirements for each.
2773
pip.parse(
2874
name = "pip",
75+
# When using gazelle you must use set the following flag
76+
# in order for the generation of gazelle dependency resolution.
77+
incompatible_generate_aliases = True,
2978
requirements_lock = "//:requirements_lock.txt",
3079
requirements_windows = "//:requirements_windows.txt",
3180
)
81+
82+
# Imports the pip toolchain generated by the given module extension into the scope of the current module.
3283
use_repo(pip, "pip")
3384

85+
# This project includes a different module that is on the local file system.
86+
# Add the module to this parent project.
3487
bazel_dep(name = "other_module", version = "", repo_name = "our_other_module")
3588
local_path_override(
3689
module_name = "other_module",
File renamed without changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy