Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: stop using some deprecated Starlark APIs #2626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gazelle/manifest/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ AllSourcesInfo = provider(fields = {"all_srcs": "All sources collected from the
_rules_python_workspace = Label("@rules_python//:WORKSPACE")

def _get_all_sources_impl(target, ctx):
is_rules_python = target.label.workspace_name == _rules_python_workspace.workspace_name
is_rules_python = target.label.repo_name == _rules_python_workspace.repo_name
if not is_rules_python:
# Avoid adding third-party dependency files to the checksum of the srcs.
return AllSourcesInfo(all_srcs = depset())
Expand Down
4 changes: 2 additions & 2 deletions python/private/pypi/multi_pip_parse.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ load("//python/private:text_util.bzl", "render")
load(":pip_repository.bzl", pip_parse = "pip_repository")

def _multi_pip_parse_impl(rctx):
rules_python = rctx.attr._rules_python_workspace.workspace_name
rules_python = rctx.attr._rules_python_workspace.repo_name
load_statements = []
install_deps_calls = []
process_requirements_calls = []
Expand Down Expand Up @@ -69,7 +69,7 @@ def _process_requirements(pkg_labels, python_version, repo_prefix):
wheel_name = Label(pkg_label).package
if not wheel_name:
# We are dealing with the cases where we don't have aliases.
workspace_name = Label(pkg_label).workspace_name
workspace_name = Label(pkg_label).repo_name
wheel_name = workspace_name[len(repo_prefix):]

_wheel_names.append(wheel_name)
Expand Down
2 changes: 1 addition & 1 deletion python/private/pypi/whl_library_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ load("//python/private:full_version.bzl", "full_version")
load(":render_pkg_aliases.bzl", "NO_MATCH_ERROR_MESSAGE_TEMPLATE")

def _whl_library_alias_impl(rctx):
rules_python = rctx.attr._rules_python_workspace.workspace_name
rules_python = rctx.attr._rules_python_workspace.repo_name
if rctx.attr.default_version:
default_repo_prefix = rctx.attr.version_map[rctx.attr.default_version]
else:
Expand Down
2 changes: 1 addition & 1 deletion python/private/pythons_hub.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _hub_build_file_content(

return _HUB_BUILD_FILE_TEMPLATE.format(
toolchains = toolchains,
rules_python = workspace_location.workspace_name,
rules_python = workspace_location.repo_name,
)

_interpreters_bzl_template = """
Expand Down
40 changes: 18 additions & 22 deletions python/private/toolchains_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ load(
load(":repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
load(":text_util.bzl", "render")

def get_repository_name(repository_workspace):
dummy_label = "//:_"
return str(repository_workspace.relative(dummy_label))[:-len(dummy_label)] or "@"

def python_toolchain_build_file_content(
prefix,
python_version,
Expand Down Expand Up @@ -90,10 +86,10 @@ def _toolchains_repo_impl(rctx):
# python_register_toolchains macro so you don't normally need to interact with
# these targets.

load("@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")
load("@@{rules_python}//python/private:py_toolchain_suite.bzl", "py_toolchain_suite")

""".format(
rules_python = rctx.attr._rules_python_workspace.workspace_name,
rules_python = rctx.attr._rules_python_workspace.repo_name,
)

toolchains = python_toolchain_build_file_content(
Expand Down Expand Up @@ -151,13 +147,13 @@ toolchain_aliases(
rctx.file("defs.bzl", content = """\
# Generated by python/private/toolchains_repo.bzl

load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
load("{rules_python}//python/private:deprecation.bzl", "with_deprecation")
load("{rules_python}//python/private:text_util.bzl", "render")
load("{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
load("{rules_python}//python:py_test.bzl", _py_test = "py_test")
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
load("@@{rules_python}//python/private:text_util.bzl", "render")
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
load(
"{rules_python}//python/entry_points:py_console_script_binary.bzl",
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
_py_console_script_binary = "py_console_script_binary",
)

Expand Down Expand Up @@ -185,7 +181,7 @@ def compile_pip_requirements(**kwargs):
""".format(
name = rctx.attr.name,
python_version = rctx.attr.python_version,
rules_python = get_repository_name(rctx.attr._rules_python_workspace),
rules_python = rctx.attr._rules_python_workspace.repo_name,
))

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

def _multi_toolchain_aliases_impl(rctx):
rules_python = rctx.attr._rules_python_workspace.workspace_name
rules_python = rctx.attr._rules_python_workspace.repo_name

for python_version, repository_name in rctx.attr.python_versions.items():
file = "{}/defs.bzl".format(python_version)
rctx.file(file, content = """\
# Generated by python/private/toolchains_repo.bzl

load("{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
load("{rules_python}//python/private:deprecation.bzl", "with_deprecation")
load("{rules_python}//python/private:text_util.bzl", "render")
load("{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
load("{rules_python}//python:py_test.bzl", _py_test = "py_test")
load("@@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it always make sense to load from the global root module context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean that there are still some other places in this file that use @rules_python instead of @@{rules_python}? Well, I thought that looked a bit funky, but wasn't sure whether that was accidental/intentional. Happy to change the code either way.

load("@@{rules_python}//python/private:deprecation.bzl", "with_deprecation")
load("@@{rules_python}//python/private:text_util.bzl", "render")
load("@@{rules_python}//python:py_binary.bzl", _py_binary = "py_binary")
load("@@{rules_python}//python:py_test.bzl", _py_test = "py_test")
load(
"{rules_python}//python/entry_points:py_console_script_binary.bzl",
"@@{rules_python}//python/entry_points:py_console_script_binary.bzl",
_py_console_script_binary = "py_console_script_binary",
)

Expand Down Expand Up @@ -343,14 +339,14 @@ def compile_pip_requirements(**kwargs):
repository_name = repository_name,
name = rctx.attr.name,
python_version = python_version,
rules_python = get_repository_name(rctx.attr._rules_python_workspace),
rules_python = rules_python,
))
rctx.file("{}/BUILD.bazel".format(python_version), "")

pip_bzl = """\
# Generated by python/private/toolchains_repo.bzl

load("@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse")
load("@@{rules_python}//python:pip.bzl", "pip_parse", _multi_pip_parse = "multi_pip_parse")

def multi_pip_parse(name, requirements_lock, **kwargs):
return _multi_pip_parse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _py_wheel_library_impl(ctx):

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