Skip to content

Commit 03854a2

Browse files
authored
refactor: don't load repo-phase objects from build-phase (bazel-contrib#2056)
As a general practice, the repo-phase and build-phase shouldn't load code from one another because they can't use each other's objects. It can also result in confusing behavior because the "starlark environment" is slightly different between the two phases. Additionally, Google's version of Bazel essentially disables repo-phase objects, so loading e.g. http_archive results in errors. This makes it more difficult to import rules_python into Google, as we have to maintain patches to cut out the code (and thus we spend more time trying to import the code than working on it).
1 parent 04f5798 commit 03854a2

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

python/private/pypi/deps.bzl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,7 @@ py_library(
123123
"""
124124

125125
# Collate all the repository names so they can be easily consumed
126-
all_requirements = [name for (name, _, _) in _RULE_DEPS]
127-
128-
def requirement(pkg):
129-
return Label("@pypi__" + pkg + "//:lib")
126+
all_repo_names = [name for (name, _, _) in _RULE_DEPS]
130127

131128
def pypi_deps():
132129
"""

python/private/pypi/pip_compile.bzl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ make it possible to have multiple tools inside the `pypi` directory
2020
"""
2121

2222
load("//python:defs.bzl", _py_binary = "py_binary", _py_test = "py_test")
23-
load(":deps.bzl", "requirement")
2423

2524
def pip_compile(
2625
name,
@@ -115,19 +114,19 @@ def pip_compile(
115114
args.extend(extra_args)
116115

117116
deps = [
118-
requirement("build"),
119-
requirement("click"),
120-
requirement("colorama"),
121-
requirement("importlib_metadata"),
122-
requirement("more_itertools"),
123-
requirement("packaging"),
124-
requirement("pep517"),
125-
requirement("pip"),
126-
requirement("pip_tools"),
127-
requirement("pyproject_hooks"),
128-
requirement("setuptools"),
129-
requirement("tomli"),
130-
requirement("zipp"),
117+
Label("@pypi__build//:lib"),
118+
Label("@pypi__click//:lib"),
119+
Label("@pypi__colorama//:lib"),
120+
Label("@pypi__importlib_metadata//:lib"),
121+
Label("@pypi__more_itertools//:lib"),
122+
Label("@pypi__packaging//:lib"),
123+
Label("@pypi__pep517//:lib"),
124+
Label("@pypi__pip//:lib"),
125+
Label("@pypi__pip_tools//:lib"),
126+
Label("@pypi__pyproject_hooks//:lib"),
127+
Label("@pypi__setuptools//:lib"),
128+
Label("@pypi__tomli//:lib"),
129+
Label("@pypi__zipp//:lib"),
131130
Label("//python/runfiles:runfiles"),
132131
] + extra_deps
133132

python/private/pypi/whl_installer/BUILD.bazel

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
load("//python:defs.bzl", "py_binary", "py_library")
2-
load("//python/private/pypi:deps.bzl", "requirement")
32

43
py_library(
54
name = "lib",
@@ -10,14 +9,13 @@ py_library(
109
"wheel_installer.py",
1110
],
1211
visibility = [
13-
"//tests:__subpackages__",
14-
"//third_party/rules_pycross/pycross/private:__subpackages__",
12+
"//:__subpackages__",
1513
],
1614
deps = [
17-
requirement("installer"),
18-
requirement("pip"),
19-
requirement("packaging"),
20-
requirement("setuptools"),
15+
"@pypi__installer//:lib",
16+
"@pypi__packaging//:lib",
17+
"@pypi__pip//:lib",
18+
"@pypi__setuptools//:lib",
2119
],
2220
)
2321

@@ -32,5 +30,5 @@ py_binary(
3230
filegroup(
3331
name = "distribution",
3432
srcs = glob(["*"]),
35-
visibility = ["//python/private/pypi:__subpackages__"],
33+
visibility = ["//:__subpackages__"],
3634
)

python/private/pypi/whl_library.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ load("//python/private:envsubst.bzl", "envsubst")
2121
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
2222
load("//python/private:toolchains_repo.bzl", "get_host_os_arch")
2323
load(":attrs.bzl", "ATTRS", "use_isolated")
24-
load(":deps.bzl", "all_requirements")
24+
load(":deps.bzl", "all_repo_names")
2525
load(":generate_whl_library_build_bazel.bzl", "generate_whl_library_build_bazel")
2626
load(":parse_whl_name.bzl", "parse_whl_name")
2727
load(":patch_whl.bzl", "patch_whl")
@@ -490,7 +490,7 @@ attr makes `extra_pip_args` and `download_only` ignored.""",
490490
] + [
491491
# Includes all the external dependencies from repositories.bzl
492492
Label("@" + repo + "//:BUILD.bazel")
493-
for repo in all_requirements
493+
for repo in all_repo_names
494494
],
495495
),
496496
}, **ATTRS)

0 commit comments

Comments
 (0)