Skip to content

Commit 9b8b29b

Browse files
committed
fix: add explicit data attribute to compile_pip_requirements and forward it to the generated py_binary
1 parent 20db956 commit 9b8b29b

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

news/3858.fixed.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(compile_pip_requirements) Add the explicit `data` attribute and forward it
2+
directly to the generated `py_binary`, so files passed via `data` can be
3+
referenced from `extra_args` using `$(location ...)`.

python/private/pypi/pip_compile.bzl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ make it possible to have multiple tools inside the `pypi` directory
2222
load("//python:py_binary.bzl", _py_binary = "py_binary")
2323
load("//python:py_test.bzl", _py_test = "py_test")
2424

25+
# The `data` attribute does not allow duplicate labels, but user-provided `data`
26+
# can overlap with labels added from attributes like `src`.
27+
def _dedupe_data(data):
28+
res = []
29+
for d in data:
30+
if d not in res:
31+
res.append(d)
32+
return res
33+
34+
2535
def pip_compile(
2636
name,
2737
srcs = None,
@@ -39,6 +49,7 @@ def pip_compile(
3949
visibility = ["//visibility:private"],
4050
tags = None,
4151
constraints = [],
52+
data = [],
4253
**kwargs):
4354
"""Generates targets for managing pip dependencies with pip-compile (piptools).
4455
@@ -81,6 +92,7 @@ def pip_compile(
8192
tags: tagging attribute common to all build rules, passed to both the _test and .update rules.
8293
visibility: passed to both the _test and .update rules.
8394
constraints: a list of files containing constraints to pass to pip-compile with `--constraint`.
95+
data: A list of labels to include as part of the `data` attribute in the generated `py_binary`.
8496
**kwargs: other bazel attributes passed to the "_test" rule.
8597
"""
8698
if len([x for x in [srcs, src, requirements_in] if x != None]) > 1:
@@ -95,16 +107,18 @@ def pip_compile(
95107

96108
requirements_txt = name + ".txt" if requirements_txt == None else requirements_txt
97109

110+
data = data or []
111+
98112
# "Default" target produced by this macro
99113
# Allow a compile_pip_requirements rule to include another one in the data
100114
# for a requirements file that does `-r ../other/requirements.txt`
101115
native.filegroup(
102116
name = name,
103-
srcs = kwargs.pop("data", []) + [requirements_txt],
117+
srcs = data + [requirements_txt],
104118
visibility = visibility,
105119
)
106120

107-
data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints
121+
data = _dedupe_data(data + [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints)
108122

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

tests/integration/compile_pip_requirements/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ compile_pip_requirements(
2929
requirements_txt = "requirements_lock.txt",
3030
)
3131

32+
genquery(
33+
name = "requirements_update_data",
34+
expression = """
35+
some(labels(data, //:requirements.update) intersect //:requirements.in) union
36+
some(labels(data, //:requirements.update) intersect //:requirements_extra.in)
37+
""",
38+
scope = [
39+
":requirements.update",
40+
],
41+
)
42+
3243
compile_pip_requirements(
3344
name = "requirements_nohashes",
3445
src = "requirements.txt",

0 commit comments

Comments
 (0)