Skip to content

Commit 2893d85

Browse files
authored
fix: Propagate testonly et al for wheel .dist targets (#1064)
* Propagate testonly et al for wheel `.dist` targets The `.dist` target depends on the wheel, so it must copy the `testonly` setting as well as some others. * Also adds a utility function to do this, since the multi-version rules also do this copying. Fixes #1057 * fixup! Allow building with unreleased Bazel versions. (#1063)
1 parent 767b050 commit 2893d85

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

docs/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ bzl_library(
8484
"//python/private:py_package.bzl",
8585
"//python/private:py_wheel.bzl",
8686
"//python/private:stamp.bzl",
87+
"//python/private:util.bzl",
8788
],
8889
)
8990

examples/wheel/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
1516
load("//examples/wheel/private:wheel_utils.bzl", "directory_writer")
1617
load("//python:defs.bzl", "py_library", "py_test")
1718
load("//python:packaging.bzl", "py_package", "py_wheel")
@@ -50,6 +51,7 @@ directory_writer(
5051
# Package just a specific py_libraries, without their dependencies
5152
py_wheel(
5253
name = "minimal_with_py_library",
54+
testonly = True, # Set this to verify the generated .dist target doesn't break things
5355
# Package data. We're building "example_minimal_library-0.0.1-py3-none-any.whl"
5456
distribution = "example_minimal_library",
5557
python_tag = "py3",
@@ -60,6 +62,11 @@ py_wheel(
6062
],
6163
)
6264

65+
build_test(
66+
name = "dist_build_tests",
67+
targets = [":minimal_with_py_library.dist"],
68+
)
69+
6370
# Package just a specific py_libraries, without their dependencies
6471
py_wheel(
6572
name = "minimal_with_py_library_with_stamp",

python/packaging.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
load("//python/private:py_package.bzl", "py_package_lib")
1818
load("//python/private:py_wheel.bzl", _PyWheelInfo = "PyWheelInfo", _py_wheel = "py_wheel")
19+
load("//python/private:util.bzl", "copy_propagating_kwargs")
1920

2021
# Re-export as public API
2122
PyWheelInfo = _PyWheelInfo
@@ -148,6 +149,7 @@ def py_wheel(name, twine = None, **kwargs):
148149
name = _dist_target,
149150
wheel = name,
150151
out = kwargs.pop("dist_folder", "{}_dist".format(name)),
152+
**copy_propagating_kwargs(kwargs)
151153
)
152154

153155
_py_wheel(name = name, **kwargs)

python/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ exports_files(
4141
"py_wheel.bzl",
4242
"reexports.bzl",
4343
"stamp.bzl",
44+
"util.bzl",
4445
],
4546
visibility = ["//docs:__pkg__"],
4647
)

python/private/util.bzl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Functionality shared by multiple pieces of code."""
2+
3+
def copy_propagating_kwargs(from_kwargs, into_kwargs = None):
4+
"""Copies args that must be compatible between two targets with a dependency relationship.
5+
6+
This is intended for when one target depends on another, so they must have
7+
compatible settings such as `testonly` and `compatible_with`. This usually
8+
happens when a macro generates multiple targets, some of which depend
9+
on one another, so their settings must be compatible.
10+
11+
Args:
12+
from_kwargs: keyword args dict whose common kwarg will be copied.
13+
into_kwargs: optional keyword args dict that the values from `from_kwargs`
14+
will be copied into. The values in this dict will take precedence
15+
over the ones in `from_kwargs` (i.e., if this has `testonly` already
16+
set, then it won't be overwritten).
17+
NOTE: THIS WILL BE MODIFIED IN-PLACE.
18+
19+
Returns:
20+
Keyword args to use for the depender target derived from the dependency
21+
target. If `into_kwargs` was passed in, then that same object is
22+
returned; this is to facilitate easy `**` expansion.
23+
"""
24+
if into_kwargs == None:
25+
into_kwargs = {}
26+
27+
# Include tags because people generally expect tags to propagate.
28+
for attr in ("testonly", "tags", "compatible_with", "restricted_to"):
29+
if attr in from_kwargs and attr not in into_kwargs:
30+
into_kwargs[attr] = from_kwargs[attr]
31+
return into_kwargs

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy