Skip to content

Commit 0f2946a

Browse files
committed
Withdraw modules functions bazed on bazelbuild#548
Based @fmeum's review comments on bazelbuild#548, removed the following functions from `lib/modules.bzl`: - `adjust_main_repo_prefix`: prefer solutions based on `attr.label_list`, `Label`, and `native.package_relative_label` to pass `Label` values in the code - `is_bzlmod_enabled`: available in `@bazel_features`, only added because of `repo_name` - `repo_name`: replaced with a `getattr` + `hasattr` expression, since Bazel 5 is near end of life and it's easier to search and replace this expression
1 parent a77e892 commit 0f2946a

File tree

3 files changed

+16
-141
lines changed

3 files changed

+16
-141
lines changed

docs/modules_doc.md

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,6 @@
22

33
Skylib module containing utilities for Bazel modules and module extensions.
44

5-
<a id="modules.adjust_main_repo_prefix"></a>
6-
7-
## modules.adjust_main_repo_prefix
8-
9-
<pre>
10-
modules.adjust_main_repo_prefix(<a href="#modules.adjust_main_repo_prefix-target_pattern">target_pattern</a>)
11-
</pre>
12-
13-
Updates the main repository prefix to match the current Bazel version.
14-
15-
The main repo prefix will be "@//" for Bazel < 7.1.0, and "@@//" for Bazel
16-
>= 7.1.0 under Bzlmod. This macro automatically updates strings representing
17-
include/exclude target patterns so that they match actual main repository
18-
target Labels correctly.
19-
20-
21-
**PARAMETERS**
22-
23-
24-
| Name | Description | Default Value |
25-
| :------------- | :------------- | :------------- |
26-
| <a id="modules.adjust_main_repo_prefix-target_pattern"></a>target_pattern | a string used to match a BUILD target pattern | none |
27-
28-
**RETURNS**
29-
30-
the string with any main repository prefix updated to match the current
31-
Bazel version
32-
33-
345
<a id="modules.apparent_repo_label_string"></a>
356

367
## modules.apparent_repo_label_string
@@ -112,33 +83,6 @@ imported via `use_repo`. The extension is marked as reproducible if supported by
11283
version of Bazel and thus doesn't result in a lockfile entry.
11384

11485

115-
<a id="modules.repo_name"></a>
116-
117-
## modules.repo_name
118-
119-
<pre>
120-
modules.repo_name(<a href="#modules.repo_name-label_or_name">label_or_name</a>)
121-
</pre>
122-
123-
Utility to provide Label compatibility with Bazel 5.
124-
125-
Under Bazel 5, calls `Label.workspace_name`. Otherwise calls
126-
`Label.repo_name`.
127-
128-
129-
**PARAMETERS**
130-
131-
132-
| Name | Description | Default Value |
133-
| :------------- | :------------- | :------------- |
134-
| <a id="modules.repo_name-label_or_name"></a>label_or_name | a Label or repository name string | none |
135-
136-
**RETURNS**
137-
138-
The repository name returned directly from the Label API, or the
139-
original string if not a Label
140-
141-
14286
<a id="modules.use_all_repos"></a>
14387

14488
## modules.use_all_repos

lib/modules.bzl

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,9 @@ def _use_all_repos(module_ctx, reproducible = False):
114114
**extension_metadata_kwargs
115115
)
116116

117-
def _repo_name(label_or_name):
118-
"""Utility to provide Label compatibility with Bazel 5.
119-
120-
Under Bazel 5, calls `Label.workspace_name`. Otherwise calls
121-
`Label.repo_name`.
122-
123-
Args:
124-
label_or_name: a Label or repository name string
125-
126-
Returns:
127-
The repository name returned directly from the Label API, or the
128-
original string if not a Label
129-
"""
130-
if hasattr(label_or_name, "repo_name"):
131-
return label_or_name.repo_name
132-
133-
return getattr(label_or_name, "workspace_name", label_or_name)
117+
_repo_attr = (
118+
"repo_name" if hasattr(Label("//:all"), "repo_name") else "workspace_name"
119+
)
134120

135121
def _apparent_repo_name(label_or_name):
136122
"""Return a repository's apparent repository name.
@@ -141,7 +127,7 @@ def _apparent_repo_name(label_or_name):
141127
Returns:
142128
The apparent repository name
143129
"""
144-
repo_name = _repo_name(label_or_name).lstrip("@")
130+
repo_name = getattr(label_or_name, _repo_attr, label_or_name).lstrip("@")
145131
delimiter_indices = []
146132

147133
# Bazed on this pattern from the Bazel source:
@@ -171,44 +157,16 @@ def _apparent_repo_label_string(label):
171157
str(label) with its canonical repository name replaced with its apparent
172158
repository name
173159
"""
174-
repo_name = _repo_name(label)
160+
repo_name = getattr(label, _repo_attr).lstrip("@")
175161
if len(repo_name) == 0:
176162
return str(label)
177163

178164
label_str = "@" + str(label).lstrip("@")
179165
return label_str.replace(repo_name, _apparent_repo_name(label))
180166

181-
_is_bzlmod_enabled = str(Label("//:all")).startswith("@@")
182-
183-
_main_repo_prefix = "@@//" if _is_bzlmod_enabled else "@//"
184-
185-
def _adjust_main_repo_prefix(target_pattern):
186-
"""Updates the main repository prefix to match the current Bazel version.
187-
188-
The main repo prefix will be "@//" for Bazel < 7.1.0, and "@@//" for Bazel
189-
>= 7.1.0 under Bzlmod. This macro automatically updates strings representing
190-
include/exclude target patterns so that they match actual main repository
191-
target Labels correctly.
192-
193-
Args:
194-
target_pattern: a string used to match a BUILD target pattern
195-
196-
Returns:
197-
the string with any main repository prefix updated to match the current
198-
Bazel version
199-
"""
200-
if target_pattern.startswith("@//") or target_pattern.startswith("@@//"):
201-
return _main_repo_prefix + target_pattern.lstrip("@/")
202-
203-
return target_pattern
204-
205167
modules = struct(
206168
as_extension = _as_extension,
207169
use_all_repos = _use_all_repos,
208-
repo_name = _repo_name,
209170
apparent_repo_name = _apparent_repo_name,
210171
apparent_repo_label_string = _apparent_repo_label_string,
211-
is_bzlmod_enabled = _is_bzlmod_enabled,
212-
main_repo_prefix = _main_repo_prefix,
213-
adjust_main_repo_prefix = _adjust_main_repo_prefix,
214172
)

tests/modules_test.bzl

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ load("//lib:modules.bzl", "modules")
1818
load("//lib:unittest.bzl", "asserts", "unittest")
1919
load("//rules:build_test.bzl", "build_test")
2020

21+
_is_bzlmod_enabled = str(Label("//tests:module_tests.bzl")).startswith("@@")
22+
2123
def _repo_rule_impl(repository_ctx):
2224
repository_ctx.file("WORKSPACE")
2325
repository_ctx.file("BUILD", """exports_files(["hello"])""")
@@ -73,10 +75,15 @@ def _apparent_repo_name_test(ctx):
7375
msg = "Return the original name without `@` if already apparent.",
7476
)
7577

78+
foo_label = Label("@foo")
79+
foo_canonical_name = getattr(
80+
foo_label,
81+
"repo_name" if hasattr(foo_label, "repo_name") else "workspace_name",
82+
)
7683
asserts.equals(
7784
env,
7885
"foo",
79-
modules.apparent_repo_name(modules.repo_name(Label("@foo"))),
86+
modules.apparent_repo_name(foo_canonical_name),
8087
msg = "Return the apparent name from a canonical name string.",
8188
)
8289

@@ -117,7 +124,7 @@ def _apparent_repo_name_test(ctx):
117124

118125
asserts.equals(
119126
env,
120-
"stardoc" if modules.is_bzlmod_enabled else "io_bazel_stardoc",
127+
"stardoc" if _is_bzlmod_enabled else "io_bazel_stardoc",
121128
modules.apparent_repo_name(Label("@io_bazel_stardoc")),
122129
msg = " ".join([
123130
"Label values will already map bazel_dep repo_names to",
@@ -170,7 +177,7 @@ def _apparent_repo_label_string_test(ctx):
170177
asserts.equals(
171178
env,
172179
"@%s//:all" % (
173-
"stardoc" if modules.is_bzlmod_enabled else "io_bazel_stardoc"
180+
"stardoc" if _is_bzlmod_enabled else "io_bazel_stardoc"
174181
),
175182
modules.apparent_repo_label_string(Label("@io_bazel_stardoc//:all")),
176183
msg = " ".join([
@@ -185,39 +192,6 @@ apparent_repo_label_string_test = unittest.make(
185192
_apparent_repo_label_string_test,
186193
)
187194

188-
def _adjust_main_repo_prefix_test(ctx):
189-
"""Unit tests for modules.apparent_repo_label_string."""
190-
env = unittest.begin(ctx)
191-
192-
expected = modules.main_repo_prefix + ":all"
193-
asserts.equals(
194-
env,
195-
expected,
196-
modules.adjust_main_repo_prefix("@//:all"),
197-
msg = "Normalizes a target pattern starting with `@//`.",
198-
)
199-
200-
asserts.equals(
201-
env,
202-
expected,
203-
modules.adjust_main_repo_prefix("@@//:all"),
204-
msg = "Normalizes a target pattern starting with `@@//`.",
205-
)
206-
207-
original = "@not_the_main_repo"
208-
asserts.equals(
209-
env,
210-
original,
211-
modules.adjust_main_repo_prefix(original),
212-
msg = "Returns non main repo target patterns unchanged.",
213-
)
214-
215-
return unittest.end(env)
216-
217-
adjust_main_repo_prefix_test = unittest.make(
218-
_adjust_main_repo_prefix_test,
219-
)
220-
221195
# buildifier: disable=unnamed-macro
222196
def modules_test_suite():
223197
"""Creates the tests for modules.bzl if Bzlmod is enabled."""
@@ -226,10 +200,9 @@ def modules_test_suite():
226200
"modules_tests",
227201
apparent_repo_name_test,
228202
apparent_repo_label_string_test,
229-
adjust_main_repo_prefix_test,
230203
)
231204

232-
if not modules.is_bzlmod_enabled:
205+
if not _is_bzlmod_enabled:
233206
return
234207

235208
build_test(

0 commit comments

Comments
 (0)