Skip to content
Draft
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: 2 additions & 0 deletions news/3858.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(compile_pip_requirements) Add the explicit `data` attribute and forward it to
the generated `py_binary`.
6 changes: 4 additions & 2 deletions python/private/pypi/pip_compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def pip_compile(
visibility = ["//visibility:private"],
tags = None,
constraints = [],
data = [],
**kwargs):
"""Generates targets for managing pip dependencies with pip-compile (piptools).

Expand Down Expand Up @@ -81,6 +82,7 @@ def pip_compile(
tags: tagging attribute common to all build rules, passed to both the _test and .update rules.
visibility: passed to both the _test and .update rules.
constraints: a list of files containing constraints to pass to pip-compile with `--constraint`.
data: A list of labels to include as part of the `data` attribute in the generated `py_binary`.
**kwargs: other bazel attributes passed to the "_test" rule.
"""
if len([x for x in [srcs, src, requirements_in] if x != None]) > 1:
Expand All @@ -100,11 +102,11 @@ def pip_compile(
# for a requirements file that does `-r ../other/requirements.txt`
native.filegroup(
name = name,
srcs = kwargs.pop("data", []) + [requirements_txt],
srcs = data + [requirements_txt],
visibility = visibility,
)

data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints
data = data + [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints
Comment on lines 103 to +109

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

To ensure defensive programming and prevent potential TypeError crashes if data is explicitly passed as None (which is a common pattern when programmatically constructing macro arguments), we should normalize data to an empty list if it is falsy/None. This is also consistent with how tags is handled later in this function (tags = tags or []).

Suggested change
native.filegroup(
name = name,
srcs = kwargs.pop("data", []) + [requirements_txt],
srcs = data + [requirements_txt],
visibility = visibility,
)
data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints
data = data + [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints
data = data or []
native.filegroup(
name = name,
srcs = data + [requirements_txt],
visibility = visibility,
)
data = data + [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints


# Use the Label constructor so this is expanded in the context of the file
# where it appears, which is to say, in @rules_python
Expand Down