Skip to content

Commit effdce8

Browse files
authored
refactor: stop using some deprecated Starlark APIs (bazel-contrib#2626)
I am currently working on an analysis tool that is capable of parsing BUILD/*.bzl files. It currently fails to process some of the Python rules, due to the rules depending on some features that are deprecated on the Bazel side. Instead of adding implementations of these deprecated features to my brand new analysis tool, I thought I'd simply patch up the Python rules instead.
1 parent fcf7221 commit effdce8

File tree

6 files changed

+24
-28
lines changed

6 files changed

+24
-28
lines changed

gazelle/manifest/defs.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ AllSourcesInfo = provider(fields = {"all_srcs": "All sources collected from the
161161
_rules_python_workspace = Label("@rules_python//:WORKSPACE")
162162

163163
def _get_all_sources_impl(target, ctx):
164-
is_rules_python = target.label.workspace_name == _rules_python_workspace.workspace_name
164+
is_rules_python = target.label.repo_name == _rules_python_workspace.repo_name
165165
if not is_rules_python:
166166
# Avoid adding third-party dependency files to the checksum of the srcs.
167167
return AllSourcesInfo(all_srcs = depset())

python/private/pypi/multi_pip_parse.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ load("//python/private:text_util.bzl", "render")
1818
load(":pip_repository.bzl", pip_parse = "pip_repository")
1919

2020
def _multi_pip_parse_impl(rctx):
21-
rules_python = rctx.attr._rules_python_workspace.workspace_name
21+
rules_python = rctx.attr._rules_python_workspace.repo_name
2222
load_statements = []
2323
install_deps_calls = []
2424
process_requirements_calls = []
@@ -69,7 +69,7 @@ def _process_requirements(pkg_labels, python_version, repo_prefix):
6969
wheel_name = Label(pkg_label).package
7070
if not wheel_name:
7171
# We are dealing with the cases where we don't have aliases.
72-
workspace_name = Label(pkg_label).workspace_name
72+
workspace_name = Label(pkg_label).repo_name
7373
wheel_name = workspace_name[len(repo_prefix):]
7474
7575
_wheel_names.append(wheel_name)

python/private/pypi/whl_library_alias.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ load("//python/private:full_version.bzl", "full_version")
1818
load(":render_pkg_aliases.bzl", "NO_MATCH_ERROR_MESSAGE_TEMPLATE")
1919

2020
def _whl_library_alias_impl(rctx):
21-
rules_python = rctx.attr._rules_python_workspace.workspace_name
21+
rules_python = rctx.attr._rules_python_workspace.repo_name
2222
if rctx.attr.default_version:
2323
default_repo_prefix = rctx.attr.version_map[rctx.attr.default_version]
2424
else:

python/private/pythons_hub.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _hub_build_file_content(
7979

8080
return _HUB_BUILD_FILE_TEMPLATE.format(
8181
toolchains = toolchains,
82-
rules_python = workspace_location.workspace_name,
82+
rules_python = workspace_location.repo_name,
8383
)
8484

8585
_interpreters_bzl_template = """

python/private/toolchains_repo.bzl

+18-22
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ load(
3131
load(":repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
3232
load(":text_util.bzl", "render")
3333

34-
def get_repository_name(repository_workspace):
35-
dummy_label = "//:_"
36-
return str(repository_workspace.relative(dummy_label))[:-len(dummy_label)] or "@"
37-
3834
def python_toolchain_build_file_content(
3935
prefix,
4036
python_version,
@@ -90,10 +86,10 @@ def _toolchains_repo_impl(rctx):
9086
# python_register_toolchains macro so you don't normally need to interact with
9187
# these targets.
9288
93-
load("@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")
89+
load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")
9490
9591
""".format(
96-
rules_python = rctx.attr._rules_python_workspace.workspace_name,
92+
rules_python = rctx.attr._rules_python_workspace.repo_name,
9793
)
9894

9995
toolchains = python_toolchain_build_file_content(
@@ -151,13 +147,13 @@ toolchain_aliases(
151147
rctx.file("defs.bzl", content = """\
152148
# Generated by python/private/toolchains_repo.bzl
153149
154-
load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
155-
load("{rules_python}//python/private:deprecation.bzl", "with_deprecation")
156-
load("{rules_python}//python/private:text_util.bzl", "render")
157-
load("{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
158-
load("{rules_python}//python:py_test.bzl", _py_test = "py_test")
150+
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
151+
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
152+
load("@@{rules_python}//python/private:text_util.bzl", "render")
153+
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
154+
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
159155
load(
160-
"{rules_python}//python/entry_points:py_console_script_binary.bzl",
156+
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
161157
_py_console_script_binary = "py_console_script_binary",
162158
)
163159
@@ -185,7 +181,7 @@ def compile_pip_requirements(**kwargs):
185181
""".format(
186182
name = rctx.attr.name,
187183
python_version = rctx.attr.python_version,
188-
rules_python = get_repository_name(rctx.attr._rules_python_workspace),
184+
rules_python = rctx.attr._rules_python_workspace.repo_name,
189185
))
190186

191187
toolchain_aliases = repository_rule(
@@ -301,20 +297,20 @@ this repo causes an eager fetch of the toolchain for the host platform.
301297
)
302298

303299
def _multi_toolchain_aliases_impl(rctx):
304-
rules_python = rctx.attr._rules_python_workspace.workspace_name
300+
rules_python = rctx.attr._rules_python_workspace.repo_name
305301

306302
for python_version, repository_name in rctx.attr.python_versions.items():
307303
file = "{}/defs.bzl".format(python_version)
308304
rctx.file(file, content = """\
309305
# Generated by python/private/toolchains_repo.bzl
310306
311-
load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
312-
load("{rules_python}//python/private:deprecation.bzl", "with_deprecation")
313-
load("{rules_python}//python/private:text_util.bzl", "render")
314-
load("{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
315-
load("{rules_python}//python:py_test.bzl", _py_test = "py_test")
307+
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
308+
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
309+
load("@@{rules_python}//python/private:text_util.bzl", "render")
310+
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
311+
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
316312
load(
317-
"{rules_python}//python/entry_points:py_console_script_binary.bzl",
313+
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
318314
_py_console_script_binary = "py_console_script_binary",
319315
)
320316
@@ -343,14 +339,14 @@ def compile_pip_requirements(**kwargs):
343339
repository_name = repository_name,
344340
name = rctx.attr.name,
345341
python_version = python_version,
346-
rules_python = get_repository_name(rctx.attr._rules_python_workspace),
342+
rules_python = rules_python,
347343
))
348344
rctx.file("{}/BUILD.bazel".format(python_version), "")
349345

350346
pip_bzl = """\
351347
# Generated by python/private/toolchains_repo.bzl
352348
353-
load("@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse")
349+
load("@@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse")
354350
355351
def multi_pip_parse(name, requirements_lock, **kwargs):
356352
return _multi_pip_parse(

third_party/rules_pycross/pycross/private/wheel_library.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _py_wheel_library_impl(ctx):
8383

8484
# TODO: Is there a more correct way to get this runfiles-relative import path?
8585
imp = paths.join(
86-
ctx.label.workspace_name or ctx.workspace_name, # Default to the local workspace.
86+
ctx.label.repo_name or ctx.workspace_name, # Default to the local workspace.
8787
ctx.label.package,
8888
ctx.label.name,
8989
"site-packages", # we put lib files in this subdirectory.

0 commit comments

Comments
 (0)