Skip to content

Commit

Permalink
chore: load specific bzl files instead of generic defs.bzl (#2483)
Browse files Browse the repository at this point in the history
Update code and examples to load the object-specific bzl files instead
of the
generic `defs.bzl`. This is mostly for code hygiene, but came out of
trying to diagnose
why Bazel 9 workspace builds kept erroing with defs.bzl somehow related.
Removing
the internal usages of defs.bzl doesn't seem to fully fix it, but does
seem to eliminate
some errors, make some progress, and narrow down what's going on.

Work towards #2469
  • Loading branch information
rickeylev authored Dec 7, 2024
1 parent 0fb4ce1 commit ca98773
Show file tree
Hide file tree
Showing 42 changed files with 58 additions and 48 deletions.
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Once you've imported the rule set using either Bzlmod or WORKSPACE, you can then
load the core rules in your `BUILD` files with the following:

```starlark
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python:py_binary.bzl", "py_binary")

py_binary(
name = "main",
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ components have examples in the {gh-path}`examples` directory.
The core rules are currently available in Bazel as built-in symbols, but this
form is deprecated. Instead, you should depend on rules_python in your
`WORKSPACE` or `MODULE.bazel` file and load the Python rules from
`@rules_python//python:defs.bzl` or load paths described in the API documentation.
`@rules_python//python:<name>.bzl` or load paths described in the API documentation.

A [buildifier](https://github.com/bazelbuild/buildtools/blob/master/buildifier/README.md)
fix is available to automatically migrate `BUILD` and `.bzl` files to add the
Expand Down
4 changes: 3 additions & 1 deletion examples/build_file_generation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
# ruleset. When the symbol is loaded you can use the rule.
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_library.bzl", "py_library")
load("@rules_python//python:py_test.bzl", "py_test")
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_python//python:defs.bzl", "py_library", "py_test")
load("@rules_python//python:py_library.bzl", "py_library")
load("@rules_python//python:py_test.bzl", "py_test")

py_library(
name = "random_number_generator",
Expand Down
4 changes: 3 additions & 1 deletion examples/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@pip//:requirements.bzl", "all_data_requirements", "all_requirements", "all_whl_requirements", "requirement")
load("@python_3_9//:defs.bzl", py_test_with_transition = "py_test")
load("@python_versions//3.10:defs.bzl", compile_pip_requirements_3_10 = "compile_pip_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_library.bzl", "py_library")
load("@rules_python//python:py_test.bzl", "py_test")

# This stanza calls a rule that generates targets for managing pip dependencies
# with pip-compile for a particular python version.
Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod/entry_points/tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//rules:run_binary.bzl", "run_binary")
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:py_test.bzl", "py_test")

# Below are targets for testing the `py_console_script_binary` feature and are
# not part of the example how to use the feature.
Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod/libs/my_lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@pip//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_library.bzl", "py_library")

py_library(
name = "my_lib",
Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod/other_module/other_module/pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ load(
"@python_3_11//:defs.bzl",
py_binary_311 = "py_binary",
)
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_library.bzl", "py_library")

py_library(
name = "lib",
Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:py_test.bzl", "py_test")

py_test(
name = "runfiles_test",
Expand Down
3 changes: 2 additions & 1 deletion examples/bzlmod/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ load("@python_versions//3.10:defs.bzl", py_binary_3_10 = "py_binary", py_test_3_
load("@python_versions//3.11:defs.bzl", py_binary_3_11 = "py_binary", py_test_3_11 = "py_test")
load("@python_versions//3.9:defs.bzl", py_binary_3_9 = "py_binary", py_test_3_9 = "py_test")
load("@pythons_hub//:versions.bzl", "MINOR_MAPPING")
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_test.bzl", "py_test")
load("@rules_python//python/config_settings:transition.bzl", py_versioned_binary = "py_binary", py_versioned_test = "py_test")
load("@rules_shell//shell:sh_test.bzl", "sh_test")

Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod/whl_mods/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:py_test.bzl", "py_test")

exports_files(
glob(["data/**"]),
Expand Down
4 changes: 3 additions & 1 deletion examples/bzlmod_build_file_generation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
# requirements.
load("@bazel_gazelle//:def.bzl", "gazelle")
load("@pip//:requirements.bzl", "all_whl_requirements")
load("@rules_python//python:defs.bzl", "py_binary", "py_library", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_library.bzl", "py_library")
load("@rules_python//python:py_test.bzl", "py_test")
load("@rules_python_gazelle_plugin//manifest:defs.bzl", "gazelle_python_manifest")
load("@rules_python_gazelle_plugin//modules_mapping:def.bzl", "modules_mapping")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_library.bzl", "py_library")

py_library(
name = "lib",
Expand Down
2 changes: 1 addition & 1 deletion examples/bzlmod_build_file_generation/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:py_test.bzl", "py_test")

# gazelle:ignore
py_test(
Expand Down
2 changes: 1 addition & 1 deletion examples/multi_python_versions/libs/my_lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@pypi//:requirements.bzl", "requirement")
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_library.bzl", "py_library")

py_library(
name = "my_lib",
Expand Down
3 changes: 2 additions & 1 deletion examples/multi_python_versions/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ load("@python//3.11:defs.bzl", py_binary_3_11 = "py_binary", py_test_3_11 = "py_
load("@python//3.8:defs.bzl", py_binary_3_8 = "py_binary", py_test_3_8 = "py_test")
load("@python//3.9:defs.bzl", py_binary_3_9 = "py_binary", py_test_3_9 = "py_test")
load("@pythons_hub//:versions.bzl", "MINOR_MAPPING", "PYTHON_VERSIONS")
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_test.bzl", "py_test")
load("@rules_python//python:versions.bzl", DEFAULT_MINOR_MAPPING = "MINOR_MAPPING", DEFAULT_TOOL_VERSIONS = "TOOL_VERSIONS")
load("@rules_python//python/private:text_util.bzl", "render") # buildifier: disable=bzl-visibility
load("@rules_shell//shell:sh_test.bzl", "sh_test")
Expand Down
5 changes: 3 additions & 2 deletions examples/pip_parse/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
load("@rules_python//python:defs.bzl", "py_binary", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_test.bzl", "py_test")
load("@rules_python//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")

# Toolchain setup, this is optional.
# Demonstrate that we can use the same python interpreter for the toolchain and executing pip in pip install (see WORKSPACE).
#
#load("@rules_python//python:defs.bzl", "py_runtime_pair")
#load("@rules_python//python:py_runtime_pair.bzl", "py_runtime_pair")
#
#py_runtime(
# name = "python3_runtime",
Expand Down
2 changes: 1 addition & 1 deletion examples/pip_parse_vendored/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@bazel_skylib//rules:diff_test.bzl", "diff_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:py_test.bzl", "py_test")
load("//:requirements.bzl", "all_data_requirements", "all_requirements", "all_whl_requirements", "requirement")

# This rule adds a convenient way to update the requirements.txt
Expand Down
2 changes: 1 addition & 1 deletion examples/pip_repository_annotations/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
load("@rules_python//python:py_test.bzl", "py_test")

exports_files(
glob(["data/**"]),
Expand Down
2 changes: 1 addition & 1 deletion examples/py_proto_library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_python//python:defs.bzl", "py_test")
load("@rules_python//python:py_test.bzl", "py_test")

py_test(
name = "pricetag_test",
Expand Down
3 changes: 2 additions & 1 deletion examples/wheel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
load("@bazel_skylib//rules:build_test.bzl", "build_test")
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("//examples/wheel/private:wheel_utils.bzl", "directory_writer", "make_variable_tags")
load("//python:defs.bzl", "py_library", "py_test")
load("//python:packaging.bzl", "py_package", "py_wheel")
load("//python:pip.bzl", "compile_pip_requirements")
load("//python:py_library.bzl", "py_library")
load("//python:py_test.bzl", "py_test")
load("//python:versions.bzl", "gen_python_config_settings")
load("//python/entry_points:py_console_script_binary.bzl", "py_console_script_binary")
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") # buildifier: disable=bzl-visibility
Expand Down
2 changes: 1 addition & 1 deletion examples/wheel/lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("//python:defs.bzl", "py_library")
load("//python:py_library.bzl", "py_library")

package(default_visibility = ["//visibility:public"])

Expand Down
2 changes: 1 addition & 1 deletion examples/wheel/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@rules_python//python:defs.bzl", "py_binary")
load("@rules_python//python:py_binary.bzl", "py_binary")

py_binary(
name = "directory_writer",
Expand Down
2 changes: 1 addition & 1 deletion python/private/proto/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bzl_library(
srcs = ["py_proto_library.bzl"],
visibility = ["//python:__pkg__"],
deps = [
"//python:defs_bzl",
"//python:py_info_bzl",
"@rules_proto//proto:defs",
],
)
Expand Down
2 changes: 1 addition & 1 deletion python/private/proto/py_proto_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"""The implementation of the `py_proto_library` rule and its aspect."""

load("@rules_proto//proto:defs.bzl", "ProtoInfo", "proto_common")
load("//python:defs.bzl", "PyInfo")
load("//python:py_info.bzl", "PyInfo")
load("//python/api:api.bzl", _py_common = "py_common")

PY_PROTO_TOOLCHAIN = "@rules_python//python/proto:toolchain_type"
Expand Down
2 changes: 1 addition & 1 deletion python/private/pypi/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ _RULE_DEPS = [
_GENERIC_WHEEL = """\
package(default_visibility = ["//visibility:public"])
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_library.bzl", "py_library")
load("@rules_python//python/private:glob_excludes.bzl", "glob_excludes")
py_library(
Expand Down
2 changes: 1 addition & 1 deletion python/private/pypi/generate_group_library_build_bazel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ load(
)

_PRELUDE = """\
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_library.bzl", "py_library")
"""

_GROUP_TEMPLATE = """\
Expand Down
3 changes: 2 additions & 1 deletion python/private/pypi/whl_installer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("//python:defs.bzl", "py_binary", "py_library")
load("//python:py_binary.bzl", "py_binary")
load("//python:py_library.bzl", "py_library")

py_library(
name = "lib",
Expand Down
2 changes: 1 addition & 1 deletion python/private/whl_filegroup/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//python:defs.bzl", "py_binary")
load("//python:py_binary.bzl", "py_binary")

filegroup(
name = "distribution",
Expand Down
6 changes: 1 addition & 5 deletions python/python.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@

"""Re-exports for some of the core Bazel Python rules.
This file is deprecated; please use the exports in defs.bzl instead. This is to
follow the new naming convention of putting core rules for a language
underneath @rules_<LANG>//<LANG>:defs.bzl. The exports in this file will be
disallowed in a future Bazel release by
`--incompatible_load_python_rules_from_bzl`.
This file is deprecated; please use the exports in `<name>.bzl` files instead.
"""

def py_library(*args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion python/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("//python:defs.bzl", "py_library")
load("//python:packaging.bzl", "py_wheel")
load("//python:py_library.bzl", "py_library")
load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")

filegroup(
Expand Down
2 changes: 1 addition & 1 deletion tests/base_rules/base_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", "PREVENT_IMPLICIT_BUILDING_TAGS", rt_util = "util")
load("//python:defs.bzl", "PyInfo")
load("//python:py_info.bzl", "PyInfo")
load("//python/private:reexports.bzl", "BuiltinPyInfo") # buildifier: disable=bzl-visibility
load("//tests/base_rules:util.bzl", pt_util = "util")
load("//tests/support:py_info_subject.bzl", "py_info_subject")
Expand Down
2 changes: 1 addition & 1 deletion tests/base_rules/py_binary/py_binary_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.
"""Tests for py_binary."""

load("//python:defs.bzl", "py_binary")
load("//python:py_binary.bzl", "py_binary")
load(
"//tests/base_rules:py_executable_base_tests.bzl",
"create_executable_tests",
Expand Down
3 changes: 2 additions & 1 deletion tests/base_rules/py_library/py_library_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:truth.bzl", "matching")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:defs.bzl", "PyRuntimeInfo", "py_library")
load("//python:py_library.bzl", "py_library")
load("//python:py_runtime_info.bzl", "PyRuntimeInfo")
load("//tests/base_rules:base_tests.bzl", "create_base_tests")
load("//tests/base_rules:util.bzl", pt_util = "util")

Expand Down
2 changes: 1 addition & 1 deletion tests/base_rules/py_test/py_test_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:defs.bzl", "py_test")
load("//python:py_test.bzl", "py_test")
load(
"//tests/base_rules:py_executable_base_tests.bzl",
"create_executable_tests",
Expand Down
2 changes: 1 addition & 1 deletion tests/load_from_macro/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("//python:defs.bzl", "py_library")
load("//python:py_library.bzl", "py_library")
load(":tags.bzl", "TAGS")

licenses(["notice"])
Expand Down
2 changes: 1 addition & 1 deletion tests/pycross/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("//python:defs.bzl", "py_test")
load("//python:py_test.bzl", "py_test")
load("//third_party/rules_pycross/pycross/private:wheel_library.bzl", "py_wheel_library") # buildifier: disable=bzl-visibility

py_wheel_library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ _tests = []

def _test_simple(env):
want = """\
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_library.bzl", "py_library")
## Group vbap
Expand Down Expand Up @@ -62,7 +62,7 @@ _tests.append(_test_simple)

def _test_in_hub(env):
want = """\
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_python//python:py_library.bzl", "py_library")
## Group vbap
Expand Down
2 changes: 1 addition & 1 deletion tests/pypi/whl_installer/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("//python:defs.bzl", "py_test")
load("//python:py_test.bzl", "py_test")

alias(
name = "lib",
Expand Down
3 changes: 2 additions & 1 deletion tests/whl_filegroup/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
load("//python:defs.bzl", "py_library", "py_test")
load("//python:packaging.bzl", "py_package", "py_wheel")
load("//python:pip.bzl", "whl_filegroup")
load("//python:py_library.bzl", "py_library")
load("//python:py_test.bzl", "py_test")
load(":whl_filegroup_tests.bzl", "whl_filegroup_test_suite")

whl_filegroup_test_suite(name = "whl_filegroup_tests")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"""Implementation of the py_wheel_library rule."""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("//python:defs.bzl", "PyInfo")
load("//python:py_info.bzl", "PyInfo")
load(":providers.bzl", "PyWheelInfo")

def _py_wheel_library_impl(ctx):
Expand Down
2 changes: 1 addition & 1 deletion tools/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("//python:defs.bzl", "py_binary")
load("//python:py_binary.bzl", "py_binary")

package(default_visibility = ["//visibility:public"])

Expand Down

0 comments on commit ca98773

Please sign in to comment.