Skip to content

Commit d9d5990

Browse files
committed
WIP: Bzlmod part the ninety-ninth
Replaced `apparent_repo_label_string` and `adjust_main_repo_prefix` based on an idea from @fmeum during his review of bazelbuild/bazel-skylib#548. Added `_expand_patterns`, which uses `native.package_relative_label` to expand the `dependency_tracking_*_deps_patterns` attributes to full, correct `Label` strings. All `test/shell/test_{strict,unused}_dependency.sh` test cases pass.
1 parent 5745c4a commit d9d5990

File tree

4 files changed

+39
-68
lines changed

4 files changed

+39
-68
lines changed

MODULE.bazel.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scala/private/macros/bzlmod.bzl

+4-59
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
"""Utilities for working with Bazel modules"""
22

3-
def _repo_name(label_or_name):
4-
"""Utility to provide Label compatibility with Bazel 5.
5-
6-
Under Bazel 5, calls `Label.workspace_name`. Otherwise calls
7-
`Label.repo_name`.
8-
9-
Args:
10-
label_or_name: a Label or repository name string
11-
12-
Returns:
13-
The repository name returned directly from the Label API, or the
14-
original string if not a Label
15-
"""
16-
if hasattr(label_or_name, "repo_name"):
17-
return label_or_name.repo_name
18-
19-
return getattr(label_or_name, "workspace_name", label_or_name)
3+
_repo_attr = (
4+
"repo_name" if hasattr(Label("//:all"), "repo_name") else "workspace_name"
5+
)
206

217
def apparent_repo_name(label_or_name):
228
"""Return a repository's apparent repository name.
@@ -30,7 +16,7 @@ def apparent_repo_name(label_or_name):
3016
Returns:
3117
The apparent repository name
3218
"""
33-
repo_name = _repo_name(label_or_name).lstrip("@")
19+
repo_name = getattr(label_or_name, _repo_attr, label_or_name).lstrip("@")
3420
delimiter_indices = []
3521

3622
# Bazed on this pattern from the Bazel source:
@@ -49,44 +35,3 @@ def apparent_repo_name(label_or_name):
4935
return repo_name[:delimiter_indices[0]]
5036

5137
return repo_name[delimiter_indices[-1] + 1:]
52-
53-
def apparent_repo_label_string(label):
54-
"""Return a Label string starting with its apparent repo name.
55-
56-
Args:
57-
label: a Label instance
58-
59-
Returns:
60-
str(label) with its canonical repository name replaced with its apparent
61-
repository name
62-
"""
63-
repo_name = _repo_name(label)
64-
if len(repo_name) == 0:
65-
return str(label)
66-
67-
label_str = "@" + str(label).lstrip("@")
68-
return label_str.replace(label.repo_name, apparent_repo_name(label))
69-
70-
_IS_BZLMOD_ENABLED = str(Label("//:all")).startswith("@@")
71-
72-
_MAIN_REPO_PREFIX = "@@//" if _IS_BZLMOD_ENABLED else "@//"
73-
74-
def adjust_main_repo_prefix(target_pattern):
75-
"""Updates the main repo prefix to match the current Bazel version.
76-
77-
The main repo prefix will be "@//" for Bazel < 7.1.0, and "@@//" for Bazel
78-
>= 7.1.0 under Bzlmod. This macro automatically updates strings representing
79-
include/exclude target patterns so that they match actual main repository
80-
target Labels correctly.
81-
82-
Args:
83-
target_pattern: a string used to match a BUILD target pattern
84-
85-
Returns:
86-
the string with any main repository prefix updated to match the current
87-
Bazel version
88-
"""
89-
if target_pattern.startswith("@//") or target_pattern.startswith("@@//"):
90-
return _MAIN_REPO_PREFIX + target_pattern.lstrip("@/")
91-
92-
return target_pattern

scala/private/phases/phase_dependency.bzl

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ load(
66
"get_strict_deps_mode",
77
"new_dependency_info",
88
)
9-
load("//scala/private:macros/bzlmod.bzl", "apparent_repo_label_string")
109
load(
1110
"//scala/private:paths.bzl",
1211
_get_files_with_extension = "get_files_with_extension",
@@ -38,7 +37,7 @@ def _phase_dependency(
3837
strict_deps_always_off):
3938
toolchain = ctx.toolchains[Label("//scala:toolchain_type")]
4039

41-
target_label = apparent_repo_label_string(ctx.label)
40+
target_label = str(ctx.label)
4241

4342
included_in_strict_deps_analysis = _is_target_included(
4443
target_label,

scala/scala_toolchain.bzl

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
load("//scala/private:macros/bzlmod.bzl", "adjust_main_repo_prefix")
21
load("//scala:providers.bzl", _DepsInfo = "DepsInfo")
32
load(
43
"@io_bazel_rules_scala_config//:config.bzl",
@@ -29,12 +28,12 @@ def _compute_dependency_tracking_method(
2928

3029
def _partition_patterns(patterns):
3130
includes = [
32-
adjust_main_repo_prefix(pattern)
31+
pattern
3332
for pattern in patterns
3433
if not pattern.startswith("-")
3534
]
3635
excludes = [
37-
adjust_main_repo_prefix(pattern.lstrip("-"))
36+
pattern.lstrip("-")
3837
for pattern in patterns
3938
if pattern.startswith("-")
4039
]
@@ -119,7 +118,7 @@ def _default_dep_providers():
119118
for p in dep_providers
120119
]
121120

122-
scala_toolchain = rule(
121+
_scala_toolchain = rule(
123122
_scala_toolchain_impl,
124123
attrs = {
125124
"scalacopts": attr.string_list(),
@@ -180,3 +179,31 @@ scala_toolchain = rule(
180179
},
181180
fragments = ["java"],
182181
)
182+
183+
def _expand_patterns(patterns):
184+
"""Expands string patterns to match actual Label values."""
185+
result = []
186+
187+
for p in patterns:
188+
exclude = p.startswith("-")
189+
p = p.lstrip("-")
190+
expanded = str(native.package_relative_label(p)) if p else ""
191+
192+
# If the original pattern doesn't contain ":", match any target
193+
# beginning with the pattern prefix.
194+
if expanded and ":" not in p:
195+
expanded = expanded[:expanded.rindex(":")]
196+
197+
result.append(("-" if exclude else "") + expanded)
198+
199+
return result
200+
201+
def scala_toolchain(**kwargs):
202+
"""Creates a Scala toolchain target."""
203+
strict = kwargs.pop("dependency_tracking_strict_deps_patterns", [""])
204+
unused = kwargs.pop("dependency_tracking_unused_deps_patterns", [""])
205+
_scala_toolchain(
206+
dependency_tracking_strict_deps_patterns = _expand_patterns(strict),
207+
dependency_tracking_unused_deps_patterns = _expand_patterns(unused),
208+
**kwargs,
209+
)

0 commit comments

Comments
 (0)