8000 feat!: generate user friendly aliases for everyone · bazel-contrib/rules_python@bddd80f · GitHub
[go: up one dir, main page]

Skip to content

Commit bddd80f

Browse files
committed
feat!: generate user friendly aliases for everyone
This is related to #814 as it is bringing user-friendly alias generation and it is doing this in a breaking way for the `bzlmod` users who are not using the `requirement` macro. The labels that they use will change from `@@repo//:{name}_pkg` to `@@repo//{name}`. I am intentionally leaving the requirement macros in the requirements.bzl unchanged as #989 might change the approach again. Since these aliases can be used by bzlmod and non-bzlmod users, we can then support the same generated `gazelle_python.yaml` for both bzlmod and non-bzlmod users.
1 parent fbe64c0 commit bddd80f

File tree

4 files changed

+27
-20
lines changed

4 files changed

+27
-20
lines changed

examples/bzlmod/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ py_library(
1515
srcs = ["__init__.py"],
1616
deps = [
1717
# You can reference libraries using plain text form or the 'requirement' macro
18-
"@pip//:pylint_pkg",
18+
"@pip//pylint",
1919
requirement("tabulate"),
2020
requirement("python-dateutil"),
2121
],

python/pip_install/pip_repository.bzl

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,36 +264,45 @@ def _bzlmod_pkg_aliases(rctx, requirements_txt):
264264
"""
265265
requirements = parse_requirements(rctx.read(requirements_txt)).requirements
266266

267-
build_content = ""
268267
for requirement in requirements:
269-
build_content += """\
268+
name = _clean_pkg_name(requirement[0])
269+
build_content = """
270+
alias(
271+
name = "{name}",
272+
actual = ":pkg",
273+
visibility = ["//visibility:public"],
274+
)
270275
271276
alias(
272-
name = "{name}_pkg",
277+
name = "pkg",
273278
actual = "@{repo_prefix}{dep}//:pkg",
279+
visibility = ["//visibility:public"],
274280
)
275281
276282
alias(
277-
name = "{name}_whl",
283+
name = "whl",
278284
actual = "@{repo_prefix}{dep}//:whl",
285+
visibility = ["//visibility:public"],
279286
)
280287
281288
alias(
282-
name = "{name}_data",
289+
name = "data",
283290
actual = "@{repo_prefix}{dep}//:data",
291+
visibility = ["//visibility:public"],
284292
)
285293
286294
alias(
287-
name = "{name}_dist_info",
295+
name = "dist_info",
288296
actual = "@{repo_prefix}{dep}//:dist_info",
297+
visibility = ["//visibility:public"],
289298
)
290299
""".format(
291-
name = _clean_pkg_name(requirement[0]),
300+
name = name,
292301
repo_prefix = rctx.attr.repo_prefix,
293-
dep = _clean_pkg_name(requirement[0]),
302+
dep = name,
294303
)
295304

296-
return build_content
305+
rctx.file("{}/BUILD.bazel".format(name), build_content)
297306

298307
def _pip_repository_impl(rctx):
299308
python_interpreter = _resolve_python_interpreter(rctx)
@@ -345,12 +354,10 @@ def _pip_repository_impl(rctx):
345354
fail("rules_python failed: %s (%s)" % (result.stdout, result.stderr))
346355

347356
# We need a BUILD file to load the generated requirements.bzl
348-
build_contents = _BUILD_FILE_CONTENTS
349-
350-
if rctx.attr.bzlmod:
351-
build_contents += _bzlmod_pkg_aliases(rctx, requirements_txt)
357+
rctx.file("BUILD.bazel", _BUILD_FILE_CONTENTS + "\n# The requirements.bzl file was generated by running:\n# " + " ".join([str(a) for a in args]))
352358

353-
rctx.file("BUILD.bazel", build_contents + "\n# The requirements.bzl file was generated by running:\n# " + " ".join([str(a) for a in args]))
359+
# Generate aliases for better usability
360+
_bzlmod_pkg_aliases(rctx, requirements_txt)
354361

355362
return
356363

python/pip_install/tools/lib/bazel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def _sanitized_label(
2929
whl_name: str, repo_prefix: str, label: str, bzlmod: bool = False
3030
) -> str:
3131
if bzlmod:
32-
return '"@{}//:{}_{}"'.format(
32+
return '"@{}//{}:{}"'.format(
3333
repo_prefix[:-1],
3434
sanitise_name(whl_name, prefix=""),
3535
label,

python/pip_install/tools/lock_file_generator/lock_file_generator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,22 +165,22 @@ def _clean_name(name):
165165
166166
def requirement(name):
167167
if _bzlmod:
168-
return "@@{repo}//:" + _clean_name(name) + "_{py_library_label}"
168+
return "@@{repo}//" + _clean_name(name) + ":{py_library_label}"
169169
return "@{repo_prefix}" + _clean_name(name) + "//:{py_library_label}"
170170
171171
def whl_requirement(name):
172172
if _bzlmod:
173-
return "@@{repo}//:" + _clean_name(name) + "_{wheel_file_label}"
173+
return "@@{repo}//" + _clean_name(name) + ":{wheel_file_label}"
174174
return "@{repo_prefix}" + _clean_name(name) + "//:{wheel_file_label}"
175175
176176
def data_requirement(name):
177177
if _bzlmod:
178-
return "@@{repo}//:" + _clean_name(name) + "_{data_label}"
178+
return "@@{repo}//" + _clean_name(name) + ":{data_label}"
179179
return "@{repo_prefix}" + _clean_name(name) + "//:{data_label}"
180180
181181
def dist_info_requirement(name):
182182
if _bzlmod:
183-
return "@@{repo}//:" + _clean_name(name) + "_{dist_info_label}"
183+
return "@@{repo}//" + _clean_name(name) + ":{dist_info_label}"
184184
return "@{repo_prefix}" + _clean_name(name) + "//:{dist_info_label}"
185185
186186
def entry_point(pkg, script = None):

0 commit comments

Comments
 (0)
0